public static MemoryStream Compress(MemoryStream inStream) { inStream.Position = 0; CoderPropId[] propIDs = { CoderPropId.DictionarySize, CoderPropId.PosStateBits, CoderPropId.LitContextBits, CoderPropId.LitPosBits, CoderPropId.Algorithm }; object[] properties = { (1 << 16), 2, 3, 0, 2 }; var outStream = new MemoryStream(); var encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); for (var i = 0; i < 8; i++) outStream.WriteByte((byte) (inStream.Length >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); outStream.Flush(); outStream.Position = 0; return outStream; }
public void writeCompressedData(byte[] data) { int dicSize; if (compressionLevel <= 5) { dicSize = 1 << (compressionLevel * 2 + 14); } else if (compressionLevel == 6) { dicSize = 1 << 25; } else { dicSize = 1 << 26; } object[] properties = { (Int32)dicSize, (Int32)(2), (Int32)(3), (Int32)(0), (Int32)(2), (Int32)(compressionLevel < 7 ? 32 : 64), "bt4", true }; Encoder encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); MemoryStream inStream = new MemoryStream(data); MemoryStream outStream = new MemoryStream(); encoder.Code(inStream, outStream, -1, -1, null); byte[] compressedData = outStream.ToArray(); //This is the custom way of OpenCTM to write the LZMA properties writeLittleInt(compressedData.Length); encoder.WriteCoderProperties(BaseStream); Write(compressedData); }
//压缩文件 public static void CompressFileLZMA(string inFile, string outFile, Action callBack) { Encoder coder = new SevenZip.Compression.LZMA.Encoder(); DGFileUtil.CreateDirectoryWhenNotExists(outFile); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); output.Flush(); output.Close(); input.Close(); output.Dispose(); input.Dispose(); if (callBack != null) { callBack(); } }
/// <summary> /// Compress input stream into output stream using LZMA. /// Remeber to set desirable positions input and output streams. /// Input reading starts at inStream.Position and ends at inStream.Length. /// Output writing starts at outStream.Position. /// </summary> /// <param name="inStream">Input stream</param> /// <param name="outStream">Output stream</param> public static void Compress(Stream inStream, Stream outStream) { Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); var writer = new BinaryWriter(outStream, Encoding.UTF8); // Write original size. writer.Write(inStream.Length - inStream.Position); // Save position with compressed size. long positionForCompressedSize = outStream.Position; // Leave placeholder for size after compression. writer.Write((Int64)0); long positionForCompressedDataStart = outStream.Position; encoder.Code(inStream, outStream, -1, -1, null); long positionAfterCompression = outStream.Position; // Seek back to the placeholder for compressed size. outStream.Position = positionForCompressedSize; // Write size after compression. writer.Write((Int64)(positionAfterCompression - positionForCompressedDataStart)); // Restore position. outStream.Position = positionAfterCompression; }
private static void Test(string fromPath, string toPath) { //创建读取文件的流 using (FileStream fsReader = new FileStream(fromPath, FileMode.Open)) { //创建写入文件的流 using (FileStream fsWriter = new FileStream(toPath, FileMode.OpenOrCreate)) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); coder.WriteCoderProperties(fsWriter); //创建一个5M的缓冲区 byte[] buffers = new byte[1024 * 1024 * 5]; int i = 0; //文件读取到缓冲区 while ((i = fsReader.Read(buffers, 0, buffers.Length)) > 0) { //将缓冲区中内容写入fsWriter流 fsWriter.Write(buffers, 0, i); //显示进度 long l = fsWriter.Length; double proc = (double)l / fsReader.Length; Debug.LogFormat("拷贝进度{0}%", proc * 100); } coder.Code(fsReader, fsWriter, fsReader.Length, -1, null); // } } }
// Compress track back to a file that the games can read public static void Compress(string filePath, DecompressedTrack track) { // Setup LZMA encoder SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); SevenZip.CoderPropID[] coderPropIDs = { SevenZip.CoderPropID.DictionarySize, SevenZip.CoderPropID.LitContextBits, SevenZip.CoderPropID.LitPosBits, SevenZip.CoderPropID.PosStateBits }; object[] properties = LzmaPropertiesFromBytes(track.Properties); encoder.SetCoderProperties(coderPropIDs, properties); // Write compressed track to file using (FileStream fileStream = File.OpenWrite(filePath)) { // Write track header fileStream.Write(track.Header, 0, track.Header.Length); // Write LZMA header encoder.WriteCoderProperties(fileStream); fileStream.Write(BitConverter.GetBytes(track.Data.Length), 0, 4); // Write compressed track data using (MemoryStream memoryStream = new MemoryStream(track.Data)) { encoder.Code(memoryStream, fileStream, memoryStream.Length, -1, null); } } }
/// <summary>Compresses an individual content file</summary> /// <param name="path">Path of the content file that will be compressed</param> public static void CompressContentFile(string path) { // Change the asset's extension to .lzma to allow the normal content manage to live // side by side with the decompressing content manager string compressedFilename = Path.ChangeExtension(path, ".lzma"); Console.WriteLine( string.Format( "Compressing {0} to {1}...", Path.GetFileName(path), Path.GetFileName(compressedFilename) ) ); Encoder lzmaEncoder = new Encoder(); using(FileStream compressedFile = new FileStream(compressedFilename, FileMode.Create)) { using(FileStream uncompressedFile = new FileStream(path, FileMode.Open)) { BinaryWriter writer = new BinaryWriter(compressedFile); writer.Write((int)uncompressedFile.Length); lzmaEncoder.WriteCoderProperties(compressedFile); lzmaEncoder.Code( uncompressedFile, compressedFile, //uncompressedFile.Length, compressedFile.Length, 0, 0, null ); } // using uncompressedFile } // using compressedFile //File.Delete(path); }
private static void CompressFileLZMA(string inFile, string outFile) { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); output.Flush(); output.Close(); input.Flush(); input.Close(); stopwatch.Stop(); Debug.LogFormat("7z encode time: {0}", stopwatch.Elapsed.TotalSeconds); }
public static void CompressFileLZMA(string inFile, string outFile) { Int32 dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; string mf = "bt4"; bool eos = true; bool stdInMode = false; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), mf, eos }; using (FileStream inStream = new FileStream(inFile, FileMode.Open)) { using (FileStream outStream = new FileStream(outFile, FileMode.Create)) { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; if (eos || stdInMode) { fileSize = -1; } else { fileSize = inStream.Length; } for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, null); } } }
public void End() { if (Disabled) return; byte[] raw = m_stream.ToArray(); Header.DataSize = (uint)raw.Length; Header.Flag |= FlagCompressed; Header.Props = new byte[8]; Encoder lzma = new Encoder(); using (MemoryStream props = new MemoryStream(Header.Props)) lzma.WriteCoderProperties(props); MemoryStream compressed = new MemoryStream(); lzma.Code(new MemoryStream(raw), compressed, raw.LongLength, -1, null); raw = compressed.ToArray(); MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); writer.Write(Header.Id); writer.Write(Header.Version); writer.Write(Header.Flag); writer.Write(Header.Seed); writer.Write(Header.DataSize); writer.Write(Header.Hash); writer.Write(Header.Props); writer.Write(raw); m_data = ms.ToArray(); }
private static byte[] compress(byte[] decompressed) { byte[] retVal = null; bool eos = true; Int32 dictionary = 1 << 16; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 64; string mf = "bt4"; var propIDs = new CoderPropID[] { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; var properties = new object[] { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); using (Stream strmInStream = new MemoryStream(decompressed)) { strmInStream.Seek(0, 0); using (MemoryStream strmOutStream = new MemoryStream()) { encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(strmOutStream); Int64 fileSize = strmInStream.Length; for (int i = 0; i < 8; i++) { strmOutStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(strmInStream, strmOutStream, -1, -1, null); retVal = strmOutStream.ToArray(); } } return(retVal); }
public static void Compress(Stream inputStream, Stream outputStream) { Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outputStream); long fileSize = inputStream.Length; for (int i = 0; i < 8; i++) outputStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inputStream, outputStream, -1, -1, null); }
static void Compress(Stream input, Stream output) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); }
static public void compress(Stream inStream, Stream outStream) { var coder = new lzma.Encoder(); // Write the encoder properties coder.WriteCoderProperties(outStream); // Write the decompressed file size. outStream.Write(BitConverter.GetBytes(inStream.Length), 0, 8); // Encode the file. coder.Code(inStream, outStream, inStream.Length, -1, null); }
public static byte[] Compress(byte[] inputBytes) { MemoryStream inStream = new MemoryStream(inputBytes); MemoryStream outStream = new MemoryStream(); Encoder encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); return outStream.ToArray(); }
private static void CompressFileLZMA(string inFile, string outFile, bool bEncrypt = false) { // bEncrypt = false; SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Write the encoder properties coder.WriteCoderProperties(output); if (bEncrypt) { byte[] inbyte = new byte[input.Length]; input.Read(inbyte, 0, inbyte.Length); MemoryStream encryptinput = new MemoryStream(); Encrypt(inbyte, Localkey, LocalIV, encryptinput); //新增,加密 encryptinput.Position = 0; // Write the decompressed file size. output.Write(BitConverter.GetBytes(encryptinput.Length), 0, 8); // Encode the file. coder.Code(encryptinput, output, encryptinput.Length, -1, null); encryptinput.Flush(); encryptinput.Close(); } else { //input.Position = 0; // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); } //Debug.LogWarning("Compressing " + inFile + " size is : " + input.Length / 1024 + "Compressed File size :: " + output.Length / 1024 + "bEncrypt=" + bEncrypt); output.Flush(); output.Close(); input.Flush(); input.Close(); }
public static byte[] Compress(byte[] data) { using (MemoryStream instrm = new MemoryStream (data)) using (MemoryStream outstrm = new MemoryStream ()) { Encoder encoder = new Encoder (); encoder.WriteCoderProperties (outstrm); outstrm.WriteByte ((byte)(data.Length >> 24)); outstrm.WriteByte ((byte)(data.Length >> 16)); outstrm.WriteByte ((byte)(data.Length >> 8)); outstrm.WriteByte ((byte)(data.Length >> 0)); encoder.Code (instrm, outstrm, -1, -1, null); outstrm.Close (); return outstrm.ToArray (); } }
public void compress(Stream inStream, Stream outStream) { var coder = new lzma.Encoder(); // Write the encoder properties coder.WriteCoderProperties(outStream); // Write the decompressed file size. outStream.Write(BitConverter.GetBytes(inStream.Length), 0, 8); // Encode the file. coder.Code(inStream, outStream, inStream.Length, -1, null); }
// 使用LZMA算法压缩文件 public static void CompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); coder.WriteCoderProperties(output); byte[] data = BitConverter.GetBytes(input.Length); output.Write(data, 0, data.Length); coder.Code(input, output, input.Length, -1, null); output.Flush(); output.Close(); input.Close(); }
public static byte[] Compress(byte[] inputBytes) { MemoryStream inStream = new MemoryStream(inputBytes); MemoryStream outStream = new MemoryStream(); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, null); return(outStream.ToArray()); }
private static void CompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); output.Flush(); output.Close(); input.Close(); }
/// <summary> /// Compresses the inputBytes in Lzma. /// </summary> /// <param name="inputBytes">Bytes to compress.</param> /// <returns>Compressed bytes.</returns> public static byte[] Compress(byte[] inputBytes) { if (inputBytes == null) throw new ArgumentNullException("inputBytes"); var inStream = new MemoryStream(inputBytes); var outStream = new MemoryStream(); var encoder = new Encoder(); encoder.SetCoderProperties(PropertiesIDs, Properties); encoder.WriteCoderProperties(outStream); var fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); return outStream.ToArray(); }
public static System.IO.Stream Compress(System.IO.Stream stream, uint length, bool EOF = false) { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); System.IO.MemoryStream sout = new System.IO.MemoryStream(); if (EOF) { encoder.SetCoderProperties(new SevenZip.CoderPropID[] { SevenZip.CoderPropID.EndMarker }, new object[] { true }); } else { //写入文件长度 sout.Write(BitConverter.GetBytes(length), 0, 4); } encoder.WriteCoderProperties(sout); encoder.Code(stream, sout, (int)length, -1, null); sout.Position = 0; return sout; }
public static byte[] Compress(byte[] bytes) { Encoder coder = new Encoder(); using (MemoryStream input = new MemoryStream(bytes)) { using (MemoryStream output = new MemoryStream()) { // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); return output.ToArray(); } } }
private static Stream CompressInternal(Stream inStream) { var outStream = new MemoryStream(); var encoder = new LZMA.Encoder(); encoder.SetCoderProperties(s_propertiesIds, s_properties); encoder.WriteCoderProperties(outStream); // Uncompressed size. var outSize = inStream.Length; var outSizeBytes = BitConverter.GetBytes(outSize); outStream.Write(outSizeBytes, 0, 4); encoder.Code(inStream, outStream, -1, -1, null); outStream.Seek(0, SeekOrigin.Begin); return(outStream); }
public static void CompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(System.BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); output.Flush(); output.Close(); }
public void End() { if (Disabled) return; Disabled = true; byte[] raw = m_stream.ToArray(); Header.DataSize = (uint)raw.Length; Header.Props = new byte[8]; if(Compressed){ //录像不压缩 Header.Flag |= FlagCompressed; Encoder lzma = new Encoder(); using (MemoryStream props = new MemoryStream(Header.Props)){ lzma.WriteCoderProperties(props); } using(MemoryStream compressed = new MemoryStream()){ using(MemoryStream rawsream = new MemoryStream(raw)){ try{ lzma.Code(rawsream, compressed, raw.LongLength, -1, null); }catch{ Logger.Warn("replay out of memory.raw.length="+raw.Length); } raw = compressed.ToArray(); } } } using(MemoryStream ms = new MemoryStream()){ using(BinaryWriter writer = new BinaryWriter(ms)){ writer.Write(Header.Id); writer.Write(Header.Version); writer.Write(Header.Flag); writer.Write(Header.Seed); writer.Write(Header.DataSize); writer.Write(Header.Hash); writer.Write(Header.Props); writer.Write(raw); } m_data = ms.ToArray(); } ThreadPool.QueueUserWorkItem(new WaitCallback(saveYrp)); Close(); }
public static byte[] Compress(byte[] bytes) { using (MemoryStream input = new MemoryStream(bytes)) { MemoryStream output = new MemoryStream(); Encoder encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(output); if (BitConverter.IsLittleEndian) { byte[] LengthHeader = BitConverter.GetBytes(input.Length); output.Write(LengthHeader, 0, LengthHeader.Length); } encoder.Code(input, output, input.Length, -1, null); return output.ToArray(); } }
private static void CompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); /* * FileMode 以何种方式打开或者创建文件 * FileMode.Open 打开 * FileMode.Create 创建并覆盖 * FileMode.CreateNew 创建新文件 * FileMode.OpenOrCreate 打开并创建 * FileMode.Truncate 覆盖文件 * FileMode.Append 追加 * * FileAcess 文件流对象如何访问该文件 * FileAcess.Read 只读 * FileAcess.Write 写 * FileAcess.ReadWirte 读写 * * FileShare 进程如何共享文件 * FileShare.None 拒绝共享 * Read 、Write、ReadWrite(同时读写)、Delete * //用法 * FileStream logFile = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write) */ //下面的这个写法 适合小文件 如果文件太大会溢出 FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); coder.WriteCoderProperties(output); //写入内存 output.Write(BitConverter.GetBytes(input.Length), 0, 8); //使用缓冲区中的数据将字节块写入此流 coder.Code(input, output, input.Length, -1, null); // //写入硬盘 output.Flush(); //清除缓冲区,把所有数据写入文件中 output.Close(); //关闭当前流并释放与当前流关联的任何资源(如套接字和文件句柄) input.Close(); input.Dispose(); //释放流所有使用的资源 output.Dispose(); }
public void End() { if (Disabled) return; byte[] raw = m_stream.ToArray(); Header.DataSize = (uint)raw.Length; Header.Flag |= FlagCompressed; Header.Props = new byte[8]; Encoder lzma = new Encoder(); using (MemoryStream props = new MemoryStream(Header.Props)) lzma.WriteCoderProperties(props); MemoryStream compressed = new MemoryStream(); lzma.Code(new MemoryStream(raw), compressed, raw.LongLength, -1, null); raw = compressed.ToArray(); MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); writer.Write(Header.Id); writer.Write(Header.Version); writer.Write(Header.Flag); writer.Write(Header.Seed); writer.Write(Header.DataSize); writer.Write(Header.Hash); writer.Write(Header.Props); writer.Write(raw); m_data = ms.ToArray(); if(Program.Config.AutoReplay){ ThreadPool.QueueUserWorkItem(new WaitCallback(saveYrp)); } }
public static bool Compress(byte[] inputBytes, string FileName, bool Overwrite) { if (File.Exists(FileName)) { if(Overwrite) { File.Delete(FileName); } else { return false; } } //MemoryStream inStream = new MemoryStream(inputBytes); using(FileStream outStream = File.Create(FileName)) { MemoryStream inStream = new MemoryStream(inputBytes); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); } return true; }
public static void CompressFileLZMA(MemoryStream inFile, string outFile) { int dictionary = 33554432; int posStateBits = 2; int litContextBits = 3; int litPosBits = 0; int algorithm = 2; int numFastBytes = 32; string mf = "bt4"; bool eos = true; bool stdInMode = false; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos }; byte[] TibiaHeader = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; using FileStream outStream = new FileStream(outFile, FileMode.Create); outStream.Write(TibiaHeader); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize; if (eos || stdInMode) { fileSize = -1; } else { fileSize = inFile.Length; } for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inFile, outStream, -1, -1, null); MemoryStream tmp = new MemoryStream(); int size = (int)outStream.Length - 32; MyBinaryWriter bw = new MyBinaryWriter(tmp); bw.Write(new byte[] { 0x70, 0x0A, 0xFA, 0x80, 0x24 }); bw.Write7BitEncodedInt(size); if (tmp.Length == 8) { outStream.Position = 0x18; } else { outStream.Position = 0x19; } outStream.Write(tmp.ToArray(), 0, (int)tmp.Length); outStream.Position = 0; outStream.Close(); }
/// <summary> /// Compresses the lzma. /// </summary> private static void CompressLzma() { if(resourcesLis == null || resourcesLis.Count <= 0) { return; } string assetBundlePath = Application.dataPath + "/AssetBundle"; string lzmaFilePath = assetBundlePath + "/AssetBundle.lzma"; if(!Directory.Exists(assetBundlePath)) { Directory.CreateDirectory(assetBundlePath); } try { MemoryStream memoryStream = new MemoryStream(); FileStream compressStream = new FileStream(lzmaFilePath, FileMode.OpenOrCreate, FileAccess.Write); int lastIndex = Application.dataPath.LastIndexOf("/"); string prePath = Application.dataPath.Substring(0, lastIndex + 1); int filePathCount = resourcesLis.Count; for (int i = 0; i < filePathCount; i++) { string assetPath = AssetDatabase.GetAssetPath(resourcesLis[i]); string filePath = prePath + assetPath; string zipBundlePath = assetPath.Replace("Assets/", ""); FileStream tempFileStream = File.Open(filePath, FileMode.Open); StringBuilder sb = new StringBuilder(); // set header info: path + filesie + separator sb.Append(zipBundlePath).Append(",").Append(tempFileStream.Length).Append("\n"); byte[] tempBuff = new byte[tempFileStream.Length]; byte[] header = Encoding.UTF8.GetBytes(sb.ToString()); tempFileStream.Read(tempBuff, 0, (int)tempFileStream.Length); // get file data memoryStream.Write(header, 0, header.Length); memoryStream.Write(tempBuff, 0, tempBuff.Length); tempFileStream.Close(); } // important !!! memoryStream.Position = 0; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.WriteCoderProperties(compressStream); byte[] compressLen = new byte[8]; // file size for (int i = 0; i < compressLen.Length; i++) { compressLen[i] = (byte)(memoryStream.Length >> (8 * i)); } compressStream.Write(compressLen, 0, 8); CodeProgress codeProgress = new CodeProgress(); // compress codeProgress.totalSize = memoryStream.Length; encoder.Code(memoryStream, compressStream, memoryStream.Length, -1, codeProgress); memoryStream.Flush(); memoryStream.Close(); compressStream.Close(); AssetDatabase.Refresh(); // refresh asssets EditorUtility.ClearProgressBar(); } catch (Exception exe) { Debug.Log(exe.Message); } }
static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize) { if (numIterations <= 0) return 0; if (dictionarySize < (1 << 18)) { System.Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)"); return 1; } System.Console.Write("\n Compressing Decompressing\n\n"); Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); CoderPropID[] propIDs = { CoderPropID.DictionarySize, }; object[] properties = { (Int32)(dictionarySize), }; UInt32 kBufferSize = dictionarySize + kAdditionalSize; UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize; encoder.SetCoderProperties(propIDs, properties); System.IO.MemoryStream propStream = new System.IO.MemoryStream(); encoder.WriteCoderProperties(propStream); byte[] propArray = propStream.ToArray(); CBenchRandomGenerator rg = new CBenchRandomGenerator(); rg.Set(kBufferSize); rg.Generate(); CRC crc = new CRC(); crc.Init(); crc.Update(rg.Buffer, 0, rg.BufferSize); CProgressInfo progressInfo = new CProgressInfo(); progressInfo.ApprovedStart = dictionarySize; UInt64 totalBenchSize = 0; UInt64 totalEncodeTime = 0; UInt64 totalDecodeTime = 0; UInt64 totalCompressedSize = 0; MemoryStream inStream = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize); MemoryStream compressedStream = new MemoryStream((int)kCompressedBufferSize); CrcOutStream crcOutStream = new CrcOutStream(); for (Int32 i = 0; i < numIterations; i++) { progressInfo.Init(); inStream.Seek(0, SeekOrigin.Begin); compressedStream.Seek(0, SeekOrigin.Begin); encoder.Code(inStream, compressedStream, -1, -1, progressInfo); TimeSpan sp2 = DateTime.UtcNow - progressInfo.Time; UInt64 encodeTime = (UInt64)sp2.Ticks; long compressedSize = compressedStream.Position; if (progressInfo.InSize == 0) throw (new Exception("Internal ERROR 1282")); UInt64 decodeTime = 0; for (int j = 0; j < 2; j++) { compressedStream.Seek(0, SeekOrigin.Begin); crcOutStream.Init(); decoder.SetDecoderProperties(propArray); UInt64 outSize = kBufferSize; System.DateTime startTime = DateTime.UtcNow; decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null); TimeSpan sp = (DateTime.UtcNow - startTime); decodeTime = (ulong)sp.Ticks; if (crcOutStream.GetDigest() != crc.GetDigest()) throw (new Exception("CRC Error")); } UInt64 benchSize = kBufferSize - (UInt64)progressInfo.InSize; PrintResults(dictionarySize, encodeTime, benchSize, false, 0); System.Console.Write(" "); PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize); System.Console.WriteLine(); totalBenchSize += benchSize; totalEncodeTime += encodeTime; totalDecodeTime += decodeTime; totalCompressedSize += (ulong)compressedSize; } System.Console.WriteLine("---------------------------------------------------"); PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0); System.Console.Write(" "); PrintResults(dictionarySize, totalDecodeTime, kBufferSize * (UInt64)numIterations, true, totalCompressedSize); System.Console.WriteLine(" Average"); return 0; }
/// <summary> /// 从缓冲的目录中压缩; /// </summary> /// <param name="isAssetDir"> 是否是 asset 中的目录</param> private static void CompressLzmaFromDir(bool isAssetDir) { if (autoPathArr == null || autoPathArr.Count <= 0) { return; } string assetBundlePath = Application.dataPath + "/StreamingAssets/Data"; string lzmaFilePath = assetBundlePath + "/Data." + LzmaTools.zipName; if (!Directory.Exists(assetBundlePath)) { Directory.CreateDirectory(assetBundlePath); } try { if (File.Exists(lzmaFilePath)) { File.Delete(lzmaFilePath); } MemoryStream memoryStream = new MemoryStream(); FileStream compressStream = new FileStream(lzmaFilePath, FileMode.OpenOrCreate, FileAccess.Write); int lastIndex = Application.dataPath.LastIndexOf("/"); string prePath = Application.dataPath.Substring(0, lastIndex + 1); int filePathCount = autoPathArr.Count; for (int i = 0; i < filePathCount; i++) { string assetPath = autoPathArr[i]; if (assetPath != null && assetPath != "") { string filePath = assetPath; string zipBundlePath = ""; if (isAssetDir) { zipBundlePath = assetPath.Replace("Assets/", ""); } else { zipBundlePath = assetPath.Replace(Directory.GetCurrentDirectory() + "/../", ""); filePath = assetPath; zipBundlePath = zipBundlePath.Replace("DataTXT", "Data"); } FileStream tempFileStream = File.Open(filePath, FileMode.Open); StringBuilder sb = new StringBuilder(); // set header info: path + filesie + separator sb.Append(zipBundlePath).Append(",").Append(tempFileStream.Length).Append("\n"); byte[] tempBuff = new byte[tempFileStream.Length]; byte[] header = Encoding.UTF8.GetBytes(sb.ToString()); tempFileStream.Read(tempBuff, 0, (int)tempFileStream.Length); // get file data memoryStream.Write(header, 0, header.Length); memoryStream.Write(tempBuff, 0, tempBuff.Length); tempFileStream.Close(); } } // important !!! memoryStream.Position = 0; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.WriteCoderProperties(compressStream); byte[] compressLen = new byte[8]; // file size for (int i = 0; i < compressLen.Length; i++) { compressLen[i] = (byte)(memoryStream.Length >> (8 * i)); } compressStream.Write(compressLen, 0, 8); CodeProgress codeProgress = new CodeProgress(); // compress codeProgress.totalSize = memoryStream.Length; encoder.Code(memoryStream, compressStream, memoryStream.Length, -1, codeProgress); memoryStream.Flush(); memoryStream.Close(); compressStream.Close(); AssetDatabase.Refresh(); // refresh asssets EditorUtility.ClearProgressBar(); Debug.Log("success compress "); } catch (Exception exe) { Debug.LogError(exe.Message); } }
/// <summary> /// Compresses data in a stream and writes the compressed data into another stream /// </summary> /// <param name="destination">Destination stream the data is written to</param> /// <param name="source">Stream from which the data to be compressed is taken</param> /// <returns>The number of bytes that were written into the destination stream</returns> private static int compress(Stream destination, Stream source) { long startingPosition = destination.Position; Encoder lzmaEncoder = new Encoder(); lzmaEncoder.WriteCoderProperties(destination); lzmaEncoder.Code( source, destination, 0, 0, null ); return (int)(destination.Position - startingPosition); }
private static void Compress(Stream inputStream, Stream outputStream) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); // Write encoder properties to output stream coder.WriteCoderProperties(outputStream); // Write size of input stream to output stream. outputStream.Write(BitConverter.GetBytes(inputStream.Length), 0, 8); // Encode coder.Code(inputStream, outputStream, inputStream.Length, -1, null); outputStream.Flush(); outputStream.Close(); }
public override byte[] Code(Byte[] data) { int dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), "bt4", false }; MemoryStream inpuMemoryStream = new MemoryStream(data); MemoryStream outputMemoryStream = new MemoryStream(); SevenZip.Compression.LZMA.Encoder encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outputMemoryStream); Int64 fileSize = inpuMemoryStream.Length; for (int i = 0; i < 8; i++) { byte b = (Byte) (fileSize >> (8*i)); outputMemoryStream.WriteByte(b); } encoder.Code(inpuMemoryStream, outputMemoryStream, -1, -1, null); var outBuffer = outputMemoryStream.ToArray(); return outBuffer; //int keyIndex = 0; //for (uint i = 0; i < data.Length; ++i) //{ // data[i] ^= mKey[keyIndex]; // if (keyIndex < mKey.Length - 1) // { // ++keyIndex; // } // else // { // keyIndex = 0; // } //} //return data; }
/// <summary> /// Compresses the report for uploading. /// </summary> /// <param name="progress">The <see cref="ProgressManager"/> instance that the /// Upload function is using.</param> /// <param name="progressChanged">The progress changed event handler that should /// be called for upload progress updates.</param> private void Compress(SteppedProgressManager progress, ProgressChangedEventHandler progressChanged) { using (FileStream archiveStream = new FileStream(ReportBaseName + ".tar", FileMode.Create, FileAccess.Write)) { //Add the report into a tar file TarArchive archive = TarArchive.CreateOutputTarArchive(archiveStream); foreach (FileInfo file in Report.Files) { TarEntry entry = TarEntry.CreateEntryFromFile(file.FullName); entry.Name = Path.GetFileName(entry.Name); archive.WriteEntry(entry, false); } archive.Close(); } ProgressManager step = new ProgressManager(); progress.Steps.Add(new SteppedProgressManagerStep(step, 0.5f, "Compressing")); CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(1 << 24), //Dictionary Size (Int32)2, //PosState Bits (Int32)0, //LitContext Bits (Int32)2, //LitPos Bits (Int32)2, //Algorithm (Int32)128, //Fast Bytes "bt4", //Match Finger true //Write end-of-stream }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); using (FileStream sevenZipFile = new FileStream(ReportBaseName + ".tar.7z", FileMode.Create)) using (FileStream tarStream = new FileStream(ReportBaseName + ".tar", FileMode.Open, FileAccess.Read, FileShare.Read, 262144, FileOptions.DeleteOnClose)) { encoder.WriteCoderProperties(sevenZipFile); Int64 fileSize = -1; for (int i = 0; i < 8; i++) { sevenZipFile.WriteByte((Byte)(fileSize >> (8 * i))); } step.Total = tarStream.Length; ICodeProgress callback = progressChanged == null ? null : new SevenZipProgressCallback(this, progress, step, progressChanged); encoder.Code(tarStream, sevenZipFile, -1, -1, callback); } }
private static void Compress(Stream inputStream, Stream outputStream, LZMADictionarySize dictSize) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); Int32 dictSize32 = (Int32)dictSize; coder.SetCoderProperties (new SevenZip.CoderPropID[] {SevenZip.CoderPropID.DictionarySize}, new object[] {dictSize32}); // Write encoder properties to output stream coder.WriteCoderProperties(outputStream); // Write size of input stream to output stream. outputStream.Write(BitConverter.GetBytes(inputStream.Length), 0, 8); // Encode coder.Code(inputStream, outputStream, inputStream.Length, -1, null); outputStream.Flush(); outputStream.Close(); }
static void Main(string[] args) { Softlynx.XDelta.Wrapper.Encode( @"C:\temp\test\src", @"C:\temp\test\dst", @"C:\temp\test\patch1"); Softlynx.XDelta.Wrapper.Decode( @"C:\temp\test\src", @"C:\temp\test\patch1", @"C:\temp\test\dst.new"); return; Stream src=File.Open(@"C:\temp\test\src",FileMode.Open); Stream bzpart = File.Open(@"C:\temp\test\src.bz.1", FileMode.Create); //BZip2.Compress(src, bzpart, false, 9); PipeStream bzfile=new PipeStream(bzpart); Stream bzfilter = new ZOutputStream(bzfile,9); //StreamCopy(src, bzfilter); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.WriteCoderProperties(bzfile); encoder.Code(src, bzfile, 0, 0, null); //bzfilter.Flush(); src.Close(); bzpart.Close(); src = File.Open(@"C:\temp\test\dst", FileMode.Open); bzpart = File.Open(@"C:\temp\test\src.bz.2", FileMode.Create); bzfile.SwitchStream(bzpart); encoder.Code(src, bzfile, 0, 0, null); //StreamCopy(src, bzfilter); bzfilter.Close(); src.Close(); bzpart.Close(); /* VDiffEncoder.BzipEncode( @"C:\temp\test\src", @"C:\temp\test\dst", @"C:\temp\test\patch1"); VDiffDecoder.BzipDecode( @"C:\temp\test\src", @"C:\temp\test\patch1", @"C:\temp\test\dst.new"); */ return; /* Softlynx.BSDiffTools.Patch.Create( @"C:\temp\test\src", @"C:\temp\test\dst", @"C:\temp\test\patch"); Softlynx.BSDiffTools.Patch.Apply( @"C:\temp\test\src", @"C:\temp\test\dst.newa", @"C:\temp\test\patch"); SoftLynx.BSDiffSharp.BSPatch.Apply( @"C:\temp\test\src", @"C:\temp\test\dst.newb", @"C:\temp\test\patch"); */ }
public static void Main() { var l = new HttpListener(); l.Prefixes.Add("http://*:80/"); l.Start(); Console.WriteLine("HttpListener is listening..."); while (l.IsListening) { var cont = l.GetContext(); Console.WriteLine("Incoming request for: {0}", cont.Request.RawUrl.Replace("/", @"\")); var fname = Environment.CurrentDirectory + cont.Request.RawUrl.Replace("/", @"\"); if (!File.Exists(fname)) { var modfname = fname.Substring(0, fname.Length - 4); if (Path.GetExtension(fname) == ".7zl" && File.Exists(modfname)) { var outStream = new FileStream(fname, FileMode.Create, FileAccess.Write); var inStream = new FileStream(modfname, FileMode.Open, FileAccess.Read); var encoder = new LZ.Encoder(); encoder.SetCoderProperties(PropIDs, Properties); encoder.WriteCoderProperties(outStream); var instreamLen = inStream.Length; outStream.Write(BitConverter.GetBytes(instreamLen), 0, 8); encoder.Code(inStream, outStream, -1, -1, null); outStream.Close(); inStream.Close(); } else { cont.Response.Close(); continue; } } var buff = File.ReadAllBytes(fname); var mime = "text/html"; switch (Path.GetExtension(fname)) { case "txt": mime = "text/plain"; break; case "tga": mime = "image/targa"; break; case "bmp": case "png": case "gif": case "tiff": case "jpeg": mime = String.Format("image/{0}", Path.GetExtension(fname)); break; case "tif": mime = "image/tiff"; break; case "jpg": case "jpe": mime = "image/jpeg"; break; case "js": mime = "text/javascript"; break; case "css": mime = "text/css"; break; } try { cont.Response.ContentType = mime; cont.Response.ContentLength64 = buff.Length; cont.Response.OutputStream.Write(buff, 0, buff.Length); //cont.Response.Close(); } catch (HttpListenerException) { } } }
/// <inheritdoc /> public byte[] Compress(byte[] data, Action<double> progressFunc = null) { CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { 1 << 23, 2, 3, 0, 2, 128, "bt4", false }; var x = new MemoryStream(); var encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(x); Int64 fileSize; fileSize = data.Length; for (int i = 0; i < 8; i++) x.WriteByte((Byte)(fileSize >> (8 * i))); ICodeProgress progress = null; if (progressFunc != null) progress = new CompressionLogger(progressFunc, data.Length); encoder.Code(new MemoryStream(data), x, -1, -1, progress); return x.ToArray(); }
static int Main2(string[] args) { System.Console.WriteLine("\nLZMA# 4.61 2008-11-23\n"); if (args.Length == 0) { PrintHelp(); return 0; } SwitchForm[] kSwitchForms = new SwitchForm[13]; int sw = 0; kSwitchForms[sw++] = new SwitchForm("?", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("H", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("EOS", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("SI", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("SO", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1); Parser parser = new Parser(sw); try { parser.ParseStrings(kSwitchForms, args); } catch { return IncorrectCommand(); } if (parser[(int)Key.Help1].ThereIs || parser[(int)Key.Help2].ThereIs) { PrintHelp(); return 0; } System.Collections.ArrayList nonSwitchStrings = parser.NonSwitchStrings; int paramIndex = 0; if (paramIndex >= nonSwitchStrings.Count) return IncorrectCommand(); string command = (string)nonSwitchStrings[paramIndex++]; command = command.ToLower(); bool dictionaryIsDefined = false; Int32 dictionary = 1 << 21; if (parser[(int)Key.Dictionary].ThereIs) { Int32 dicLog; if (!GetNumber((string)parser[(int)Key.Dictionary].PostStrings[0], out dicLog)) IncorrectCommand(); dictionary = (Int32)1 << dicLog; dictionaryIsDefined = true; } string mf = "bt4"; if (parser[(int)Key.MatchFinder].ThereIs) mf = (string)parser[(int)Key.MatchFinder].PostStrings[0]; mf = mf.ToLower(); if (command == "b") { const Int32 kNumDefaultItereations = 10; Int32 numIterations = kNumDefaultItereations; if (paramIndex < nonSwitchStrings.Count) if (!GetNumber((string)nonSwitchStrings[paramIndex++], out numIterations)) numIterations = kNumDefaultItereations; return LzmaBench.LzmaBenchmark(numIterations, (UInt32)dictionary); } string train = ""; if (parser[(int)Key.Train].ThereIs) train = (string)parser[(int)Key.Train].PostStrings[0]; bool encodeMode = false; if (command == "e") encodeMode = true; else if (command == "d") encodeMode = false; else IncorrectCommand(); bool stdInMode = parser[(int)Key.StdIn].ThereIs; bool stdOutMode = parser[(int)Key.StdOut].ThereIs; Stream inStream = null; if (stdInMode) { throw (new Exception("Not implemeted")); } else { if (paramIndex >= nonSwitchStrings.Count) IncorrectCommand(); string inputName = (string)nonSwitchStrings[paramIndex++]; inStream = new FileStream(inputName, FileMode.Open, FileAccess.Read); } FileStream outStream = null; if (stdOutMode) { throw (new Exception("Not implemeted")); } else { if (paramIndex >= nonSwitchStrings.Count) IncorrectCommand(); string outputName = (string)nonSwitchStrings[paramIndex++]; outStream = new FileStream(outputName, FileMode.Create, FileAccess.Write); } FileStream trainStream = null; if (train.Length != 0) trainStream = new FileStream(train, FileMode.Open, FileAccess.Read); if (encodeMode) { if (!dictionaryIsDefined) dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; bool eos = parser[(int)Key.EOS].ThereIs || stdInMode; if (parser[(int)Key.Mode].ThereIs) if (!GetNumber((string)parser[(int)Key.Mode].PostStrings[0], out algorithm)) IncorrectCommand(); if (parser[(int)Key.FastBytes].ThereIs) if (!GetNumber((string)parser[(int)Key.FastBytes].PostStrings[0], out numFastBytes)) IncorrectCommand(); if (parser[(int)Key.LitContext].ThereIs) if (!GetNumber((string)parser[(int)Key.LitContext].PostStrings[0], out litContextBits)) IncorrectCommand(); if (parser[(int)Key.LitPos].ThereIs) if (!GetNumber((string)parser[(int)Key.LitPos].PostStrings[0], out litPosBits)) IncorrectCommand(); if (parser[(int)Key.PosBits].ThereIs) if (!GetNumber((string)parser[(int)Key.PosBits].PostStrings[0], out posStateBits)) IncorrectCommand(); CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), mf, eos }; Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; if (eos || stdInMode) fileSize = -1; else fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); if (trainStream != null) { CDoubleStream doubleStream = new CDoubleStream(); doubleStream.s1 = trainStream; doubleStream.s2 = inStream; doubleStream.fileIndex = 0; inStream = doubleStream; long trainFileSize = trainStream.Length; doubleStream.skipSize = 0; if (trainFileSize > dictionary) doubleStream.skipSize = trainFileSize - dictionary; trainStream.Seek(doubleStream.skipSize, SeekOrigin.Begin); encoder.SetTrainSize((uint)(trainFileSize - doubleStream.skipSize)); } encoder.Code(inStream, outStream, -1, -1, null); } else if (command == "d") { byte[] properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) throw (new Exception("input .lzma is too short")); Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); decoder.SetDecoderProperties(properties); if (trainStream != null) { if (!decoder.Train(trainStream)) throw (new Exception("can't train")); } long outSize = 0; for (int i = 0; i < 8; i++) { int v = inStream.ReadByte(); if (v < 0) throw (new Exception("Can't Read 1")); outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = inStream.Length - inStream.Position; decoder.Code(inStream, outStream, compressedSize, outSize, null); } else throw (new Exception("Command Error")); return 0; }
/// <summary> /// Compresses the specified stream with LZMA algorithm (C# inside) /// </summary> /// <param name="inStream">The source uncompressed stream</param> /// <param name="outStream">The destination compressed stream</param> /// <param name="inLength">The length of uncompressed data (null for inStream.Length)</param> /// <param name="codeProgressEvent">The event for handling the code progress</param> public static void CompressStream(Stream inStream, Stream outStream, int? inLength, EventHandler<ProgressEventArgs> codeProgressEvent){ if (!inStream.CanRead || !outStream.CanWrite) throw new ArgumentException("The specified streams are invalid."); var encoder = new Encoder(); WriteLzmaProperties(encoder); encoder.WriteCoderProperties(outStream); long streamSize = inLength.HasValue ? inLength.Value : inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((byte) (streamSize >> (8*i))); encoder.Code(inStream, outStream, -1, -1, new LzmaProgressCallback(streamSize, codeProgressEvent)); }
/// <summary> /// Compresses byte array with LZMA algorithm (C# inside) /// </summary> /// <param name="data">Byte array to compress</param> /// <returns>Compressed byte array</returns> public static byte[] CompressBytes(byte[] data){ using (var inStream = new MemoryStream(data)) using (var outStream = new MemoryStream()) { var encoder = new Encoder(); WriteLzmaProperties(encoder); encoder.WriteCoderProperties(outStream); long streamSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((byte) (streamSize >> (8*i))); encoder.Code(inStream, outStream, -1, -1, null); return outStream.ToArray(); } }
private void CreateManifest_(List<FileInfo> results, string[] args) { string dataDir = args[0]; string patcherExe = args[1]; string destPath = args[2]; string outputFilename = args[3]; string projectPath = args[4]; SetStatus("Getting patcher version", 0); ProcessOutput patcher = null; try { patcher = ProcessOutput.Run(patcherExe, "-version", "", null, false, false); } catch (System.Exception) { } string patcherVersion = "[Fix me by hand]"; if (patcher != null) { patcher.Wait(); patcherVersion = patcher.stdout.Aggregate((i, j) => i + "\n" + j); } CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker, }; object[] properties = { (Int32)(1<<26), (Int32)(2), (Int32)(3), (Int32)(2), (Int32)(128), "bt4", false, }; SetStatus("Starting XML file...", 0); XmlDocument doc = new XmlDocument(); XmlElement rootNode = doc.CreateElement("dayzrp"); XmlElement serversNode = doc.CreateElement("servers"); XmlElement patchNode = doc.CreateElement("patch"); XmlAttribute data = doc.CreateAttribute("data"); XmlAttribute launcherVersion = doc.CreateAttribute("launcherVersion"); XmlAttribute launcherUrl = doc.CreateAttribute("launcherUrl"); XmlAttribute patchMode = doc.CreateAttribute("mode"); launcherVersion.Value = patcherVersion; launcherUrl.Value = Path.GetFileName(patcherExe); data.Value = dataDir; patchMode.Value = "lzma"; patchNode.Attributes.Append(data); patchNode.Attributes.Append(launcherUrl); patchNode.Attributes.Append(launcherVersion); patchNode.Attributes.Append(patchMode); XmlComment comment = doc.CreateComment("<server name='RP1 : S1' host='81.170.227.227' port='2302'/>"); serversNode.AppendChild(comment); rootNode.AppendChild(serversNode); rootNode.AppendChild(patchNode); doc.AppendChild(rootNode); int progress = 0; double progressCount = (double)results.Count * 2; foreach (var file in results) { XmlElement fileNode = doc.CreateElement("file"); XmlAttribute path = doc.CreateAttribute("path"); XmlAttribute hash = doc.CreateAttribute("hash"); XmlAttribute url = doc.CreateAttribute("url"); string nameValue = GetRelativeName(file, projectPath); string hashValue; SetStatus("Hashing " + nameValue, progress++ / progressCount); using (Stream stream = file.OpenRead()) { hashValue = StringExtension.ToHexString(m_md5.ComputeHash(stream)); stream.Close(); } path.Value = nameValue; hash.Value = hashValue; url.Value = dataDir + "/" + nameValue + ".lzma"; fileNode.Attributes.Append(path); fileNode.Attributes.Append(hash); fileNode.Attributes.Append(url); patchNode.AppendChild(fileNode); SetStatus("Compressing " + nameValue, progress++ / progressCount); using (Stream inStream = file.OpenRead()) { string outFile = Path.Combine(destPath, nameValue + ".lzma"); EnsurePathExists(outFile); // if( (new FileInfo(outFile)).Exists ) // continue; Stream outStream = new FileStream(outFile, FileMode.Create, FileAccess.Write); LZMA.Encoder encoder = new LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); inStream.Close(); outStream.Close(); } } SetStatus("Saving XML...", 1); doc.Save(outputFilename); SetStatus("Done!", 0); Dispatcher.BeginInvoke(DispatcherPriority.Send, (Action)delegate() { builtButton.IsEnabled = true; }); }