当压缩文件达到 65536 字节时,Ionic.zlib 停止压缩? - C#

发布于 2024-11-07 00:32:11 字数 624 浏览 3 评论 0原文

嘿,我在使用 Ionic.zlib 压缩文件时遇到问题,我对 C# 很陌生,所以问题可能很容易解决。如果我压缩一个大文件,假设大小为 500kb,那么一旦压缩文件达到 65536 字节,它就会停止,如果我随后解压缩该文件,则会丢失大量数据:/。我可以通过将缓冲区设置为 4,000,000 来解决此问题,但我听说最好将其设置为 0x4000。

        ZlibStream compressor = new ZlibStream(gsc_stream, CompressionMode.Compress, CompressionLevel.BestCompression, true);
        byte[] buffer = new byte[0x4000];
        Int32 n;

        int previous = Convert.ToInt32(zone.Position);

        while ((n = compressor.Read(buffer, 0, buffer.Length)) != 0)
        {
            zone.Write(buffer, 0, n);

        }
        zone.Flush();
        compressor.Flush();

Hey, I'm having problems compressing files with Ionic.zlib, I'm very new to C# so the problem may be easily solvable. If I compress a large file, let's say 500kb in size then once the compressed file has reached 65536 bytes it will stop, if I then decompress the file theres a lot of data missing :/. I can fix this by setting the buffer to like 4,000,000, but I heard that it's best to have it set to 0x4000.

        ZlibStream compressor = new ZlibStream(gsc_stream, CompressionMode.Compress, CompressionLevel.BestCompression, true);
        byte[] buffer = new byte[0x4000];
        Int32 n;

        int previous = Convert.ToInt32(zone.Position);

        while ((n = compressor.Read(buffer, 0, buffer.Length)) != 0)
        {
            zone.Write(buffer, 0, n);

        }
        zone.Flush();
        compressor.Flush();

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

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

发布评论

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

评论(1

无边思念无边月 2024-11-14 00:32:11

看起来你的想法正好相反。
如果您尝试压缩流gsc_stream中的文件并将结果写入流zone,那么正确的代码是:

using (ZlibStream compressor = new ZlibStream(zone, CompressionMode.Compress, CompressionLevel.BestCompression, true))
{
    byte[] buffer = new byte[0x4000];
    int n;
    while ((n = gsc_stream.Read(buffer, 0, buffer.Length)) != 0)
    {
        compressor.Write(buffer, 0, n);
    }
    zone.Flush();
    compressor.Flush();
}

Looks like you have it the other way around.
If you're trying to compress the file in the stream gsc_stream and write the result into the stream zone then correct code would be:

using (ZlibStream compressor = new ZlibStream(zone, CompressionMode.Compress, CompressionLevel.BestCompression, true))
{
    byte[] buffer = new byte[0x4000];
    int n;
    while ((n = gsc_stream.Read(buffer, 0, buffer.Length)) != 0)
    {
        compressor.Write(buffer, 0, n);
    }
    zone.Flush();
    compressor.Flush();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文