ZLib解压

发布于 2024-08-03 09:53:34 字数 795 浏览 6 评论 0原文

我正在尝试使用 zlib .net 库压缩数据。无论未压缩字符串的内容如何,​​我似乎只在 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2024-08-10 09:53:34

这里的问题是,ZOutputStream 实际上在 finish() 方法(由 Close 调用)中将一些信息写入流中。 Close 方法还会关闭基本流,因此在这种情况下没有多大用处。

将代码更改为以下内容应该可以工作:

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文