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