zlib.Net的压缩和解压缩问题 [英] Compression and decompression problem with zlib.Net

查看:106
本文介绍了zlib.Net的压缩和解压缩问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ZLIB.Net ,我只是不明白该怎么办压缩不是 FileStream 而是 MemoryStream 的流。通过执行以下操作:

I am using ZLIB.Net and I just don't understand what should I do to compress a stream which is not FileStream, but MemoryStream. By doing:

byte[] buffer = ASCIIEncoding.ASCII.GetBytes("Hello World");

MemoryStream outStream = new MemoryStream();
zlib.ZOutputStream outZStream = new zlib.ZOutputStream(
    outStream,
    zlib.zlibConst.Z_BEST_COMPRESSION);

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

buffer = outStream.GetBuffer();
Debug.WriteLine(DateTime.Now.ToString() + ":" + buffer.Length);

MemoryStream inStream = new MemoryStream(buffer);
MemoryStream mo = new MemoryStream();
zlib.ZInputStream inZStream = new zlib.ZInputStream(
    inStream,
    zlib.zlibConst.Z_BEST_COMPRESSION);

int n = 0;
while ((n = inZStream.Read(buffer, 0, buffer.Length)) > 0)
{
    mo.Write(buffer, 0, n);
}

string STR = ASCIIEncoding.ASCII.GetString(mo.GetBuffer());

我无法获得字符串 Hello World 返回。

I can't get the string "Hello World" back.

推荐答案

longbkit ,感谢您的参考。
里面有代码:

longbkit, Thank you for that reference. There is code in there:

 public static void CompressData(byte[] inData, out byte[] outData)
{
    using (MemoryStream outMemoryStream = new MemoryStream())
    using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_DEFAULT_COMPRESSION))
    using (Stream inMemoryStream = new MemoryStream(inData))
    {
        CopyStream(inMemoryStream, outZStream);
        outZStream.finish();
        outData = outMemoryStream.ToArray();
    }
}

public static void DecompressData(byte[] inData, out byte[] outData)
{
    using (MemoryStream outMemoryStream = new MemoryStream())
    using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream))
    using (Stream inMemoryStream = new MemoryStream(inData))
    {
        CopyStream(inMemoryStream, outZStream);
        outZStream.finish();
        outData = outMemoryStream.ToArray();
    }
}

public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
{
    byte[] buffer = new byte[2000];
    int len;
    while ((len = input.Read(buffer, 0, 2000)) > 0)
    {
        output.Write(buffer, 0, len);
    }
    output.Flush();
}   

它有效。
但是我看到的是压缩和解压缩之间的唯一区别是ZOutput构造函数中的压缩类型...

It works. But what I see is the only diffrence between Compression and Decompression is Compression Type in ZOutput Constructor...

了不起。对我来说,如果在解压缩-输入时将压缩称为输出,则会更加清楚。

Amazing. For me would be more clear if Compression is called Output while Decompression - Input. or such.. in fact it's Output only.


用户 John Smith破坏了原始代码,因为CopyTo不起作用并在其编辑后引发异常文字(并获得他人的两次认可...)。
由OP恢复为原始代码示例。
,请下次再次测试已编辑的代码,谢谢。

❋ user "John Smith" broke original code since CopyTo doesn't work and raises exception in his edited text (And approved twice by others...). Reverting back to original code example by OP. Please test edited code next time before editing, thank you.

这篇关于zlib.Net的压缩和解压缩问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆