0

We are using DotNetZipLib for handling zip files.

I have a zip file which contains another zip file which contains a lot of (large) files.

outerZipFile.zip
  -> foo.ext
  -> innerZipFile    
      -> file 1
      -> file 2
  -> bar.ext

I want to open the inner zip file without having to read the whole entry into memory at once nor extract it to the hard drive. I have found no way to open the inner entry as a stream because this throws an exception:

public void Foo(Stream source)
{  
  using (var zip = ZipFile.Read(source))
  {
    foreach (var entry in zip.Entries)
    {
      if (entry.FileName != "innerZipFile")
      {
        continue;
      }
      var stream = entry.OpenReader();
      // throws 'Ionic.Zip.ZipException'
      // Cannot read that as a ZipFile
      var innerZip = ZipFile.Read(stream);
    }
  }
}

Is there any way to open the inner zip from a stream without having to read the complete data?

My goal is to read just some of the inner entries without having to extract the whole inner zip (even into memory), because the inner zip can be up to 1GB of data.

DerApe
  • 3,097
  • 2
  • 35
  • 55
  • Unless I'm mistaken you cannot seek inside a zip file, you must decompress it to memory or disk. Someone please correct me if I'm mistaken as I'd be keen to learn otherwise. The accepted answer in [this answer](http://stackoverflow.com/questions/11857913/getting-a-cannot-read-that-as-a-zip-file-exception-while-trying-to-get-a-strea?rq=1) has an example but I'm not able to test that it works. – Equalsk Jan 18 '17 at 15:25
  • @Equalsk Yea, I have found this too, but this will the extract the whole content into the memory stream (as you said) and we are talking about couple 100MB which I am trying to avoid – DerApe Jan 18 '17 at 15:28
  • Possible duplicate of [How to read data from a zip file without having to unzip the entire file](http://stackoverflow.com/questions/5967864/how-to-read-data-from-a-zip-file-without-having-to-unzip-the-entire-file) – TheLethalCoder Jan 18 '17 at 15:38
  • Maybe you could [extract the inner zip file as a stream](http://stackoverflow.com/questions/5967864/how-to-read-data-from-a-zip-file-without-having-to-unzip-the-entire-file), then open another instance of ZipFile from the stream, and read the contents of that as another stream? – stuartd Jan 18 '17 at 15:38
  • Note in the above dupe some answers show the use of reading specific files either into memory or to disk. You can then use this new stream, or file, to extract, or enter, the zip as you normally would. – TheLethalCoder Jan 18 '17 at 15:43
  • @TheLethalCoder I know, but I like I stated in the question, I do not want to extract the whole data (even into memory) but rather read it as a stream. My goal is to extract just some files of the inner zip without having to read all inner entries – DerApe Jan 18 '17 at 15:45
  • @derape I've managed to get it to work using the standard .NET library but I believe it is reading everything into memory... – TheLethalCoder Jan 18 '17 at 15:58
  • Would you like me to post the solution I've got to view even if it, most likely, reads everything into memory? Seeing as you say your code throws an exception. – TheLethalCoder Jan 18 '17 at 16:11
  • @TheLethalCoder If you are sure that your solution ready everything into memory then it is not necessary, otherwise go ahead and post it :-) – DerApe Jan 19 '17 at 07:10

0 Answers0