Beispiel #1
0
        public ZStreamWriter(Stream stream, int level, uint bufferSize, int bits = zlib.MAX_WBITS)
        {
            if (!stream.CanWrite)
            {
                throw new ArgumentException("Stream not writable");
            }

            if (bufferSize == 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize", "must be greater zero");
            }

            baseStream = stream;

            zstream = new zlib.z_stream();

            switch (zlib.deflateInit(zstream, level, bits))
            {
            case zlib.Z_OK:
                break;

            case zlib.Z_MEM_ERROR:
            case zlib.Z_STREAM_ERROR:
                throw new Exception("zlib memory error");

            default:
                throw new Exception("Unknown zlib error");
            }

            zstream.out_buf   = new byte[bufferSize];
            zstream.next_out  = 0;
            zstream.avail_out = bufferSize;
        }
Beispiel #2
0
        public ZStreamReader(Stream stream, uint bufferSize, int bits = zlib.DEF_WBITS)
        {
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream not readable.", "stream");
            }

            if (bufferSize == 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize", "must be greater zero");
            }

            baseStream = stream;

            zstream = new zlib.z_stream();

            switch (zlib.inflateInit(zstream))
            {
            case zlib.Z_OK:
                break;

            case zlib.Z_MEM_ERROR:
            case zlib.Z_STREAM_ERROR:
                throw new Exception("zlib memory error");

            default:
                throw new Exception("Unknown zlib error");
            }

            zstream.in_buf   = new byte[bufferSize];
            zstream.next_in  = 0;
            zstream.avail_in = (uint)baseStream.Read(zstream.in_buf, (int)zstream.next_in, (int)bufferSize);
        }
Beispiel #3
0
#pragma warning disable CS0114
        public void Close()
        {
            if (baseStream != null)
            {
                baseStream.Dispose();
            }
            baseStream = null;

            if (zstream != null)
            {
                zlib.inflateEnd(zstream);
            }

            zstream = null;

            base.Dispose();
        }
Beispiel #4
0
        public void WriteToEnd()
        {
            if (baseStream == null)
            {
                throw new ObjectDisposedException(null, "File closed");
            }

            int ret;

            do
            {
                ret = zlib.deflate(zstream, zlib.Z_FINISH);
                if (ret == zlib.Z_OK)
                {
                    if (zstream.avail_out == 0)
                    {
                        baseStream.Write(zstream.out_buf, 0, zstream.next_out);
                        zstream.next_out  = 0;
                        zstream.avail_out = (uint)zstream.out_buf.Length;
                    }
                }
                else if (ret != zlib.Z_STREAM_END)
                {
                    if (zstream.msg != null && zstream.msg.Length > 0)
                    {
                        throw new Exception(zstream.msg);
                    }

                    throw new Exception("zlib error");
                }
            } while (ret != zlib.Z_STREAM_END);

            if (zstream.next_out > 0)
            {
                baseStream.Write(zstream.out_buf, 0, zstream.next_out);
                zstream.next_out  = 0;
                zstream.avail_out = (uint)zstream.out_buf.Length;
            }

            if (zstream != null)
            {
                zlib.deflateEnd(zstream);
            }
            zstream = null;
        }