private void WriteData(Stream stream)
        {
            if (_originalStream != null)
            {
                WriteOriginalData(stream);
                return;
            }
            bool closeFileStream = false;
            if (_fileStream == null)
            {
                _fileStream = File.OpenRead(_fileName);
                closeFileStream = true;
            }
            long startPosition = stream.Position;
            _uncompressedSize = 0;
            _compressedSize = 0;
            _crc32 = Crc32Hash.DefaultSeed;
            Stream outStream = stream;
            if (CompressionMethod == (uint) CompressionType.Deflate)
            {
                outStream = new DeflateStream(stream, CompressionMode.Compress, CompressionLevel.BestCompression, true);
            }

            int bytesRead;
            var buffer = new byte[WriteBufferSize];
            do
            {
                bytesRead = _fileStream.Read(buffer, 0, WriteBufferSize);
                if (bytesRead <= 0) break;
                _uncompressedSize += (uint) bytesRead;
                outStream.Write(buffer, 0, bytesRead);
                _crc32 = Crc32Hash.CalculateHash(_crc32, buffer, bytesRead);
            } while (bytesRead == WriteBufferSize);
            _crc32 ^= Crc32Hash.DefaultSeed;
            outStream.Flush();

            if (CompressionMethod == (uint) CompressionType.Deflate)
            {
                outStream.Close();
            }
            _compressedSize = (uint) (stream.Position - startPosition);
            if (closeFileStream)
            {
                _fileStream.Close();
                _fileStream = null;
            }
        }
        /// <summary>
        ///   Uncompress a DEFLATE'd byte array into a single string.
        /// </summary>
        ///
        /// <seealso cref="DeflateStream.CompressString(String)">DeflateStream.CompressString(String)</seealso>
        /// <seealso cref="DeflateStream.UncompressBuffer(byte[])">DeflateStream.UncompressBuffer(byte[])</seealso>
        /// <seealso cref="GZipStream.UncompressString(byte[])">GZipStream.UncompressString(byte[])</seealso>
        /// <seealso cref="ZlibStream.UncompressString(byte[])">ZlibStream.UncompressString(byte[])</seealso>
        ///
        /// <param name="compressed">
        ///   A buffer containing DEFLATE-compressed data.
        /// </param>
        ///
        /// <returns>The uncompressed string</returns>
        public static String UncompressString(byte[] compressed)
        {
            using (var input = new System.IO.MemoryStream(compressed))
            {
                System.IO.Stream decompressor =
                    new DeflateStream(input, CompressionMode.Decompress);

                return ZlibBaseStream.UncompressString(compressed, decompressor);
            }
        }
        internal void Extract(Stream stream)
        {
            if (_originalStream == null)
                throw new Exception(string.Format("{0} entry has not been written to archive.", Name));
            ReadLocalHeader();
            if (_localUncompressedSize == 0) return;
            long streamPos = _originalStream.Position;
            _originalStream.Position = _dataOffset;
            Stream readStream = _originalStream;
            if (CompressionMethod != 0)
            {
                readStream = new DeflateStream(_originalStream, CompressionMode.Decompress, true);
            }
            var buffer = new byte[WriteBufferSize];
            long size = _localUncompressedSize;
            while (size > 0)
            {
                var length = (int) Math.Min(size, buffer.Length);
                int bytesRead = readStream.Read(buffer, 0, length);
                if (bytesRead <= 0) break;
                size -= bytesRead;
                stream.Write(buffer, 0, bytesRead);
            }
            stream.Flush();

            if (CompressionMethod != 0)
            {
                readStream.Dispose();
            }
            _originalStream.Position = streamPos;
        }
 /// <summary>
 ///   Compress a string into a byte array using DEFLATE (RFC 1951).
 /// </summary>
 ///
 /// <remarks>
 ///   Uncompress it with <see cref="DeflateStream.UncompressString(byte[])"/>.
 /// </remarks>
 ///
 /// <seealso cref="DeflateStream.UncompressString(byte[])">DeflateStream.UncompressString(byte[])</seealso>
 /// <seealso cref="DeflateStream.CompressBuffer(byte[])">DeflateStream.CompressBuffer(byte[])</seealso>
 /// <seealso cref="GZipStream.CompressString(string)">GZipStream.CompressString(string)</seealso>
 /// <seealso cref="ZlibStream.CompressString(string)">ZlibStream.CompressString(string)</seealso>
 ///
 /// <param name="s">
 ///   A string to compress. The string will first be encoded
 ///   using UTF8, then compressed.
 /// </param>
 ///
 /// <returns>The string in compressed form</returns>
 public static byte[] CompressString(String s)
 {
     using (var ms = new System.IO.MemoryStream())
     {
         System.IO.Stream compressor =
             new DeflateStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
         ZlibBaseStream.CompressString(s, compressor);
         return ms.ToArray();
     }
 }