我正在尝试通过此代码创建zip文件,但没有任何作用,ZipFile的约束器无法获取
()仅使用参数重载,而我没有SAVE
方法?
什么错了?
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("C://inetpub//wwwroot//Files//Wireframes//" + url, zip.Name);
zip.AddDirectory("C://inetpub//wwwroot//Files//Wireframes//" + url);
zip.Save(downloadFileName);
}
答案 0 :(得分:1)
要使用SharpZipLib压缩整个目录,您可以尝试以下方法:
private void ZipFolder(string folderName, string outputFile)
{
string[] files = Directory.GetFiles(folderName);
using (ZipOutputStream zos = new ZipOutputStream(File.Create(outputFile)))
{
zos.SetLevel(9); // 9 = highest compression
byte[] buffer = new byte[4096];
foreach (string file in files)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
zos.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int byteRead;
do
{
byteRead = fs.Read(buffer, 0,buffer.Length);
zos.Write(buffer, 0, byteRead);
}
while (byteRead > 0);
}
}
zos.Finish();
zos.Close();
}
正如您所看到的,我们的代码与您的示例完全不同 正如我在上面的评论中所说,你的例子似乎来自DotNetZip 如果您希望使用该库,您的代码将是:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("C://inetpub//wwwroot//Files//Wireframes//" + url);
zip.AddDirectory("C://inetpub//wwwroot//Files//Wireframes//" + url, "WireFrames");
zip.Save(downloadFileName);
}
编辑:在某个目录中添加al PNG文件
using (ZipFile zip = new ZipFile())
{
string filesPNG = Directory.GetFiles("C://inetpub//wwwroot//Files//Wireframes//" + url, "*.PNG);
foreach(string file in filesPNG)
zip.AddFile(file);
zip.Save(downloadFileName);
}