Creating a stream from the ZIP file

Nov 22, 2010 at 2:23 PM
Edited Nov 22, 2010 at 2:54 PM

I am attempting to do a binary read of a file from within the ZIP archive using the following code :

 

MemoryStream inp = new MemoryStream(100);

String sZipPath = @"data2.zip";

String sFileName = "format.xml";
char[] charArray;
byte[] byteArray;

  using (ZipFile zip = ZipFile.Read(sZipPath))
      {
          if (zip.EntryFileNames.Contains(sFileName))
          {
           // use the string indexer on the zip file
             zip[sFileName].Extract(inp);
             byteArray = new byte[200];
             int i = inp.Read(byteArray, 0, 100);
           }
      }

When stepping through the code the ZIP file is found and opened, the filename specified is located and the Memory stream is created with the contents of the file. The problem is with the "Read" method against the memory steam, currently it executes and populates the byteArray with a 100 occurrences of x'00' - not the content of the file.

What am I doing wrong ?

 

Nov 22, 2010 at 2:30 PM

I think you need to move to the start of yout "inp" stream before reading. At the moment your code extracts the compressed file to the stream, then reads the next 100 bytes after the end of that data.

Try this:

// use the string indexer on the zip file
zip[sFileName].Extract(inp);
inp.Position = 0;
byteArray = new byte[200];
int i = inp.Read(byteArray, 0, 100);

Cheers,

M

Nov 22, 2010 at 2:54 PM

Thanks for the quick response, your suggestion was correct. It's working great now.

Richard