Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Packaging/DotNetZip/Zlib/DeflateStream.cs @ 15945

Last change on this file since 15945 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 30.4 KB
Line 
1// DeflateStream.cs
2// ------------------------------------------------------------------
3//
4// Copyright (c) 2009-2010 Dino Chiesa.
5// All rights reserved.
6//
7// This code module is part of DotNetZip, a zipfile class library.
8//
9// ------------------------------------------------------------------
10//
11// This code is licensed under the Microsoft Public License.
12// See the file License.txt for the license details.
13// More info on: http://dotnetzip.codeplex.com
14//
15// ------------------------------------------------------------------
16//
17// last saved (in emacs):
18// Time-stamp: <2011-July-31 14:48:11>
19//
20// ------------------------------------------------------------------
21//
22// This module defines the DeflateStream class, which can be used as a replacement for
23// the System.IO.Compression.DeflateStream class in the .NET BCL.
24//
25// ------------------------------------------------------------------
26
27
28using System;
29
30namespace OfficeOpenXml.Packaging.Ionic.Zlib
31{
32    /// <summary>
33    /// A class for compressing and decompressing streams using the Deflate algorithm.
34    /// </summary>
35    ///
36    /// <remarks>
37    ///
38    /// <para>
39    ///   The DeflateStream is a <see
40    ///   href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</see> on a <see
41    ///   cref="System.IO.Stream"/>.  It adds DEFLATE compression or decompression to any
42    ///   stream.
43    /// </para>
44    ///
45    /// <para>
46    ///   Using this stream, applications can compress or decompress data via stream
47    ///   <c>Read</c> and <c>Write</c> operations.  Either compresssion or decompression
48    ///   can occur through either reading or writing. The compression format used is
49    ///   DEFLATE, which is documented in <see
50    ///   href="http://www.ietf.org/rfc/rfc1951.txt">IETF RFC 1951</see>, "DEFLATE
51    ///   Compressed Data Format Specification version 1.3.".
52    /// </para>
53    ///
54    /// <para>
55    ///   This class is similar to <see cref="ZlibStream"/>, except that
56    ///   <c>ZlibStream</c> adds the <see href="http://www.ietf.org/rfc/rfc1950.txt">RFC
57    ///   1950 - ZLIB</see> framing bytes to a compressed stream when compressing, or
58    ///   expects the RFC1950 framing bytes when decompressing. The <c>DeflateStream</c>
59    ///   does not.
60    /// </para>
61    ///
62    /// </remarks>
63    ///
64    /// <seealso cref="ZlibStream" />
65    /// <seealso cref="GZipStream" />
66    public class DeflateStream : System.IO.Stream
67    {
68        internal ZlibBaseStream _baseStream;
69        internal System.IO.Stream _innerStream;
70        bool _disposed;
71
72        /// <summary>
73        ///   Create a DeflateStream using the specified CompressionMode.
74        /// </summary>
75        ///
76        /// <remarks>
77        ///   When mode is <c>CompressionMode.Compress</c>, the DeflateStream will use
78        ///   the default compression level. The "captive" stream will be closed when
79        ///   the DeflateStream is closed.
80        /// </remarks>
81        ///
82        /// <example>
83        /// This example uses a DeflateStream to compress data from a file, and writes
84        /// the compressed data to another file.
85        /// <code>
86        /// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
87        /// {
88        ///     using (var raw = System.IO.File.Create(fileToCompress + ".deflated"))
89        ///     {
90        ///         using (Stream compressor = new DeflateStream(raw, CompressionMode.Compress))
91        ///         {
92        ///             byte[] buffer = new byte[WORKING_BUFFER_SIZE];
93        ///             int n;
94        ///             while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
95        ///             {
96        ///                 compressor.Write(buffer, 0, n);
97        ///             }
98        ///         }
99        ///     }
100        /// }
101        /// </code>
102        ///
103        /// <code lang="VB">
104        /// Using input As Stream = File.OpenRead(fileToCompress)
105        ///     Using raw As FileStream = File.Create(fileToCompress &amp; ".deflated")
106        ///         Using compressor As Stream = New DeflateStream(raw, CompressionMode.Compress)
107        ///             Dim buffer As Byte() = New Byte(4096) {}
108        ///             Dim n As Integer = -1
109        ///             Do While (n &lt;&gt; 0)
110        ///                 If (n &gt; 0) Then
111        ///                     compressor.Write(buffer, 0, n)
112        ///                 End If
113        ///                 n = input.Read(buffer, 0, buffer.Length)
114        ///             Loop
115        ///         End Using
116        ///     End Using
117        /// End Using
118        /// </code>
119        /// </example>
120        /// <param name="stream">The stream which will be read or written.</param>
121        /// <param name="mode">Indicates whether the DeflateStream will compress or decompress.</param>
122        public DeflateStream(System.IO.Stream stream, CompressionMode mode)
123            : this(stream, mode, CompressionLevel.Default, false)
124        {
125        }
126
127        /// <summary>
128        /// Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel.
129        /// </summary>
130        ///
131        /// <remarks>
132        ///
133        /// <para>
134        ///   When mode is <c>CompressionMode.Decompress</c>, the level parameter is
135        ///   ignored.  The "captive" stream will be closed when the DeflateStream is
136        ///   closed.
137        /// </para>
138        ///
139        /// </remarks>
140        ///
141        /// <example>
142        ///
143        ///   This example uses a DeflateStream to compress data from a file, and writes
144        ///   the compressed data to another file.
145        ///
146        /// <code>
147        /// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
148        /// {
149        ///     using (var raw = System.IO.File.Create(fileToCompress + ".deflated"))
150        ///     {
151        ///         using (Stream compressor = new DeflateStream(raw,
152        ///                                                      CompressionMode.Compress,
153        ///                                                      CompressionLevel.BestCompression))
154        ///         {
155        ///             byte[] buffer = new byte[WORKING_BUFFER_SIZE];
156        ///             int n= -1;
157        ///             while (n != 0)
158        ///             {
159        ///                 if (n &gt; 0)
160        ///                     compressor.Write(buffer, 0, n);
161        ///                 n= input.Read(buffer, 0, buffer.Length);
162        ///             }
163        ///         }
164        ///     }
165        /// }
166        /// </code>
167        ///
168        /// <code lang="VB">
169        /// Using input As Stream = File.OpenRead(fileToCompress)
170        ///     Using raw As FileStream = File.Create(fileToCompress &amp; ".deflated")
171        ///         Using compressor As Stream = New DeflateStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression)
172        ///             Dim buffer As Byte() = New Byte(4096) {}
173        ///             Dim n As Integer = -1
174        ///             Do While (n &lt;&gt; 0)
175        ///                 If (n &gt; 0) Then
176        ///                     compressor.Write(buffer, 0, n)
177        ///                 End If
178        ///                 n = input.Read(buffer, 0, buffer.Length)
179        ///             Loop
180        ///         End Using
181        ///     End Using
182        /// End Using
183        /// </code>
184        /// </example>
185        /// <param name="stream">The stream to be read or written while deflating or inflating.</param>
186        /// <param name="mode">Indicates whether the <c>DeflateStream</c> will compress or decompress.</param>
187        /// <param name="level">A tuning knob to trade speed for effectiveness.</param>
188        public DeflateStream(System.IO.Stream stream, CompressionMode mode, CompressionLevel level)
189            : this(stream, mode, level, false)
190        {
191        }
192
193        /// <summary>
194        ///   Create a <c>DeflateStream</c> using the specified
195        ///   <c>CompressionMode</c>, and explicitly specify whether the
196        ///   stream should be left open after Deflation or Inflation.
197        /// </summary>
198        ///
199        /// <remarks>
200        ///
201        /// <para>
202        ///   This constructor allows the application to request that the captive stream
203        ///   remain open after the deflation or inflation occurs.  By default, after
204        ///   <c>Close()</c> is called on the stream, the captive stream is also
205        ///   closed. In some cases this is not desired, for example if the stream is a
206        ///   memory stream that will be re-read after compression.  Specify true for
207        ///   the <paramref name="leaveOpen"/> parameter to leave the stream open.
208        /// </para>
209        ///
210        /// <para>
211        ///   The <c>DeflateStream</c> will use the default compression level.
212        /// </para>
213        ///
214        /// <para>
215        ///   See the other overloads of this constructor for example code.
216        /// </para>
217        /// </remarks>
218        ///
219        /// <param name="stream">
220        ///   The stream which will be read or written. This is called the
221        ///   "captive" stream in other places in this documentation.
222        /// </param>
223        ///
224        /// <param name="mode">
225        ///   Indicates whether the <c>DeflateStream</c> will compress or decompress.
226        /// </param>
227        ///
228        /// <param name="leaveOpen">true if the application would like the stream to
229        /// remain open after inflation/deflation.</param>
230        public DeflateStream(System.IO.Stream stream, CompressionMode mode, bool leaveOpen)
231            : this(stream, mode, CompressionLevel.Default, leaveOpen)
232        {
233        }
234
235        /// <summary>
236        ///   Create a <c>DeflateStream</c> using the specified <c>CompressionMode</c>
237        ///   and the specified <c>CompressionLevel</c>, and explicitly specify whether
238        ///   the stream should be left open after Deflation or Inflation.
239        /// </summary>
240        ///
241        /// <remarks>
242        ///
243        /// <para>
244        ///   When mode is <c>CompressionMode.Decompress</c>, the level parameter is ignored.
245        /// </para>
246        ///
247        /// <para>
248        ///   This constructor allows the application to request that the captive stream
249        ///   remain open after the deflation or inflation occurs.  By default, after
250        ///   <c>Close()</c> is called on the stream, the captive stream is also
251        ///   closed. In some cases this is not desired, for example if the stream is a
252        ///   <see cref="System.IO.MemoryStream"/> that will be re-read after
253        ///   compression.  Specify true for the <paramref name="leaveOpen"/> parameter
254        ///   to leave the stream open.
255        /// </para>
256        ///
257        /// </remarks>
258        ///
259        /// <example>
260        ///
261        ///   This example shows how to use a <c>DeflateStream</c> to compress data from
262        ///   a file, and store the compressed data into another file.
263        ///
264        /// <code>
265        /// using (var output = System.IO.File.Create(fileToCompress + ".deflated"))
266        /// {
267        ///     using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
268        ///     {
269        ///         using (Stream compressor = new DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, true))
270        ///         {
271        ///             byte[] buffer = new byte[WORKING_BUFFER_SIZE];
272        ///             int n= -1;
273        ///             while (n != 0)
274        ///             {
275        ///                 if (n &gt; 0)
276        ///                     compressor.Write(buffer, 0, n);
277        ///                 n= input.Read(buffer, 0, buffer.Length);
278        ///             }
279        ///         }
280        ///     }
281        ///     // can write additional data to the output stream here
282        /// }
283        /// </code>
284        ///
285        /// <code lang="VB">
286        /// Using output As FileStream = File.Create(fileToCompress &amp; ".deflated")
287        ///     Using input As Stream = File.OpenRead(fileToCompress)
288        ///         Using compressor As Stream = New DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, True)
289        ///             Dim buffer As Byte() = New Byte(4096) {}
290        ///             Dim n As Integer = -1
291        ///             Do While (n &lt;&gt; 0)
292        ///                 If (n &gt; 0) Then
293        ///                     compressor.Write(buffer, 0, n)
294        ///                 End If
295        ///                 n = input.Read(buffer, 0, buffer.Length)
296        ///             Loop
297        ///         End Using
298        ///     End Using
299        ///     ' can write additional data to the output stream here.
300        /// End Using
301        /// </code>
302        /// </example>
303        /// <param name="stream">The stream which will be read or written.</param>
304        /// <param name="mode">Indicates whether the DeflateStream will compress or decompress.</param>
305        /// <param name="leaveOpen">true if the application would like the stream to remain open after inflation/deflation.</param>
306        /// <param name="level">A tuning knob to trade speed for effectiveness.</param>
307        public DeflateStream(System.IO.Stream stream, CompressionMode mode, CompressionLevel level, bool leaveOpen)
308        {
309            _innerStream = stream;
310            _baseStream = new ZlibBaseStream(stream, mode, level, ZlibStreamFlavor.DEFLATE, leaveOpen);
311        }
312
313        #region Zlib properties
314
315        /// <summary>
316        /// This property sets the flush behavior on the stream.
317        /// </summary>
318        /// <remarks> See the ZLIB documentation for the meaning of the flush behavior.
319        /// </remarks>
320        virtual public FlushType FlushMode
321        {
322            get { return (this._baseStream._flushMode); }
323            set
324            {
325                if (_disposed) throw new ObjectDisposedException("DeflateStream");
326                this._baseStream._flushMode = value;
327            }
328        }
329
330        /// <summary>
331        ///   The size of the working buffer for the compression codec.
332        /// </summary>
333        ///
334        /// <remarks>
335        /// <para>
336        ///   The working buffer is used for all stream operations.  The default size is
337        ///   1024 bytes.  The minimum size is 128 bytes. You may get better performance
338        ///   with a larger buffer.  Then again, you might not.  You would have to test
339        ///   it.
340        /// </para>
341        ///
342        /// <para>
343        ///   Set this before the first call to <c>Read()</c> or <c>Write()</c> on the
344        ///   stream. If you try to set it afterwards, it will throw.
345        /// </para>
346        /// </remarks>
347        public int BufferSize
348        {
349            get
350            {
351                return this._baseStream._bufferSize;
352            }
353            set
354            {
355                if (_disposed) throw new ObjectDisposedException("DeflateStream");
356                if (this._baseStream._workingBuffer != null)
357                    throw new ZlibException("The working buffer is already set.");
358                if (value < ZlibConstants.WorkingBufferSizeMin)
359                    throw new ZlibException(String.Format("Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin));
360                this._baseStream._bufferSize = value;
361            }
362        }
363
364        /// <summary>
365        ///   The ZLIB strategy to be used during compression.
366        /// </summary>
367        ///
368        /// <remarks>
369        ///   By tweaking this parameter, you may be able to optimize the compression for
370        ///   data with particular characteristics.
371        /// </remarks>
372        public CompressionStrategy Strategy
373        {
374            get
375            {
376                return this._baseStream.Strategy;
377            }
378            set
379            {
380            if (_disposed) throw new ObjectDisposedException("DeflateStream");
381                this._baseStream.Strategy = value;
382            }
383        }
384
385        /// <summary> Returns the total number of bytes input so far.</summary>
386        virtual public long TotalIn
387        {
388            get
389            {
390                return this._baseStream._z.TotalBytesIn;
391            }
392        }
393
394        /// <summary> Returns the total number of bytes output so far.</summary>
395        virtual public long TotalOut
396        {
397            get
398            {
399                return this._baseStream._z.TotalBytesOut;
400            }
401        }
402
403        #endregion
404
405        #region System.IO.Stream methods
406        /// <summary>
407        ///   Dispose the stream.
408        /// </summary>
409        /// <remarks>
410        ///   <para>
411        ///     This may or may not result in a <c>Close()</c> call on the captive
412        ///     stream.  See the constructors that have a <c>leaveOpen</c> parameter
413        ///     for more information.
414        ///   </para>
415        ///   <para>
416        ///     Application code won't call this code directly.  This method may be
417        ///     invoked in two distinct scenarios.  If disposing == true, the method
418        ///     has been called directly or indirectly by a user's code, for example
419        ///     via the public Dispose() method. In this case, both managed and
420        ///     unmanaged resources can be referenced and disposed.  If disposing ==
421        ///     false, the method has been called by the runtime from inside the
422        ///     object finalizer and this method should not reference other objects;
423        ///     in that case only unmanaged resources must be referenced or
424        ///     disposed.
425        ///   </para>
426        /// </remarks>
427        /// <param name="disposing">
428        ///   true if the Dispose method was invoked by user code.
429        /// </param>
430        protected override void Dispose(bool disposing)
431        {
432            try
433            {
434                if (!_disposed)
435                {
436                    if (disposing && (this._baseStream != null))
437                        this._baseStream.Close();
438                    _disposed = true;
439                }
440            }
441            finally
442            {
443                base.Dispose(disposing);
444            }
445        }
446
447
448
449        /// <summary>
450        /// Indicates whether the stream can be read.
451        /// </summary>
452        /// <remarks>
453        /// The return value depends on whether the captive stream supports reading.
454        /// </remarks>
455        public override bool CanRead
456        {
457            get
458            {
459                if (_disposed) throw new ObjectDisposedException("DeflateStream");
460                return _baseStream._stream.CanRead;
461            }
462        }
463
464        /// <summary>
465        /// Indicates whether the stream supports Seek operations.
466        /// </summary>
467        /// <remarks>
468        /// Always returns false.
469        /// </remarks>
470        public override bool CanSeek
471        {
472            get { return false; }
473        }
474
475
476        /// <summary>
477        /// Indicates whether the stream can be written.
478        /// </summary>
479        /// <remarks>
480        /// The return value depends on whether the captive stream supports writing.
481        /// </remarks>
482        public override bool CanWrite
483        {
484            get
485            {
486                if (_disposed) throw new ObjectDisposedException("DeflateStream");
487                return _baseStream._stream.CanWrite;
488            }
489        }
490
491        /// <summary>
492        /// Flush the stream.
493        /// </summary>
494        public override void Flush()
495        {
496            if (_disposed) throw new ObjectDisposedException("DeflateStream");
497            _baseStream.Flush();
498        }
499
500        /// <summary>
501        /// Reading this property always throws a <see cref="NotImplementedException"/>.
502        /// </summary>
503        public override long Length
504        {
505            get { throw new NotImplementedException(); }
506        }
507
508        /// <summary>
509        /// The position of the stream pointer.
510        /// </summary>
511        ///
512        /// <remarks>
513        ///   Setting this property always throws a <see
514        ///   cref="NotImplementedException"/>. Reading will return the total bytes
515        ///   written out, if used in writing, or the total bytes read in, if used in
516        ///   reading.  The count may refer to compressed bytes or uncompressed bytes,
517        ///   depending on how you've used the stream.
518        /// </remarks>
519        public override long Position
520        {
521            get
522            {
523                if (this._baseStream._streamMode == Ionic.Zlib.ZlibBaseStream.StreamMode.Writer)
524                    return this._baseStream._z.TotalBytesOut;
525                if (this._baseStream._streamMode == Ionic.Zlib.ZlibBaseStream.StreamMode.Reader)
526                    return this._baseStream._z.TotalBytesIn;
527                return 0;
528            }
529            set { throw new NotImplementedException(); }
530        }
531
532        /// <summary>
533        /// Read data from the stream.
534        /// </summary>
535        /// <remarks>
536        ///
537        /// <para>
538        ///   If you wish to use the <c>DeflateStream</c> to compress data while
539        ///   reading, you can create a <c>DeflateStream</c> with
540        ///   <c>CompressionMode.Compress</c>, providing an uncompressed data stream.
541        ///   Then call Read() on that <c>DeflateStream</c>, and the data read will be
542        ///   compressed as you read.  If you wish to use the <c>DeflateStream</c> to
543        ///   decompress data while reading, you can create a <c>DeflateStream</c> with
544        ///   <c>CompressionMode.Decompress</c>, providing a readable compressed data
545        ///   stream.  Then call Read() on that <c>DeflateStream</c>, and the data read
546        ///   will be decompressed as you read.
547        /// </para>
548        ///
549        /// <para>
550        ///   A <c>DeflateStream</c> can be used for <c>Read()</c> or <c>Write()</c>, but not both.
551        /// </para>
552        ///
553        /// </remarks>
554        /// <param name="buffer">The buffer into which the read data should be placed.</param>
555        /// <param name="offset">the offset within that data array to put the first byte read.</param>
556        /// <param name="count">the number of bytes to read.</param>
557        /// <returns>the number of bytes actually read</returns>
558        public override int Read(byte[] buffer, int offset, int count)
559        {
560            if (_disposed) throw new ObjectDisposedException("DeflateStream");
561            return _baseStream.Read(buffer, offset, count);
562        }
563
564
565        /// <summary>
566        /// Calling this method always throws a <see cref="NotImplementedException"/>.
567        /// </summary>
568        /// <param name="offset">this is irrelevant, since it will always throw!</param>
569        /// <param name="origin">this is irrelevant, since it will always throw!</param>
570        /// <returns>irrelevant!</returns>
571        public override long Seek(long offset, System.IO.SeekOrigin origin)
572        {
573            throw new NotImplementedException();
574        }
575
576        /// <summary>
577        /// Calling this method always throws a <see cref="NotImplementedException"/>.
578        /// </summary>
579        /// <param name="value">this is irrelevant, since it will always throw!</param>
580        public override void SetLength(long value)
581        {
582            throw new NotImplementedException();
583        }
584
585        /// <summary>
586        ///   Write data to the stream.
587        /// </summary>
588        /// <remarks>
589        ///
590        /// <para>
591        ///   If you wish to use the <c>DeflateStream</c> to compress data while
592        ///   writing, you can create a <c>DeflateStream</c> with
593        ///   <c>CompressionMode.Compress</c>, and a writable output stream.  Then call
594        ///   <c>Write()</c> on that <c>DeflateStream</c>, providing uncompressed data
595        ///   as input.  The data sent to the output stream will be the compressed form
596        ///   of the data written.  If you wish to use the <c>DeflateStream</c> to
597        ///   decompress data while writing, you can create a <c>DeflateStream</c> with
598        ///   <c>CompressionMode.Decompress</c>, and a writable output stream.  Then
599        ///   call <c>Write()</c> on that stream, providing previously compressed
600        ///   data. The data sent to the output stream will be the decompressed form of
601        ///   the data written.
602        /// </para>
603        ///
604        /// <para>
605        ///   A <c>DeflateStream</c> can be used for <c>Read()</c> or <c>Write()</c>,
606        ///   but not both.
607        /// </para>
608        ///
609        /// </remarks>
610        ///
611        /// <param name="buffer">The buffer holding data to write to the stream.</param>
612        /// <param name="offset">the offset within that data array to find the first byte to write.</param>
613        /// <param name="count">the number of bytes to write.</param>
614        public override void Write(byte[] buffer, int offset, int count)
615        {
616            if (_disposed) throw new ObjectDisposedException("DeflateStream");
617            _baseStream.Write(buffer, offset, count);
618        }
619        #endregion
620
621
622
623
624        /// <summary>
625        ///   Compress a string into a byte array using DEFLATE (RFC 1951).
626        /// </summary>
627        ///
628        /// <remarks>
629        ///   Uncompress it with <see cref="DeflateStream.UncompressString(byte[])"/>.
630        /// </remarks>
631        ///
632        /// <seealso cref="DeflateStream.UncompressString(byte[])">DeflateStream.UncompressString(byte[])</seealso>
633        /// <seealso cref="DeflateStream.CompressBuffer(byte[])">DeflateStream.CompressBuffer(byte[])</seealso>
634        /// <seealso cref="GZipStream.CompressString(string)">GZipStream.CompressString(string)</seealso>
635        /// <seealso cref="ZlibStream.CompressString(string)">ZlibStream.CompressString(string)</seealso>
636        ///
637        /// <param name="s">
638        ///   A string to compress. The string will first be encoded
639        ///   using UTF8, then compressed.
640        /// </param>
641        ///
642        /// <returns>The string in compressed form</returns>
643        public static byte[] CompressString(String s)
644        {
645            using (var ms = new System.IO.MemoryStream())
646            {
647                System.IO.Stream compressor =
648                    new DeflateStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
649                ZlibBaseStream.CompressString(s, compressor);
650                return ms.ToArray();
651            }
652        }
653
654
655        /// <summary>
656        ///   Compress a byte array into a new byte array using DEFLATE.
657        /// </summary>
658        ///
659        /// <remarks>
660        ///   Uncompress it with <see cref="DeflateStream.UncompressBuffer(byte[])"/>.
661        /// </remarks>
662        ///
663        /// <seealso cref="DeflateStream.CompressString(string)">DeflateStream.CompressString(string)</seealso>
664        /// <seealso cref="DeflateStream.UncompressBuffer(byte[])">DeflateStream.UncompressBuffer(byte[])</seealso>
665        /// <seealso cref="GZipStream.CompressBuffer(byte[])">GZipStream.CompressBuffer(byte[])</seealso>
666        /// <seealso cref="ZlibStream.CompressBuffer(byte[])">ZlibStream.CompressBuffer(byte[])</seealso>
667        ///
668        /// <param name="b">
669        ///   A buffer to compress.
670        /// </param>
671        ///
672        /// <returns>The data in compressed form</returns>
673        public static byte[] CompressBuffer(byte[] b)
674        {
675            using (var ms = new System.IO.MemoryStream())
676            {
677                System.IO.Stream compressor =
678                    new DeflateStream( ms, CompressionMode.Compress, CompressionLevel.BestCompression );
679
680                ZlibBaseStream.CompressBuffer(b, compressor);
681                return ms.ToArray();
682            }
683        }
684
685
686        /// <summary>
687        ///   Uncompress a DEFLATE'd byte array into a single string.
688        /// </summary>
689        ///
690        /// <seealso cref="DeflateStream.CompressString(String)">DeflateStream.CompressString(String)</seealso>
691        /// <seealso cref="DeflateStream.UncompressBuffer(byte[])">DeflateStream.UncompressBuffer(byte[])</seealso>
692        /// <seealso cref="GZipStream.UncompressString(byte[])">GZipStream.UncompressString(byte[])</seealso>
693        /// <seealso cref="ZlibStream.UncompressString(byte[])">ZlibStream.UncompressString(byte[])</seealso>
694        ///
695        /// <param name="compressed">
696        ///   A buffer containing DEFLATE-compressed data.
697        /// </param>
698        ///
699        /// <returns>The uncompressed string</returns>
700        public static String UncompressString(byte[] compressed)
701        {
702            using (var input = new System.IO.MemoryStream(compressed))
703            {
704                System.IO.Stream decompressor =
705                    new DeflateStream(input, CompressionMode.Decompress);
706
707                return ZlibBaseStream.UncompressString(compressed, decompressor);
708            }
709        }
710
711
712        /// <summary>
713        ///   Uncompress a DEFLATE'd byte array into a byte array.
714        /// </summary>
715        ///
716        /// <seealso cref="DeflateStream.CompressBuffer(byte[])">DeflateStream.CompressBuffer(byte[])</seealso>
717        /// <seealso cref="DeflateStream.UncompressString(byte[])">DeflateStream.UncompressString(byte[])</seealso>
718        /// <seealso cref="GZipStream.UncompressBuffer(byte[])">GZipStream.UncompressBuffer(byte[])</seealso>
719        /// <seealso cref="ZlibStream.UncompressBuffer(byte[])">ZlibStream.UncompressBuffer(byte[])</seealso>
720        ///
721        /// <param name="compressed">
722        ///   A buffer containing data that has been compressed with DEFLATE.
723        /// </param>
724        ///
725        /// <returns>The data in uncompressed form</returns>
726        public static byte[] UncompressBuffer(byte[] compressed)
727        {
728            using (var input = new System.IO.MemoryStream(compressed))
729            {
730                System.IO.Stream decompressor =
731                    new DeflateStream( input, CompressionMode.Decompress );
732
733                return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
734            }
735        }
736
737    }
738
739}
740
Note: See TracBrowser for help on using the repository browser.