Read text from a zipentry.

Feb 3, 2010 at 3:53 PM

Hi Cheeso

Just wondering if this is possible, i have a text file which is zipped in a zipfile.

I need to extract the text from the zipentry.

Are there any methods which would do that.

Thanks

N

Coordinator
Feb 3, 2010 at 8:06 PM

ZipEntry.OpenReader will do it.

Feb 4, 2010 at 8:47 AM
Edited Feb 4, 2010 at 8:52 AM

Hi Cheeso

It returns a CrcCalculatorStream. I am wondering how to get the text/string from a zipentry.

Thanks

N

Coordinator
Feb 4, 2010 at 11:08 AM

It's a stream;  you read all the bytes from the stream, then convert the bytes to a string using the encoding required.

For example

var stream = entry.OpenReader();
byte[] b = new byte[entry.UncompressedSize];
stream.Read(b,0,b.Length);
var string = System.Text.Encoding.ASCII.GetString(b);

You need to know which encoding to use.

If you prefer, you can Extract to a MemoryStream, something like this:

var ms = new MemoryStream();
entry.Extract(ms);
var string = System.Text.Encoding.ASCII.GetString(ms.ToArray());

Feb 4, 2010 at 11:15 AM

Thanks Cheeso.