Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a "Cannot Read That as a Zip File" Exception while trying to get a stream from an Inner Zip File (a Zip within another Zip)

Tags:

c#

dotnetzip

In C#, I'm using the DotNetZip I have a zip called "innerZip.zip" which contains some data, and another zip called "outerZip.zip" which contains the innerZip. why am i doing it like this ? well, when setting the password, the password actually applies to individual entries that are added to the archive and not the whole archive, by using this inner/outer combo, I could set a pass to the whole inner zip because it's an entry of the outer one.

Problem is, well, code speaks better than normal words:

ZipFile outerZip = ZipFile.Read("outerZip.zip");
outerZip.Password = "VeXe";
Stream innerStream = outerZip["innerZip.zip"].OpenReader();
ZipFile innerZip = ZipFile.Read(innerStream); // I'm getting the exception here.
innerZip["Songs\\IronMaiden"].Extract(tempLocation);

why am I getting that exception ? the inner file is a zip file, so i shouldn't be getting that exception right ? is there a way to get around this or I just have to extract the inner one from the outer, and then access it ?

Thanx in advance ..

like image 780
vexe Avatar asked Aug 08 '12 05:08

vexe


1 Answers

This exception occurs because the CrcCalculatorStream stream that OpenReader creates is not seekable, and ZipFile.Read(Stream) tries to seek while opening the zip file.

The nature of zip compression prevents seeking to a location in the zipped content, the content must be decompressed in order.

One way around this would be to extract the inner zip file to a MemoryStream and then load that via ZipFile.Read.

MemoryStream ms = new MemoryStream();
outerZip["innerZip.zip"].Extract(ms);
ms.Seek(0, SeekOrigin.Begin);
ZipFile innerZip = ZipFile.Read(ms);
innerZip["Songs\\IronMaiden"].Extract(tempLocation);
like image 94
Syon Avatar answered Sep 30 '22 10:09

Syon