1

I am trying to create zip file by this code, but nothing works, the constractor of ZipFile doesnt get () only overloading with arguments, and i don't have the SAVE method? Whats wrong?

   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);
        }
4

1 回答 1

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);                    
}            

编辑:在某个目录中添加所有 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);                    
}            
于 2012-06-11T10:25:07.460 回答