public void Memory_Compress_Decompress() { var input = (byte[])PlainBytes.Clone(); byte[] compressed; using (var outStream = new MemoryStream(input.Length)) { using (var gzip = new GZipStream(outStream, CompressionMode.Compress)) { gzip.Write(input, 0, input.Length); } compressed = outStream.ToArray(); Assert.IsNotNull(compressed); Assert.IsTrue(compressed.Length > 0); } using (var outStream = new MemoryStream(input.Length)) using (var gzip = new GZipStream(new MemoryStream(compressed), CompressionMode.Decompress)) { var readCount = 0; var buffer = new byte[CompressorTool.BUFFER_SIZE]; while ((readCount = gzip.Read(buffer, 0, buffer.Length)) > 0) { outStream.Write(buffer, 0, readCount); } byte[] decompressed = outStream.ToArray(); Assert.IsTrue(decompressed.Length > 0); Assert.AreEqual(PlainBytes, decompressed); } }
public void Memory_Compress_Decompress_Async() { var input = (byte[])PlainBytes.Clone(); byte[] compressed; using (var outStream = new MemoryStream(input.Length)) { using (var gzip = new GZipStream(outStream, CompressionMode.Compress)) { gzip.WriteAsync(input, 0, input.Length).Wait(); } compressed = outStream.ToArray(); Assert.IsNotNull(compressed); Assert.IsTrue(compressed.Length > 0); } using (var gzip = new GZipStream(new MemoryStream(compressed), CompressionMode.Decompress)) { var decompressed = With.TryFunctionAsync(() => gzip.ReadAllBytesAsync().Result); Assert.IsTrue(decompressed.Length > 0); Assert.AreEqual(PlainBytes, decompressed); } }