尝试一下,从源 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)); //This will add them as a directory
zipDest.AddFiles((System.IO.Directory.EnumerateFiles(outputDirectory)),false,""); //This will add the files to the root
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>(); // Get List of all the archives files
for (int i = 0; i < files.Count; i++)
{
if(files[i].Contains("ZipTest")) //replace the directory you wish to delete files from here
zipDest.RemoveEntry(files[i]);
}