Represents a Zlib stream for compression or decompression.

The ZlibStream is a Decorator on a . It adds ZLIB compression or decompression to any stream.

Using this stream, applications can compress or decompress data via stream Read and Write operations. Either compresssion or decompression can occur through either reading or writing. The compression format used is ZLIB, which is documented in IETF RFC 1950, "ZLIB Compressed Data Format Specification version 3.3". This implementation of ZLIB always uses DEFLATE as the compression method. (see IETF RFC 1951, "DEFLATE Compressed Data Format Specification version 1.3.")

The ZLIB format allows for varying compression methods, window sizes, and dictionaries. This implementation always uses the DEFLATE compression method, a preset dictionary, and 15 window bits by default.

This class is similar to DeflateStream, except that it adds the RFC1950 header and trailer bytes to a compressed stream when compressing, or expects the RFC1950 header and trailer bytes when decompressing. It is also similar to the GZipStream.

Inheritance: System.IO.Stream
Esempio n. 1
0
 /// <summary>
 /// Uncompress a byte array into a byte array.
 /// </summary>
 /// <seealso cref="ZlibStream.CompressBuffer(byte[])"/>
 /// <seealso cref="ZlibStream.UncompressString(byte[])"/>
 /// <param name="compressed">
 /// A buffer containing ZLIB-compressed data.
 /// </param>
 public static byte[] UncompressBuffer(byte[] compressed)
 {
     // workitem 8460
     byte[] working = new byte[1024];
     using (var output = new MemoryStream())
     {
         using (var input = new MemoryStream(compressed))
         {
             using (Stream decompressor = new ZlibStream(input))
             {
                 int n;
                 while ((n = decompressor.Read(working, 0, working.Length)) != 0)
                 {
                     output.Write(working, 0, n);
                 }
             }
             return(output.ToArray());
         }
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Uncompress a byte array into a single string.
 /// </summary>
 /// <seealso cref="ZlibStream.CompressString(String)"/>
 /// <param name="compressed">
 /// A buffer containing ZLIB-compressed data.  
 /// </param>
 public static String UncompressString(byte[] compressed)
 {
     // workitem 8460
     byte[] working = new byte[1024];
     var encoding = System.Text.Encoding.UTF8;
     using (var output = new MemoryStream())
     {
         using (var input = new MemoryStream(compressed))
         {
             using (Stream decompressor = new ZlibStream(input))
             {
                 int n;
                 while ((n = decompressor.Read(working, 0, working.Length)) != 0)
                 {
                     output.Write(working, 0, n);
                 }
             }
             // reset to allow read from start
             output.Seek(0, SeekOrigin.Begin);
             var sr = new StreamReader(output, encoding);
             return sr.ReadToEnd();
         }
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Uncompress a byte array into a byte array.
 /// </summary>
 /// <seealso cref="ZlibStream.CompressBuffer(byte[])"/>
 /// <seealso cref="ZlibStream.UncompressString(byte[])"/>
 /// <param name="compressed">
 /// A buffer containing ZLIB-compressed data.  
 /// </param>
 public static byte[] UncompressBuffer(byte[] compressed)
 {
     // workitem 8460
     byte[] working = new byte[1024];
     using (var output = new MemoryStream())
     {
         using (var input = new MemoryStream(compressed))
         {
             using (Stream decompressor = new ZlibStream(input))
             {
                 int n;
                 while ((n = decompressor.Read(working, 0, working.Length)) != 0)
                 {
                     output.Write(working, 0, n);
                 }
             }
             return output.ToArray();
         }
     }
 }