在c#中使用zlib.net压缩和解压缩字符串

时间:2015-07-23 08:36:59

标签: c# zlib

我在c#中使用zlib.net来压缩和解压缩字符串。

这是我的代码

class GZOutputStream : ZOutputStream
{
    public GZOutputStream(Stream in_Renamed) : base(in_Renamed)
    {
        byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(sDictionary);
        z.inflateSetDictionary(dictionary, dictionary.Length);
    }

    public GZOutputStream(Stream in_Renamed, int level) : base(in_Renamed, level)
    {
        byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(sDictionary);
        z.deflateSetDictionary(dictionary, dictionary.Length);
    }
}

public static byte[] compressString(string source)
{ 
    byte[] buffer = System.Text.Encoding.Default.GetBytes (source);

    MemoryStream memOutput = new MemoryStream ();
    GZOutputStream zipOut   = new GZOutputStream(memOutput, zlibConst.Z_DEFAULT_COMPRESSION);

    zipOut.Write(buffer, 0, buffer.Length);
    zipOut.finish();

    memOutput.Seek(0, SeekOrigin.Begin);
    byte[] result = memOutput.ToArray();

    return result;
}

public static byte[] deCompressString(string source)
{ 
    byte[] buffer = System.Text.Encoding.Default.GetBytes (source);
    MemoryStream memOutput = new MemoryStream ();
    GZOutputStream zipOut   = new GZOutputStream(memOutput);

    zipOut.Write(buffer, 0, buffer.Length);
    zipOut.finish();

    memOutput.Seek(0, SeekOrigin.Begin);
    byte[] result = memOutput.ToArray();

    return result;
}

当我压缩字符串时,效果很好。但是,当我解压缩compress函数的结果字符串时,有一个例外:

  

ZStreamException:inflating:
  zlib.ZOutputStream.Write(System.Byte [] b1,Int32 off,Int32 len)

那么解决方案是什么?

2 个答案:

答案 0 :(得分:0)

我知道

有5年历史了,但是对于仍然在寻找(并且一直在)的任何人,问题在于ZOutputStream中的Write方法在第一次调用之后会返回 zlibConst.Z_NEED_DICT z.inflate ,那就是需要提供字典的时间。因此,您需要重写Write方法,测试该返回值并在那时提供字典...

public override void Write(System.Byte[] b1, int off, int len)
    {
        if (len == 0)
            return;
        int err;
        
        z.next_in = b1;
        z.next_in_index = off;
        z.avail_in = len;
        do
        {
            z.next_out = buf;
            z.next_out_index = 0;
            z.avail_out = bufsize;
            if (compress)
                err = z.deflate(flush_Renamed_Field);
            else
                err = z.inflate(flush_Renamed_Field);

            if (err == zlibConst.Z_NEED_DICT)
            {
                int y=z.inflateSetDictionary(dict, dict.Length);
                err = z.inflate(flush_Renamed_Field);
            }

            if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
            {
                throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
            }
            ms.Write(buf, 0, bufsize - z.avail_out);
        }
        while (z.avail_in > 0 || z.avail_out == 0);
    }
   
}

答案 1 :(得分:-1)

    public static string DeCompressToString(byte[] buffer)
    {
        MemoryStream memOutput = new MemoryStream();
        ZOutputStream zipOut = new ZOutputStream(memOutput);

        zipOut.Write(buffer, 0, buffer.Length);
        zipOut.finish();

        memOutput.Seek(0, SeekOrigin.Begin);
        byte[] result = memOutput.ToArray();

        var str = System.Text.Encoding.Default.GetString(result);

        return str;
    }