我有文件存储在多个不同的文件夹中,我需要从这些文件夹中的所有文件制作ZIP存档。我使用System.IO.Compression创建了一个简单的函数,它只从一个文件夹获取数据并制作ZIP存档,但我无法弄清楚如何为多个文件夹执行此操作。 ZIP中不需要文件夹,只需要文件中的文件。
如果在这个库中无法完成,我可以使用不同的DotNetZip或类似的。
string folder1 = @"c:\ex\ZipFolder1";
string zipPath = @"c:\ex\AllFiles.zip";
ZipFile.CreateFromDirectory(folder1, zipPath);
我想你正在使用DotNetZip
.Just创建一个ZipFile
并通过方法AddFiles添加文件
using (var file = new ZipFile())
{
//fileNames is an array containing the paths of the files from differents folders
file.AddFiles(fileNames);
file.Save(zipFile);
}
Have you tried using AddFile() method of ZipFile Class. Here you can replace 'PathOfFiles' dynamically as per your requirement.
var fileNames = new string[] { "a.txt", "b.xlsx", "c.png" };
using (var zip = new ZipFile()){
foreach (var file in fileNames)
zip.AddFile(@"PathOfFiles\" + fileName, "");
zip.Name = "ZipFile";
var pushStreamContent = new PushStreamContent((stream, content, context) =>
{
zip.Save(stream);
stream.Close();
}, "application/zip");
}