public void Lzma_CompressDecompressEndMark() { // Start with a MemoryStream created from the sample data, when a stream is // sent into the encoder an end mark is always written using (MemoryStream source = new MemoryStream(s_sampledata)) { using (MemoryStream dest = new MemoryStream()) { // Compress the data into the destination memory stream instance LzmaEncoder encoder = new LzmaEncoder(); encoder.WriteEndMark = true; encoder.Encode(source, dest); // The compressed data should be smaller than the source data Assert.IsTrue(dest.Length < source.Length); source.SetLength(0); // Clear the source stream dest.Position = 0; // Reset the destination stream // Decompress the data back into the source memory stream using (LzmaReader decompressor = new LzmaReader(dest, true)) decompressor.CopyTo(source); // Ensure that the original data has been restored Assert.AreEqual(source.Length, s_sampledata.Length); Assert.IsTrue(s_sampledata.SequenceEqual(source.ToArray())); } } }
public void Lzma_CompressDecompressNoEndMark() { using (MemoryStream dest = new MemoryStream()) { // Compress the data into the destination memory stream instance using // the raw sample data array and without an end mark. Since the length // of the input data is known, the encoder will not enforce the end mark LzmaEncoder encoder = new LzmaEncoder(); encoder.WriteEndMark = false; encoder.Encode(s_sampledata, dest); // The compressed data should be smaller than the source data Assert.IsTrue(dest.Length < s_sampledata.Length); // Decompress the data back into a new memory stream using (MemoryStream uncompressed = new MemoryStream()) { dest.Position = 0; using (LzmaReader decompressor = new LzmaReader(dest, true)) decompressor.CopyTo(uncompressed); // Ensure that the original data has been restored Assert.AreEqual(uncompressed.Length, s_sampledata.Length); Assert.IsTrue(s_sampledata.SequenceEqual(uncompressed.ToArray())); } } }
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(); }
internal static void WriteLzmaProperties(LzmaEncoder encoder) { #region LZMA properties definition CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { _lzmaDictionarySize, 2, 3, 0, 2, 256, "bt4", false }; #endregion encoder.SetCoderProperties(propIDs, properties); }
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); }
public void Lzma_Dispose() { byte[] buffer = new byte[8192]; // 8KiB data buffer // Create a memorystream to hold the compressed sample data using (MemoryStream ms = new MemoryStream()) { LzmaEncoder encoder = new LzmaEncoder(); encoder.Encode(s_sampledata, ms); // Create a decompression stream and immediately dispose of it LzmaReader stream = new LzmaReader(ms); stream.Dispose(); // Test double dispose stream.Dispose(); // All properties and methods should throw an ObjectDisposedException try { var bs = stream.BaseStream; Assert.Fail("Property access should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { var b = stream.CanRead; Assert.Fail("Property access should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { var b = stream.CanSeek; Assert.Fail("Property access should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { var b = stream.CanWrite; Assert.Fail("Property access should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { stream.Flush(); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { var l = stream.Length; Assert.Fail("Property access should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { var l = stream.Position; Assert.Fail("Property access should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { stream.Position = 12345L; Assert.Fail("Property access should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { stream.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { stream.Seek(0, SeekOrigin.Current); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { stream.SetLength(12345L); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } try { stream.Write(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); } } }
public void Lzma_Write() { LzmaEncoder encoder = new LzmaEncoder(); var compressed = encoder.Encode(s_sampledata); using (LzmaReader reader = new LzmaReader(new MemoryStream(compressed))) { byte[] buffer = new byte[2048]; try { reader.Write(buffer, 0, buffer.Length); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); } } }
public void Lzma_BaseStream() { using (MemoryStream dest = new MemoryStream()) { LzmaEncoder encoder = new LzmaEncoder(); encoder.Encode(s_sampledata, dest); using (LzmaReader stream = new LzmaReader(dest)) { Assert.IsNotNull(stream.BaseStream); Assert.AreSame(dest, stream.BaseStream); } } }
public void Lzma_Read() { byte[] buffer = new byte[8192]; // 8KiB data buffer using (MemoryStream compressed = new MemoryStream()) { LzmaEncoder encoder = new LzmaEncoder(); encoder.Encode(s_sampledata, compressed); compressed.Flush(); // Check the constructor for ArgumentNullException while we're here try { using (LzmaReader decompressor = new LzmaReader(null, false)) { }; Assert.Fail("Constructor should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } // Create a decompressor to test some of the error cases using (LzmaReader decompressor = new LzmaReader(compressed, true)) { // Send in some bum arguments to Read() to check they are caught try { decompressor.Read(null, 0, 0); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { decompressor.Read(buffer, -1, 0); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); } try { decompressor.Read(buffer, 0, -1); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); } try { decompressor.Read(buffer, 0, buffer.Length + 1024); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentException)); } // Attempting to read from the end of the compressed stream should throw an InvalidDataException try { decompressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidDataException)); } } // Create a new decompressor against the same stream and make sure it doesn't throw compressed.Position = 0; using (LzmaReader decompressor = new LzmaReader(compressed, true)) { // Reading zero bytes should not throw an exception decompressor.Read(buffer, 0, 0); while (decompressor.Read(buffer, 0, 8192) != 0) { } } } }
public void Lzma_CompressExternal() { // This method generates an output file that can be tested externally; "thethreemusketeers.txt" is // set to Copy Always to the output directory, it can be diffed after running the external tool LzmaEncoder encoder = new LzmaEncoder(); // Don't use an endmark with this test, the commandline xz-utils version of "lzma" cannot decompress // files with an endmark from what I can gather. Using 7-zip is recommended instead to test, it can // open both flavors of LZMA and decompress them successfully encoder.WriteEndMark = false; using (var outfile = File.Create(Path.Combine(Environment.CurrentDirectory, "thethreemusketeers.lzma"))) { encoder.Encode(s_sampledata, outfile); outfile.Flush(); } }
/// <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); }
public void Lzma_Position() { // Start with a MemoryStream created from the sample data using (MemoryStream compressed = new MemoryStream(s_sampledata)) { LzmaEncoder encoder = new LzmaEncoder(); encoder.Encode(s_sampledata, compressed); compressed.Position = 0; using (LzmaReader reader = new LzmaReader(compressed)) { // Attempting to set the position on the stream should throw try { reader.Position = 12345L; Assert.Fail("Property should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); } // Read some data from the stream, position should be non-zero byte[] buffer = new byte[8192]; reader.Read(buffer, 0, 8192); Assert.AreNotEqual(0L, reader.Position); } } }
public void Lzma_Encoder() { LzmaEncoder encoder = new LzmaEncoder(); // Check the default values Assert.AreEqual(LzmaCompressionLevel.Default, encoder.CompressionLevel); Assert.AreEqual(LzmaCompressionMode.Default, encoder.CompressionMode); Assert.AreEqual(LzmaDictionarySize.Default, encoder.DictionarySize); Assert.AreEqual(LzmaFastBytes.Default, encoder.FastBytes); Assert.AreEqual(LzmaHashBytes.Default, encoder.HashBytes); Assert.AreEqual(LzmaLiteralContextBits.Default, encoder.LiteralContextBits); Assert.AreEqual(LzmaLiteralPositionBits.Default, encoder.LiteralPositionBits); Assert.AreEqual(LzmaMatchFindMode.Default, encoder.MatchFindMode); Assert.AreEqual(LzmaMatchFindPasses.Default, encoder.MatchFindPasses); Assert.AreEqual(LzmaPositionBits.Default, encoder.PositionBits); Assert.AreEqual(true, encoder.UseMultipleThreads); Assert.AreEqual(true, encoder.WriteEndMark); // Set and reset encoder parameters to exercise the property setters encoder.CompressionLevel = LzmaCompressionLevel.Fastest; Assert.AreEqual(LzmaCompressionLevel.Fastest, encoder.CompressionLevel); encoder.CompressionLevel = LzmaCompressionLevel.Default; Assert.AreEqual(LzmaCompressionLevel.Default, encoder.CompressionLevel); encoder.CompressionMode = LzmaCompressionMode.Fast; Assert.AreEqual(LzmaCompressionMode.Fast, encoder.CompressionMode); encoder.CompressionMode = LzmaCompressionMode.Default; Assert.AreEqual(LzmaCompressionMode.Default, encoder.CompressionMode); encoder.DictionarySize = LzmaDictionarySize.Maximum; Assert.AreEqual(LzmaDictionarySize.Maximum, encoder.DictionarySize); encoder.DictionarySize = LzmaDictionarySize.Default; Assert.AreEqual(LzmaDictionarySize.Default, encoder.DictionarySize); encoder.FastBytes = LzmaFastBytes.Maximum; Assert.AreEqual(LzmaFastBytes.Maximum, encoder.FastBytes); encoder.FastBytes = LzmaFastBytes.Default; Assert.AreEqual(LzmaFastBytes.Default, encoder.FastBytes); encoder.HashBytes = LzmaHashBytes.Maximum; Assert.AreEqual(LzmaHashBytes.Maximum, encoder.HashBytes); encoder.HashBytes = LzmaHashBytes.Default; Assert.AreEqual(LzmaHashBytes.Default, encoder.HashBytes); encoder.LiteralContextBits = LzmaLiteralContextBits.Maximum; Assert.AreEqual(LzmaLiteralContextBits.Maximum, encoder.LiteralContextBits); encoder.LiteralContextBits = LzmaLiteralContextBits.Default; Assert.AreEqual(LzmaLiteralContextBits.Default, encoder.LiteralContextBits); encoder.LiteralPositionBits = LzmaLiteralPositionBits.Maximum; Assert.AreEqual(LzmaLiteralPositionBits.Maximum, encoder.LiteralPositionBits); encoder.LiteralPositionBits = LzmaLiteralPositionBits.Default; Assert.AreEqual(LzmaLiteralPositionBits.Default, encoder.LiteralPositionBits); encoder.MatchFindMode = LzmaMatchFindMode.HashChain; Assert.AreEqual(LzmaMatchFindMode.HashChain, encoder.MatchFindMode); encoder.MatchFindMode = LzmaMatchFindMode.Default; Assert.AreEqual(LzmaMatchFindMode.Default, encoder.MatchFindMode); encoder.MatchFindPasses = LzmaMatchFindPasses.Maximum; Assert.AreEqual(LzmaMatchFindPasses.Maximum, encoder.MatchFindPasses); encoder.MatchFindPasses = LzmaMatchFindPasses.Default; Assert.AreEqual(LzmaMatchFindPasses.Default, encoder.MatchFindPasses); encoder.PositionBits = LzmaPositionBits.Maximum; Assert.AreEqual(LzmaPositionBits.Maximum, encoder.PositionBits); encoder.PositionBits = LzmaPositionBits.Default; Assert.AreEqual(LzmaPositionBits.Default, encoder.PositionBits); encoder.UseMultipleThreads = false; Assert.AreEqual(false, encoder.UseMultipleThreads); encoder.UseMultipleThreads = true; Assert.AreEqual(true, encoder.UseMultipleThreads); encoder.WriteEndMark = false; Assert.AreEqual(false, encoder.WriteEndMark); encoder.WriteEndMark = true; Assert.AreEqual(true, encoder.WriteEndMark); // Check all of the Encoder methods work and encode as expected byte[] expected, expectedstreamed, actual; // There are 2 expected results since when the length of the input // is known it's encoded into the compressed stream using (MemoryStream ms = new MemoryStream()) { encoder.Encode(s_sampledata, ms); ms.Flush(); expected = ms.ToArray(); } using (MemoryStream ms = new MemoryStream()) { encoder.Encode(new MemoryStream(s_sampledata), ms); ms.Flush(); expectedstreamed = ms.ToArray(); } // Check parameter validations try { actual = encoder.Encode((byte[])null); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { actual = encoder.Encode((Stream)null); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { actual = encoder.Encode(null, 0, 0); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { encoder.Encode(s_sampledata, null); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { encoder.Encode((byte[])null, new MemoryStream()); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { encoder.Encode((byte[])null, 0, 0, new MemoryStream()); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { encoder.Encode(s_sampledata, 0, s_sampledata.Length, (Stream)null); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { encoder.Encode((Stream)null, new MemoryStream()); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } try { encoder.Encode(new MemoryStream(), (Stream)null); Assert.Fail("Method call should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); } // Check actual encoding operations actual = encoder.Encode(s_sampledata); Assert.IsTrue(Enumerable.SequenceEqual(expected, actual)); // length is known actual = encoder.Encode(new MemoryStream(s_sampledata)); Assert.IsTrue(Enumerable.SequenceEqual(expectedstreamed, actual)); // length is not known actual = encoder.Encode(s_sampledata, 0, s_sampledata.Length); // length is known Assert.IsTrue(Enumerable.SequenceEqual(expected, actual)); using (MemoryStream dest = new MemoryStream()) { encoder.Encode(s_sampledata, dest); Assert.IsTrue(Enumerable.SequenceEqual(expected, dest.ToArray())); // length is known } using (MemoryStream dest = new MemoryStream()) { encoder.Encode(new MemoryStream(s_sampledata), dest); Assert.IsTrue(Enumerable.SequenceEqual(expectedstreamed, dest.ToArray())); // length is not known } using (MemoryStream dest = new MemoryStream()) { encoder.Encode(s_sampledata, 0, s_sampledata.Length, dest); Assert.IsTrue(Enumerable.SequenceEqual(expected, dest.ToArray())); // length is known } }