解压zlib的 [英] ZLib decompression

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

问题描述

我试图压缩使用zlib .NET库数据。不管未压缩字符串的内容的我只似乎得到的数据的两个字节中的原始[]。

  {
字符串未压缩=1234567890;
字节[]数据= UTF8Encoding.Default.GetBytes(未压缩);

MemoryStream的输入=新的MemoryStream(数据);
MemoryStream的输出=新的MemoryStream();
流outZStream =新ZOutputStream(输出,zlibConst.Z_DEFAULT_COMPRESSION);

CopyStream(输入,outZStream);

output.Seek(0,SeekOrigin.Begin);
字节[] =原output.ToArray();
字符串压缩= Convert.ToBase64String(生);
}

公共无效CopyStream(的System.IO.Stream输入,输出的System.IO.Stream)
{
字节[]缓冲区=新的字节[2000] ;
INT LEN;
而((LEN = input.Read(缓冲液,0,2000))大于0)
{
output.Write(缓冲液,0,LEN);
}
output.Flush();
}


解决方案

这里的问题是, ZOutputStream实际上写的一些信息到终点()方法的流(这是由关闭调用)。 Close方法也关闭基本流,所以这不是在这种情况下多大用处。



中的代码更改为以下应该工作:

  {
字符串未压缩=1234567890;
字节[]数据= UTF8Encoding.Default.GetBytes(未压缩);

MemoryStream的输入=新的MemoryStream(数据);
MemoryStream的输出=新的MemoryStream();
ZOutputStream outZStream =新ZOutputStream(输出,zlibConst.Z_DEFAULT_COMPRESSION);

CopyStream(输入,outZStream);

outZStream.finish();

output.Seek(0,SeekOrigin.Begin);
字节[] =原output.ToArray();
字符串压缩= Convert.ToBase64String(生);
}

公共无效CopyStream(的System.IO.Stream输入,输出的System.IO.Stream)
{
字节[]缓冲区=新的字节[2000] ;
INT LEN;
而((LEN = input.Read(缓冲液,0,2000))大于0)
{
output.Write(缓冲液,0,LEN);
}
output.Flush();
}


I am trying to compress data using the zlib .net library. Regardless of the content of the uncompressed string I only seem to get two bytes of data in the raw[].

{
    string uncompressed = "1234567890";
    byte[] data = UTF8Encoding.Default.GetBytes(uncompressed);

    MemoryStream input = new MemoryStream(data);
    MemoryStream output = new MemoryStream();
    Stream outZStream = new ZOutputStream(output,zlibConst.Z_DEFAULT_COMPRESSION);

    CopyStream(input, outZStream);

    output.Seek(0, SeekOrigin.Begin);
    byte[] raw = output.ToArray();
    string compressed = Convert.ToBase64String(raw);
}

public 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();
}

解决方案

The problem here is that the ZOutputStream actually writes some of the information into the stream in the finish() method (which is called by Close). The Close method also closes the base stream, so that is not much use in this situation.

Changing the code to the following should work:

{
    string uncompressed = "1234567890";
    byte[] data = UTF8Encoding.Default.GetBytes(uncompressed);

    MemoryStream input = new MemoryStream(data);
    MemoryStream output = new MemoryStream();
    ZOutputStream outZStream = new ZOutputStream(output,zlibConst.Z_DEFAULT_COMPRESSION);

    CopyStream(input, outZStream);

    outZStream.finish();

    output.Seek(0, SeekOrigin.Begin);
    byte[] raw = output.ToArray();
    string compressed = Convert.ToBase64String(raw);
}

public 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();
}

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

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