我尝试了以下(只是快速PoC)方法,但出现了异常:
System.IO.IOException: Message =处于创建模式的条目只能写入一次,并且一次只能打开一个条目。
using (MemoryStream zipMS = new MemoryStream())
{
using (ZipArchive zipArchive = new ZipArchive(zipMS, ZipArchiveMode.Create, true))
{
//read the file bytes
//byte[] fileToZipBytes = System.IO.File.ReadAllBytes(fileToZip);
//create the entry - this is the zipped filename
// just end with "/"
ZipArchiveEntry zipFileEntry1 = zipArchive.CreateEntry("folder1/folderchild1/test/test.png");
ZipArchiveEntry zipFileEntry2 = zipArchive.CreateEntry("folder1/folderchild1/test2/test2.png");
//add the file 1 contents
using (Stream zipEntryStream = zipFileEntry1.Open())
using (BinaryWriter zipFileBinary = new BinaryWriter(zipEntryStream))
{
zipFileBinary.Write(ssnewImage1);
}
//add the file 2 contents
using (Stream zipEntryStream2 = zipFileEntry2.Open())
using (BinaryWriter zipFileBinary2 = new BinaryWriter(zipEntryStream2))
{
zipFileBinary2.Write(ssnewImage2);
}
}
using (FileStream finalZipFileStream = new FileStream(@"C:\Users\peters\Desktop\res.zip", FileMode.Create))
{
zipMS.Seek(0, SeekOrigin.Begin);
zipMS.CopyTo(finalZipFileStream);
}
}
希望有人可以帮助我完成这项任务。
干杯