How to use ICSharpCode.ZipLib with a stream?

I am very sorry for the conservative title and the question itself, but I was lost.

Samples provided using ICsharpCode.ZipLib do not include what I'm looking for. I want to unzip byte [] by putting it in InflaterInputStream (ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream)

I found the decompress function, but it does not 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 in the string (size = stream.Read (writeData, 0, writeData.Length);) says that it has an invalid header.

My question is not how to fix this function, this function is not provided in the library, I just found it googling. My question is how to unpack the same as a function with InflaterStream, but without exceptions.

Thanks and again - sorry for the conservative question.

+4
source share
3 answers

Well, it looks like the data is simply inappropriate, and otherwise the code will work fine. (Admittedly, I would use the "using" statement for threads, rather than directly calling Close .)

Where did you get the data?

+1
source

Why don't you use the System.IO.Compression.DeflateStream class (available with .Net 2.0)? This uses the same compression / decompression method, but does not require an additional library dependency.

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

+1
source

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

All Articles