创建多个 byte[] 数组/文件的 zip 文件

Creating zip file of multiple byte[] arrays / files

我正在尝试创建一个 zip 文件,其中包含 zip 文件。我正在使用 ICSharpCode.SharpZipLib(由于项目限制必须使用它)。如果我只有 1 个 byte[] 数组,这很好用。但它不适用于 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);
}

有人可以帮忙吗?我错过了什么?

我不能尝试,但我认为你在迭代体中需要的代码更少

此外,我已经删除了 outpustmemorystream 并且只使用了 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);
}

我想通了。 这是为我工作的人:

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);
            }
        }