static void Main(string[] args) { var coder = new LzmaEncoder(); long inputSize = 0; long outputSize = 0; using (var inStream = File.OpenRead(@"c:\tmp\test.tar")) using (var outStream = File.Create(@"c:\tmp\test.lzma")) { var coderProperties = new Dictionary <CoderPropID, object>() { { CoderPropID.DictionarySize, 1024 * 1024 } }; coder.SetCoderProperties(coderProperties); coder.Code(inStream, outStream, -1, -1, new ConsoleProgress(inStream.Length)); inputSize = inStream.Length; outputSize = outStream.Length; } Console.WriteLine(); var ratio = Math.Round((double)inputSize / (double)outputSize, 2); Console.WriteLine("Input Size: {0}", inputSize); Console.WriteLine("Output Size: {0}", outputSize); Console.WriteLine("ratio: {0}", ratio); Console.WriteLine("=============== All done ==============="); long x = -1; ulong y = (ulong)x; Console.WriteLine(y); Console.ReadKey(); }
public void LzmaCompressAndDecompressShouldBeOk() { var source = this.CreateCompressionSourceData(); byte[] compressedBytes = null; byte[] properties = null; var encoderProperties = new Dictionary <CoderPropID, object>() { { CoderPropID.DictionarySize, 1024 * 512 } //512kb }; using (var inputStream = new MemoryStream(source, false)) using (var outputStream = new MemoryStream()) using (var propertiesStream = new MemoryStream()) { var encoder = new LzmaEncoder(); encoder.SetCoderProperties(encoderProperties); encoder.WriteCoderProperties(propertiesStream); properties = propertiesStream.ToArray(); encoder.Code(inputStream, outputStream, -1, -1, null); compressedBytes = outputStream.ToArray(); Assert.NotEqual(source, outputStream.ToArray()); } using (var inputStream = new MemoryStream(compressedBytes, false)) using (var outputStream = new MemoryStream()) { var decoder = new LzmaDecoder(); decoder.SetDecoderProperties(properties); decoder.Code(inputStream, outputStream, compressedBytes.Length, source.Length, null); Assert.Equal(source, outputStream.ToArray()); } }
/// <summary>写入数据并压缩</summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count"></param> public override void Write(byte[] buffer, int offset, int count) { if (_Mode != CompressionMode.Compress) { throw new InvalidOperationException(); } if (_Encoder == null) { _Encoder = new LzmaEncoder(); #region 计算压缩等级 CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)1 << Get(_Level, 0, 29), (Int32)2, (Int32)3, (Int32)0, (Int32)2, // Algorithm (Int32)256, // NumFastBytes "bt4", // MatchFinder MF false // EndMarker EOF }; //object[] properties = //{ // (Int32)Get(_Level, 0, 29), // (Int32)Get(_Level, 0, 4), // (Int32)Get(_Level, 0, 8), // (Int32)Get(_Level, 0, 4), // (Int32)Get(_Level, 0, 2), // Algorithm // (Int32)Get(_Level, 0, 128), // NumFastBytes // "bt4", // MatchFinder MF // false // EndMarker EOF //}; #endregion _Encoder.SetCoderProperties(propIDs, properties); _Encoder.WriteCoderProperties(BaseStream); // 8字节长度 BaseStream.Write(BitConverter.GetBytes((Int64)0)); } var ms = new MemoryStream(buffer, offset, count); _Encoder.Code(ms, BaseStream, -1, -1, null); }
/// <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) { if (!inStream.CanRead || !outStream.CanWrite) { throw new ArgumentException("The specified streams are invalid."); } var encoder = new LzmaEncoder(); 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, null); }
/// <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 LzmaEncoder(); 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()); } } }
/// <summary> /// Compresses the given stream and writes to the output stream. /// </summary> /// <param name="inStream">The input stream.</param> /// <param name="outStream">The output stream.</param> /// <param name="progress">The progress callback, if desired.</param> public static void Compress(Stream inStream, Stream outStream, ICodeProgress progress = null) { lock (m_EncoderLock) { if (m_Encoder == null) { m_Encoder = new LzmaEncoder(); m_Encoder.SetCoderProperties(propIDs, properties); } m_Encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } m_Encoder.Code(inStream, outStream, -1, -1, progress); } }
public static void Compress(Stream inStream, Stream outStream) { BinaryWriter writer = new BinaryWriter(outStream); //writer.WriteNucleusHeader(); //writer.WriteNucleusLZMAHeader(); //writer.Flush(); LzmaEncoder encoder = new LzmaEncoder(); 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); }