2017-07-25 66 views
-2

我想使用c#壓縮文件夾中的文件和文件夾,而且我查看了以前提出的問題,但它們不適用於我......我目前正在嘗試與DotNetZip合作。以下是我的代碼:使用c#在一個文件夾中壓縮文件並返回響應

using (ZipFile zip = new ZipFile()) 
     { 
      string[] files = Directory.GetFiles(@"C:\Users\T1132\Desktop\logs"); 
      // add all those files to the logs folder in the zip file 
      zip.AddFiles(files, "logs"); 
      zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G"); 
      var a = System.DateTime.Now.ToString(); 
      zip.Save(@"C:\Users\T1132\Desktop\logs\Archiver.zip"); 
      foreach (var f in files) 
      { 
       System.IO.File.Delete(f); 
       System.Diagnostics.Debug.WriteLine(f + "will be deleted"); 
      } 
     } 

上面的代碼工作,但它只能壓縮文件並保留文件夾。請幫助我,謝謝。

+0

檢查[這個答案](https://stackoverflow.com/a/10643602/4685428) –

+2

請參閱[問]和[MCVE]如何提出好的問題。 DotNetZip遇到的具體問題是什麼?你有什麼嘗試?你卡在哪裏? –

回答

0

有很多方法可以使用DotNetZip壓縮文件。

using (ZipFile zip = new ZipFile()) 
{ 
    zip.Encoding = System.Text.Encoding.GetEncoding("big5"); // chinese 
    zip.AddDirectory(@"MyDocuments\ProjectX"); 
    zip.Save(ZipFileToCreate); 
} 

上面拉動一個目錄。對於更多的用途,你可以按照鏈接

https://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples

http://grapevine.dyndns-ip.com/download/authentec/download/dotnetzip/examples.htm

感謝

+0

感謝這到目前爲止不錯,但我想要在控制檯應用程序屏幕上彈出一個響應,有什麼想法? –

+0

你想在控制檯上做什麼? –

+0

我想要一個回覆,指定它是否成功 –

0

我使用這一點,它是偉大的工作。確保目錄具有足夠的權限。

System.IO.Compression.ZipFile.CreateFromDirectory(zipPath, path + strFileName + ".zip"); 
+0

它不適合我傷心。 –

+0

有沒有錯誤?如果是這樣,那是什麼? – summerGhost

+0

我已經明白了,謝謝你的幫忙@summerGhost –

0

您可以使用下面的代碼。

string zipFileName= "zip full path with extension"; 
      var files = Directory.GetFiles(" directory path"); 
     Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(); 
     foreach(var file in files) { 
      zip.AddFile(file); 
     } 
     zip.Save(zipFileName); 
相關問題