Exemplo n.º 1
0
        public static byte[] BCGZipDecompress(BufLen buf)
        {
            var reader = new BufRefLen(buf);

            using (var ms = new MemoryStream())
            {
                // Skip gzip header
                var gzheader = reader.ReadBufLen(10);
                var flag     = gzheader.Peek8(3);
                if ((flag & 0x04) != 0)
                {
                    reader.Seek(reader.Read16());                           // "Extra"
                }
                if ((flag & 0x08) != 0)
                {
                    while (reader.Read8() != 0)
                    {
                        ;                                                   // "Name"
                    }
                }
                if ((flag & 0x10) != 0)
                {
                    while (reader.Read8() != 0)
                    {
                        ;                                                   // "Comment"
                    }
                }
                if ((flag & 0x02) != 0)
                {
                    reader.Read16();                         // "CRC16"
                }
                ms.Write(reader.BaseArray, reader.BaseArrayOffset, reader.Length);
                ms.Position = 0;

                using (var gzs = new ZInputStream(ms, true))
                {
                    var gzdata = StreamUtils.Read(gzs);
                    return(gzdata);
                }
            }
        }
Exemplo n.º 2
0
        public static BufLen BCGZipDecompressNew(BufLen buf)
        {
            var reader = new BufRefLen(buf);

            // Skip gzip header
            var gzheader = reader.ReadBufLen(10);
            var flag     = gzheader.Peek8(3);

            if ((flag & 0x04) != 0)
            {
                reader.Seek(reader.Read16());                           // "Extra"
            }
            if ((flag & 0x08) != 0)
            {
                while (reader.Read8() != 0)
                {
                    ;                                                   // "Name"
                }
            }
            if ((flag & 0x10) != 0)
            {
                while (reader.Read8() != 0)
                {
                    ;                                                   // "Comment"
                }
            }
            if ((flag & 0x02) != 0)
            {
                reader.Read16();                         // "CRC16"
            }
            var z = new ZStream();
            z.inflateInit(true);

            var dest   = new byte[buf.Length * 2];
            var destix = 0;

            z.next_in_index = reader.BaseArrayOffset;
            z.next_in       = reader.BaseArray;
            z.avail_in      = reader.Length - 8;

bigger_dest:

            z.next_out       = dest;
            z.next_out_index = destix;
            z.avail_out      = dest.Length - destix;
            var err = z.inflate(JZlib.Z_FINISH);
            if (err != JZlib.Z_BUF_ERROR && err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
            {
                throw new IOException("inflating: " + z.msg);
            }

            if (z.avail_out == 0)
            {
                var newdest = new byte[dest.Length * 2];
                Array.Copy(dest, newdest, dest.Length);
                destix = dest.Length;
                dest   = newdest;
                goto bigger_dest;
            }

            var result = new BufLen(dest, 0, dest.Length - z.avail_out);
            z.inflateEnd();

            return(result);
        }