简体   繁体   中英

How to add a zip file to zip file using C# and DotNetZip

I am using DotNetZip ( http://dotnetzip.codeplex.com/ ) to build Zip files on the fly. I would like to be able to create a ZIP file in code and then add it to another ZIP file in code. Will this work? My code is below. I am trying to use a MemoryStream to hold the internal zip file and then add that memory stream to the main zip file. I'm not sure if this is correct. When I try this, my internal zip file has zero bytes and it fails when I try to open up the resulting main zip file.

using (ZipFile _mainZip = new ZipFile())
{
  using (ZipFile _internalZip = new ZipFile())
  {
    ..add stuff to the internal zip..
    MemoryStream _myMemoryStream = new MemoryStream();
    _internalZip.Save(_myMemoryStream);
    _mainZip.AddEntry("myTitle.zip", _myMemoryStream);
  }
}
_mainZip.Save("myZip.zip");

It might be that the MemoryStream's position is at the end of the stream.
You could try to set it to the begining:

_myMemoryStream.Position = 0;

That might work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM