如何将 ICSharpCode.ZipLib 与流一起使用?

发布于 2024-07-16 18:28:08 字数 1123 浏览 7 评论 0原文

我对保守的标题和我的问题本身感到非常抱歉,但我迷路了。

ICsharpCode.ZipLib 提供的示例不包含我正在搜索的内容。 我想通过将 byte[] 放入 InflaterInputStream(ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream) 中来解压缩它

,我找到了一个解压缩函数,但它不起作用。

    public static byte[] Decompress(byte[] Bytes)
    {
        ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream stream =
            new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(new MemoryStream(Bytes));
        MemoryStream memory = new MemoryStream();
        byte[] writeData = new byte[4096];
        int size;

        while (true)
        {
            size = stream.Read(writeData, 0, writeData.Length);
            if (size > 0)
            {
                memory.Write(writeData, 0, size);
            }
            else break;
        }
        stream.Close();
        return memory.ToArray();
    }

它在 line(size = stream.Read(writeData, 0, writeData.Length);) 处抛出异常,表示它有一个无效的标头。

我的问题不是如何修复该函数,该函数没有随库提供,我只是在谷歌上搜索到它。我的问题是,如何以与 InflaterStream 函数相同的方式解压缩,但没有例外。

再次感谢 - 对于保守的问题感到抱歉。

I'm very sorry for the conservative title and my question itself,but I'm lost.

The samples provided with ICsharpCode.ZipLib doesn't include what I'm searching for.
I want to decompress a byte[] by putting it in InflaterInputStream(ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream)

I found a decompress function ,but it doesn't work.

    public static byte[] Decompress(byte[] Bytes)
    {
        ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream stream =
            new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(new MemoryStream(Bytes));
        MemoryStream memory = new MemoryStream();
        byte[] writeData = new byte[4096];
        int size;

        while (true)
        {
            size = stream.Read(writeData, 0, writeData.Length);
            if (size > 0)
            {
                memory.Write(writeData, 0, size);
            }
            else break;
        }
        stream.Close();
        return memory.ToArray();
    }

It throws an exception at line(size = stream.Read(writeData, 0, writeData.Length);) saying it has a invalid header.

My question is not how to fix the function,this function is not provided with the library,I just found it googling.My question is,how to decompress the same way the function does with InflaterStream,but without exceptions.

Thanks and again - sorry for the conservative question.

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

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

发布评论

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

评论(3

迷雾森÷林ヴ 2024-07-23 18:28:09

lucene中的代码非常好。

public static byte[] Compress(byte[] input) {
        // Create the compressor with highest level of compression  
        Deflater compressor = new Deflater();
        compressor.SetLevel(Deflater.BEST_COMPRESSION);

        // Give the compressor the data to compress  
        compressor.SetInput(input);
        compressor.Finish();

        /* 
         * Create an expandable byte array to hold the compressed data. 
         * You cannot use an array that's the same size as the orginal because 
         * there is no guarantee that the compressed data will be smaller than 
         * the uncompressed data. 
         */
        MemoryStream bos = new MemoryStream(input.Length);

        // Compress the data  
        byte[] buf = new byte[1024];
        while (!compressor.IsFinished) {
            int count = compressor.Deflate(buf);
            bos.Write(buf, 0, count);
        }

        // Get the compressed data  
        return bos.ToArray();
    }

    public static byte[] Uncompress(byte[] input) {
        Inflater decompressor = new Inflater();
        decompressor.SetInput(input);

        // Create an expandable byte array to hold the decompressed data  
        MemoryStream bos = new MemoryStream(input.Length);

        // Decompress the data  
        byte[] buf = new byte[1024];
        while (!decompressor.IsFinished) {
            int count = decompressor.Inflate(buf);
            bos.Write(buf, 0, count);
        }

        // Get the decompressed data  
        return bos.ToArray();
    }

the code in lucene is very nice.

public static byte[] Compress(byte[] input) {
        // Create the compressor with highest level of compression  
        Deflater compressor = new Deflater();
        compressor.SetLevel(Deflater.BEST_COMPRESSION);

        // Give the compressor the data to compress  
        compressor.SetInput(input);
        compressor.Finish();

        /* 
         * Create an expandable byte array to hold the compressed data. 
         * You cannot use an array that's the same size as the orginal because 
         * there is no guarantee that the compressed data will be smaller than 
         * the uncompressed data. 
         */
        MemoryStream bos = new MemoryStream(input.Length);

        // Compress the data  
        byte[] buf = new byte[1024];
        while (!compressor.IsFinished) {
            int count = compressor.Deflate(buf);
            bos.Write(buf, 0, count);
        }

        // Get the compressed data  
        return bos.ToArray();
    }

    public static byte[] Uncompress(byte[] input) {
        Inflater decompressor = new Inflater();
        decompressor.SetInput(input);

        // Create an expandable byte array to hold the decompressed data  
        MemoryStream bos = new MemoryStream(input.Length);

        // Decompress the data  
        byte[] buf = new byte[1024];
        while (!decompressor.IsFinished) {
            int count = decompressor.Inflate(buf);
            bos.Write(buf, 0, count);
        }

        // Get the decompressed data  
        return bos.ToArray();
    }
叹梦 2024-07-23 18:28:09

嗯,听起来数据不合适,否则代码可以正常工作。 (诚​​然,我会为流使用“using”语句,而不是显式调用Close。)

您从哪里获取数据?

Well it sounds like the data is just inappropriate, and that otherwise the code would work okay. (Admittedly I'd use a "using" statement for the streams instead of calling Close explicitly.)

Where did you get your data from?

酒几许 2024-07-23 18:28:09

为什么不使用 System.IO.Compression.DeflateStream 类(自 .Net 2.0 起可用)? 这使用相同的压缩/解压缩方法,但不需要额外的库依赖项。

从 .Net 2.0 开始,如果您需要文件容器支持,则只需要 ICSharpCode.ZipLib。

Why don't you use the System.IO.Compression.DeflateStream class (available since .Net 2.0)? This uses the same compression/decompression method but doesn't require an extra library dependency.

Since .Net 2.0 you only need the ICSharpCode.ZipLib if you need the file container support.

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