public void Gzip_CompressDecompress() { // Start with a MemoryStream created from the sample data using (MemoryStream source = new MemoryStream(s_sampledata)) { using (MemoryStream dest = new MemoryStream()) { // Compress the data into the destination memory stream instance using (GzipWriter compressor = new GzipWriter(dest, CompressionLevel.Optimal, true)) source.CopyTo(compressor); // 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 (GzipReader decompressor = new GzipReader(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 Gzip_DecompressExternal() { // Decompress a stream created externally to this library using (GzipReader reader = new GzipReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("zuki.io.compression.test.thethreemusketeers.gz"))) { 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())); } } }
public void Gzip_Position() { // Start with a MemoryStream created from the sample data using (MemoryStream source = new MemoryStream(s_sampledata)) { using (MemoryStream dest = new MemoryStream()) { // Test a compression stream using (GzipWriter compressor = new GzipWriter(dest, CompressionLevel.Optimal, true)) { // The stream should report position zero prior to compression Assert.AreEqual(0L, compressor.Position); source.CopyTo(compressor); // The stream should report non-zero after compression Assert.AreNotEqual(0L, compressor.Position); // Attempting to set the position on the stream should throw try { compressor.Position = 12345L; Assert.Fail("Property should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); } } source.SetLength(0); // Clear the source stream dest.Position = 0; // Reset the destination stream // Test a decompression stream using (GzipReader decompressor = new GzipReader(dest, true)) { // The stream should report position zero prior to compression Assert.AreEqual(0L, decompressor.Position); decompressor.CopyTo(source); // The stream should report non-zero after compression Assert.AreNotEqual(0L, decompressor.Position); // Attempting to set the position on the stream should throw try { decompressor.Position = 12345L; Assert.Fail("Property should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); } } } } }