Exemplo n.º 1
0
    public static void SaveAsGzipFile(System.IO.Stream s, string content)
    {
        byte[] data    = System.Text.Encoding.UTF8.GetBytes(content);
        byte[] deflate = Deflator.DeflateToByteArray(data, 9);

        s.WriteByte(31);  // ID1
        s.WriteByte(139); // ID2
        s.WriteByte(8);   // CM  deflate
        s.WriteByte(1);   // text file

        long mtime = (long)(System.DateTime.Now - UNIX_EPOCH).TotalSeconds;

        s.WriteByte((byte)(0xFF & mtime));
        s.WriteByte((byte)(0xFF & mtime >> 8));
        s.WriteByte((byte)(0xFF & mtime >> 16));
        s.WriteByte((byte)(0xFF & mtime >> 24));

        s.WriteByte(2); // highest compression
        s.WriteByte(3); // line break = unix style '\n'

        s.Write(deflate, 0, deflate.Length);

        uint crc32 = Deflator.CalculateCrc32(data);

        s.WriteByte((byte)(0xFF & crc32));
        s.WriteByte((byte)(0xFF & crc32 >> 8));
        s.WriteByte((byte)(0xFF & crc32 >> 16));
        s.WriteByte((byte)(0xFF & crc32 >> 24));
        uint isize = (uint)data.Length;

        s.WriteByte((byte)(0xFF & isize));
        s.WriteByte((byte)(0xFF & isize >> 8));
        s.WriteByte((byte)(0xFF & isize >> 16));
        s.WriteByte((byte)(0xFF & isize >> 24));
    }