Esempio n. 1
0
        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()));
                }
            }
        }
Esempio n. 2
0
        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()));
                }
            }
        }
Esempio n. 3
0
        public void Lzma_DecompressExternal()
        {
            // Decompress a stream created externally to this library
            using (LzmaReader reader = new LzmaReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("zuki.io.compression.test.thethreemusketeers.lzma")))
            {
                using (MemoryStream dest = new MemoryStream())
                {
                    reader.CopyTo(dest);
                    dest.Flush();

                    // Verify that the output matches the sample data byte-for-byte
                    Assert.IsTrue(Enumerable.SequenceEqual(s_sampledata, dest.ToArray()));
                }
            }
        }