How to use ICSharpCode.ZipLib with stream?

早过忘川 提交于 2019-12-10 16:27:07

问题


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.


回答1:


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?




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/741591/how-to-use-icsharpcode-ziplib-with-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!