This project is read-only.

New functionality for byte arrays

Sep 18, 2008 at 12:43 PM
Hi Cheeso,
I am currently using both your Zip classes and the classes from ICSharpCode.SharpZipLib.
I would very much like to consolidate all the zipping functionality into one set of classes (yours).
The functionality I need is to be able to treat the zip entity as a byte array and then compress/decompress it.

Please see my current functions below as an example



static byte[] PackFile(String filePath) {




byte[] b = File.ReadAllBytes(filePath);




Int32 size = b.Length;




MemoryStream ms = new MemoryStream();




// Prepend the compressed data with the length of the uncompressed data (firs 4 bytes)








BinaryWriter writer = new BinaryWriter(ms);





// compress the data








BZip2OutputStream zos = new BZip2OutputStream(ms);


zos.Write(b, 0, size);




return ms.ToArray();





public static byte[] UnpackFile(byte[] compressesBytes) {




MemoryStream ms = new MemoryStream(compressesBytes);




// read final uncompressed string size stored in first 4 bytes








BinaryReader reader = new BinaryReader(ms);




Int32 size = reader.ReadInt32();




// decompress string








BZip2InputStream zis = new BZip2InputStream(ms);




Byte[] bytesBuffer = new Byte[size];


zis.Read(bytesBuffer, 0, bytesBuffer.Length);





return bytesBuffer;




As always any help here would be greatly appreciated.

Many Thanks

Sep 19, 2008 at 8:38 PM

I'm not clear what you want to do.

I looked at the code you included.  It seems like what you want is to take an individual file and compress it.  Is that right?  and the output should be a byte array or stream.  Is that right?

If so, then you should look into the DeflateStream in the System.IO.Compression namespace, or the GZipStream in the same namespace.

These classes are included in the base class library for .NET.  They can compress individual files - the input is a stream (like the memorystream from your sample code) and the output is a stream of bytes that has been compressed.  It is fully reversible; you can use the same class with the "uncompress" option on it to uncompress the compressed stream.

These streams do not produce zip files - they produced compressed streams, or if you like, byte arrays.  The zipfile itself has a structure with header info and "metadata" (data about the files stored in the zip file).  This metadata is not produced by the DeflateStream (nor the GzipStream).  The metadata is why you need the DotNetZip library (or #ziplib).

Does this help?
Sep 23, 2008 at 10:18 AM
Hi Cheeso,

Funny how you overlook the simple things sometimes....

I am now using the System.IO.Compression namespace and works like a charm.

Many thanks