2

I am trying to create a zip file that contains zip files inside of it. I am using ICSharpCode.SharpZipLib (must use this due to project restrictions). this works fine if I have only 1 byte[] array.. but it is not working for list of byte[].

foreach (byte[] internalZipFile in zipFiles)
{
    // Source : internal zip file
    MemoryStream inputMemoryStream = new      MemoryStream(internalZipFile);

    ZipEntry newZipEntry = new ZipEntry("AdManifest-" + i.ToString() + ".zip");
    newZipEntry.DateTime = DateTime.Now;

    zipStream.PutNextEntry(newZipEntry);

    StreamUtils.Copy(inputMemoryStream, zipStream, new byte[1024]);

    zipStream.CloseEntry();

    zipStream.IsStreamOwner = false; // to stop the close and underlying stream
    zipStream.Close();

    outputMemoryStream.Position = 0;

    zipByteArray = outputMemoryStream.ToArray();

    i++;
}
using (FileStream fileStream = new FileStream(@"c:\manifest.zip", FileMode.Create))
{
    fileStream.Write(zipByteArray, 0, zipByteArray.Length);
}

Can someone please assist? what am i missing?

Daniel
  • 9,491
  • 12
  • 50
  • 66
Hamed Salameh
  • 223
  • 1
  • 5
  • 13

2 Answers2

3

I figured this out. here us the one working for me :

byte[] zipByteArray = null;
        int i = 0;

        if (zipFiles != null && zipFiles.Count > 0)
        {
            MemoryStream outputMemoryStream = new MemoryStream();
            ZipOutputStream zipStream = new ZipOutputStream(outputMemoryStream);
            zipStream.SetLevel(3);

            foreach (byte[] internalZipFile in zipFiles)
            {
                MemoryStream inputMemoryStream = new MemoryStream(internalZipFile);

                    ZipEntry newZipEntry = new ZipEntry("AdManifest-" + i.ToString() + ".zip");
                    newZipEntry.DateTime = DateTime.Now;
                    newZipEntry.Size = internalZipFile.Length;

                    zipStream.PutNextEntry(newZipEntry);

                    StreamUtils.Copy(inputMemoryStream, zipStream, new byte[1024]);
                    zipStream.CloseEntry();
                i++;
            }

            zipStream.IsStreamOwner = false; // to stop the close and underlying stream
            zipStream.Close();

            outputMemoryStream.Position = 0;
            zipByteArray = outputMemoryStream.ToArray();

            using (FileStream fileStream = new FileStream(@"c:\manifest.zip", FileMode.Create))
            {
                fileStream.Write(zipByteArray, 0, zipByteArray.Length);
            }
        }
Hamed Salameh
  • 223
  • 1
  • 5
  • 13
0

I can't try it, but i think than you need less code in iteration body

Plus that i've removed outpustmemorystream and used only zipStream.

foreach (byte[] internalZipFile in zipFiles)
{
    // Source : internal zip file
    MemoryStream inputMemoryStream = new MemoryStream(internalZipFile);

    ZipEntry newZipEntry = new ZipEntry("AdManifest-" + i.ToString() + ".zip");
    newZipEntry.DateTime = DateTime.Now;

    zipStream.PutNextEntry(newZipEntry);

    StreamUtils.Copy(inputMemoryStream, zipStream, new byte[1024]);

    zipStream.CloseEntry();

    i++;
}
zipStream.IsStreamOwner = false; // to stop the close and underlying stream

zipStream.Position = 0;

zipByteArray = zipStream.ToArray();

zipStream.Close();

using (FileStream fileStream = new FileStream(@"c:\manifest.zip", FileMode.Create))
{
    fileStream.Write(zipByteArray, 0, zipByteArray.Length);
}