尝试一下这个方法,在从源zip文件中提取文件后,您需要将目标zip文件读入ZipFile
对象,然后使用AddFiles
方法将源文件添加到目标文件中,最后保存。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ionic.Zip;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string myZip = @"C:\temp\test.zip";
string myOtherZip = @"C:\temp\anotherZip.zip";
string outputDirectory = @"C:\ZipTest";
using (ZipFile zip = ZipFile.Read(myZip))
{
zip.ExtractAll(outputDirectory, ExtractExistingFileAction.OverwriteSilently);
}
using (ZipFile zipDest = ZipFile.Read(myOtherZip))
{
zipDest.AddFiles((System.IO.Directory.EnumerateFiles(outputDirectory)),false,"");
zipDest.Save();
}
}
}
}
修改添加目录到ZipFile的方法(这适用于单个子目录级别)
using (ZipFile zipDest = ZipFile.Read(myOtherZip))
{
foreach (var dir in System.IO.Directory.GetDirectories(outputDirectory))
{
zipDest.AddFiles((System.IO.Directory.EnumerateFiles(dir)),false,outputDirectory ); //directory to the root
}
zipDest.AddFiles((System.IO.Directory.EnumerateFiles(outputDirectory)),false,""); //This will add the files to the root
zipDest.Save();
}
从Zip目录中删除文件的方法
List<string> files = zipDest.EntryFileNames.ToList<string>()
for (int i = 0
{
if(files[i].Contains("ZipTest")) //replace the directory you wish to delete files from here
zipDest.RemoveEntry(files[i])
}