0

I have a zip file. I want open it with SharpZipLib and add a new ZipEntry to it that it is created in memory. I am new to SharpZipLib. I googled very much but couldn't find similar problem.

My Sample Code is:

public Stream GetNewZipFileStream(string zipFilePath)
{
   byte[] zipFileBytes = null;
   zipFileBytes = ReadFileBytes(zipFilePath);
   var zipFileMemoryStream = new MemoryStream(zipFileBytes);

   ZipOutputStream zipOutStream = new ZipOutputStream(zipFileMemoryStream);

   var newEntry = new ZipEntry("NewFile.txt");
   zipOutStream.PutNextEntry(newEntry);

   var newFileMemoryStream = MakeOnTheFlyStream();
   StreamUtils.Copy(newFileMemoryStream , zipOutStream, new byte[4096]);

   zipOutStream.CloseEntry();
   newFileMemoryStream.Close();
   zipOutStream.IsStreamOwner = false;
   zipOutStream.Close();
   newFileMemoryStream.Position = 0;
   return newFileMemoryStream;
}

ReadFileBytes and MakeOnTheFlyStream are my methods.

  • Possible duplicate of https://stackoverflow.com/questions/1356003/c-sharp-sharpziplib-adding-file-to-existing-archive – Nareen Babu Nov 04 '19 at 13:30
  • where is your try? add some code to know where and what problem you are getting? – Mukul Keshari Nov 04 '19 at 13:34
  • @user2932057 No, It's different. I need to write an stream into a ZipEntry and add it to an existing zip file. – Mohammad Ali Nov 04 '19 at 13:50
  • That's what that other question is doing, too. –  Nov 04 '19 at 14:33
  • @Amy All of that solutions add new file from hard with a path. But I generate my file stream with `MakeOnTheFlyStrem` and I don't want to save it on hard because of security reasons. So I have a FileStream and I want to add it to ZipFile. – Mohammad Ali Nov 04 '19 at 15:15
  • @MohammadAliTaqvazadeh They open that file off the hard drive... *as a stream*. You already have a stream. So *skip* the part where they load the file. –  Nov 04 '19 at 15:21
  • @Amy I tried [this solution](https://stackoverflow.com/a/1356306/7672704) but when I opened my new zip file I got this error: _The archive is either in unknown format or damaged_ – Mohammad Ali Nov 04 '19 at 16:02
  • Finally I found appropriate solution: [SharpZipLib - adding a ZipEntry to a ZipFile throws a ZipException](https://stackoverflow.com/a/28631536/7672704) – Mohammad Ali Nov 04 '19 at 20:22

0 Answers0