Share via


How to extract and read the files from a Zip file which is sent as a byte array format.

Question

Thursday, October 7, 2010 9:29 AM

I have a web service which gets a zip file in terms of byte array. I need to extract the file and read the contents of the files. How to do that. I would be grateful to you.

Thanks in advance.

All replies (5)

Thursday, October 7, 2010 10:32 AM ✅Answered | 1 vote

Hi,

Try ICSharpCode.SharpZipLib.Zip. It's free. You can double check the license here http://www.icsharpcode.net/opensource/sharpziplib/.

A sample usage is available at http://www.eggheadcafe.com/tutorials/aspnet/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-f.aspx.

Thanks & Regards, Gokul.N


Friday, October 8, 2010 6:07 AM ✅Answered

if you are using ICSharpCode.SharpZipLib, just turn it into a MemoryStream and pass it to the ZipInputStream  - then you can read the individual entries for the file and write them to disk or do whatever you want:

using ( MemoryStream mem = new MemoryStream ( myByteArray ))
using ( ZipInputStream zipStream = new ZipInputStream ( mem ) )
{
 ZipEntry currentEntry;
 while ( ( currentEntry = zipStream.GetNextEntry () ) != null )
 {
  Console.WriteLine ("{0} is {1} bytes", currentEntry.Name, currentEntry.Size);
  byte[] data = new byte[currentEntry.Size];
  zipEntry.Read ( data, 0, data.Length );

  // do what ever with the data
 }
}

There is some support for zip files in the framework - there is the ZipPackage in System.IO.Packaging, and while it supports zip compression/decompression - it expects that the contents be arranged in certain ways according to the Open Packaging Specification.  There's also the GZipStream/DeflateStream in System.IO.Compression, but it's not a full container - in other words, it doesn't necessarily support directory structures and multiple files.

So, is it possilbe to write your own?  Absolutely, but it probably isn't worth it since ICSharpCode.SharpZipLib is free, well tested (it's been around for ages), and can be used in commercial apps....

 Just wanted to add, there is actually a much easier way to decompress a Zip file with sharpziplib - you can just write the byte array to a temp file, and then use the FastZip class to unzip it to where you really want it :) 

Tom Shelton


Thursday, October 7, 2010 11:16 AM

The first thing you should do is to put the byte array back to the type of the object by using a memorystream.

However, you cannot set a byte array back to an unknown type (let say dynamic or something like that)

Be aware like you see from the reply from Gokul that not all Zip file use the same type.

 

Success
Cor


Friday, October 8, 2010 5:29 AM

Thanks for the prompt reply to Gokul and Cor. But Cor I  do not understand puttying the byte array back to the type of object. Does it mean, I need to change the byte array to a zip file using memory stream.

I am very new to this .NET world. Is there a way to write the logic myself or to use the in built classes in .NET without using the 3rd party libraries as Gokul has sent.

Thanks


Friday, October 8, 2010 5:46 AM

If you want to unzip it, yes.

To do the logic yourself, you need to have the Type description (class), try the owner of the webservice or look if there is not also an help inside the returned values of the webservice. You do the same when you set a Blob (bytearray) to an image you know.

Success
Cor