/// <summary> /// Constructor which creates a new <see cref="ZLibStream" /> object and initializes it as a compression or a decompression one depending on the <paramref name="dir"/> parameter. /// </summary> /// <param name="stream">The stream containing compressed data for decompression or the stream to store compressed data for compression.</param> /// <param name="dir">One of the <see cref="CompressionDirection" /> values that indicates the action to take (compression or decompression).</param> /// <param name="leaveOpen">Whether we need to leave the underlying stream open when <see cref="ZLibStream.Close">closing</see> the current stream.</param> public ZLibStream(Stream stream, CompressionDirection dir, bool leaveOpen) { if (stream == null) { throw new ArgumentNullException("Stream to decompression cannot be null", "stream"); } this.compressionDirection = dir; this._stream = stream; this.leaveOpen = leaveOpen; if (dir == CompressionDirection.Compression) { if (!this._stream.CanWrite) { throw new ArgumentException("The stream is not writable", "stream"); } this.compressionStream = new ZOutputStream(this._stream, this.compLevel); } else { if (!this._stream.CanRead) { throw new ArgumentException("The stream is not readable", "stream"); } this.decompressionStream = new ZInputStream(this._stream); } }
internal static void DecompressData(byte[] inData, out byte[] outData) { try { using (Stream inMemoryStream = new MemoryStream(inData)) using (ZInputStream outZStream = new ZInputStream(inMemoryStream)) { MemoryStream outMemoryStream = new MemoryStream(); CopyStream(outZStream, outMemoryStream); outData = outMemoryStream.ToArray(); } } catch { outData = new byte[0]; } }