2013-10-17 23 views
1

我想使用Ionic Zip在C#中打包一個目錄。通常我只是將使用這段代碼Ionic Zip - 將整個目錄保存在子目錄中並遍歷所有文件

 using (ZipFile pack = new ZipFile()) 
     {  
      pack.AddDirectory(defPackageCreationPath + "\\installfiles", "");    
      pack.Save(outputPath + "\\package.mpp"); 
     } 

這是工作正常,但是我需要通過每個文件itterate被包裝,以檢查它們的文件名字符,因爲我有包裝時被損壞了一些文件,如果它們包含特定的字符。

重要的是,要添加的目錄也包含子目錄,並且需要將這些目錄結轉到zip文件並在其中創建。

你能協助嗎?

+1

你能壓縮它們,然後重命名這些文件? http://msdn.microsoft.com/en-us/library/bb513869.aspx – Shawn

回答

1

不知道這是你在找什麼,但你可以很容易地獲得包括子目錄在內的所有文件的字符串數組。使用目錄類

像這樣

string[] Files = Directory.GetFiles(@"M:\Backup", "*.*", SearchOption.AllDirectories); 

foreach (string file in Files) 
{ 
    DoTests(file); 
} 

這將包括路徑文件。

您將需要System.IO;

using System.IO; 
0

您也可以嘗試這樣的事:

using (ZipFile pack = new ZipFile()) 
{ 
    pack.AddProgress += (s, eventArgs) => 
     { 
      // check if EventType is Adding_AfterAddEntry or NullReferenceException will be thrown 
      if (eventArgs.EventType == ZipProgressEventType.Adding_AfterAddEntry) 
      { 
       // Do the replacement here. 
       // eventArgs.CurrentEntry is the current file processed and 
       // eventArgs.CurrentEntry.FileName holds the file name 
       // 
       // Example: all files will begin with __ 
       eventArgs.CurrentEntry.FileName = "___" + eventArgs.CurrentEntry.FileName; 
      } 
     }; 

     pack.AddDirectory(defPackageCreationPath + "\\installfiles", "");    
     pack.Save(outputPath + "\\package.mpp"); 
    } 
} 
相關問題