Example #1
0
        public static byte[] BCGZipCompress(byte[] buf)
        {
            using (var ms = new MemoryStream())
            {
                ms.WriteByte(0x1f);
                ms.WriteByte(0x8b);
                ms.WriteByte(0x08);
                ms.WriteByte(0x00);
                ms.WriteByte(0x00);
                ms.WriteByte(0x00);
                ms.WriteByte(0x00);
                ms.WriteByte(0x00);
                ms.WriteByte(0x02);
                ms.WriteByte(0xFF);

                using (var gzs = new ZOutputStream(ms, 6, true))
                {
                    gzs.Write(buf, 0, buf.Length);
                    gzs.Flush();
                }

                var crc = LZUtils.CRC32(buf);

                // ZOutputStream closes the outer stream...
                using (var ms2 = new MemoryStream())
                {
                    StreamUtils.Write(ms2, ms.ToArray());
                    StreamUtils.WriteUInt32(ms2, crc);
                    StreamUtils.WriteInt32(ms2, buf.Length);

                    var msb = ms2.ToArray();

                    /*
                     * using ( var foos = new FileStream( "test.gz", FileMode.Create, FileAccess.Write ) )
                     * {
                     * foos.Write( msb );
                     * }
                     */

                    return(msb);
                }
            }
        }
Example #2
0
        public static BufLen BCGZipCompressNew(BufLen buf)
        {
            var crc = LZUtils.CRC32(buf);

            var dest   = new byte[buf.Length + 4096];
            var destix = 0;

            dest[destix++] = 0x1f;
            dest[destix++] = 0x8b;
            dest[destix++] = 0x08;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x02;
            dest[destix++] = 0xFF;

            var z = new ZStream();

            z.deflateInit(6, true);

            z.next_in_index = buf.BaseArrayOffset;
            z.next_in       = buf.BaseArray;
            z.avail_in      = buf.Length;

bigger_dest:

            z.next_out       = dest;
            z.next_out_index = destix;
            z.avail_out      = dest.Length - destix;
            var err = z.deflate(JZlib.Z_FINISH);

            if (err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
            {
                throw new IOException("deflating: " + 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;
            }

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

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

            result.Poke32(crc, result.Length - 8);
            result.Poke32((uint)buf.Length, result.Length - 4);

            z.deflateEnd();

            /*
             * using ( var foos = new FileStream( "test.gz", FileMode.Create, FileAccess.Write ) )
             * {
             * foos.Write( msb );
             * }
             */
            return(result);
        }