2014-06-11 17 views
-3

我正在尋找一種方法來打開一個.7z文件,並通過使用C#代碼獲取壓縮內容的數量/數量。C#代碼 - 如何統計文件數量ia .7z

string[] directoryPaths = Directory.GetDirectories(@"heregoesmypath"); 

foreach (string dircetory in directoryPaths) 
{ 
    int fileCount = (from file in Directory.EnumerateFiles(@dircetory, "*", SearchOption.AllDirectories) select file).Count(); 

    string text = fileCount.ToString() + " Files of: " + "????" + " Files in Directory : " + dircetory; 

try 
{ 
    File.AppendAllText(_yourfile, text + Environment.NewLine + Environment.NewLine); 
} 

catch (Exception ex) 
{ 
    MessageBox.Show(ex.ToString()); 
} 
      string[] zippeddirectoryPaths = Directory.GetFiles(@"myresultpath"); 

      foreach (string exdircetory in zippeddirectoryPaths) 
       { 

        // FileInfo info = new FileInfo(exdircetory); 
        // var test = info.Length; 
        // MessageBox.Show(test.ToString() + "Bytes"); 
        // var test2 = info.Extension; 
        // MessageBox.Show(test2.ToString() + "Format");} 
        // var test3 = Empty; 
        // here must be a methode, which count my amount of files on the 
        // .7z file and save it in var test3 

這是我到目前爲止的代碼...而不是最後5行bevore我需要得到梅索德在.7z壓縮文件中的文件的數量。

以手動的方式,你將有一個文件,你做一個正確的klick去7-Zip和klick打開。 7-Zip用戶界面打開併爲您提供按鈕屬性。如果你點擊那個按鈕,另一個窗口打開並顯示文件和文件夾的數量/數量。

因此,如果我已經壓縮了很多文件和文件夾,我根本不願意手動做這件事。我喜歡在C#中獲得一種方式將我的結果保存在一個var中,以便稍後在我的代碼中使用它們來編寫日誌記錄。

所以請給我建議或示例代碼來解決這個問題。

+1

發佈一些代碼,你已經完成的工作。 – Ricky

+0

至少在尋求幫助之前進行嘗試。 –

回答

0

您可以使用7z SDK直接從您的本土工具調用7z函數。可以使用7z commandline interface並評估文本輸出。

調用示例:

7z.exe l Repository.7z > list.txt 

... 

結果舉例:

7-Zip 9.22 beta Copyright (c) 1999-2011 Igor Pavlov 2011-04-18 

Listing archive: Repository.7z 

-- 
Path = Repository.7z 
Type = 7z 
Method = LZMA 
Solid = + 
Blocks = 1 
Physical Size = 976726 
Headers Size = 22453 

    Date  Time Attr   Size Compressed Name 
------------------- ----- ------------ ------------ ------------------------ 
2011-08-31 15:57:36 ....A  663225  954273 Repository\codelists\version 0031\codelists.zip 
2010-08-30 18:06:23 ....A  30936    Repository\...\map_ALOCAT_EG4003_Child.xml 
2010-10-04 16:38:02 ....A  30936    Repository\...\map_ALOCAT_EG4003_Child.xml 
..... 
2011-09-09 10:40:14 D....   0   0 Repository\ALOCAT\EDIFACT 
2011-09-09 10:56:25 D....   0   0 Repository\ALOCAT 
2011-09-09 10:46:13 D....   0   0 Repository 
------------------- ----- ------------ ------------ ------------------------ 
          130869837  954273 895 files, 2167 folders 
+0

結果看起來非常好,但它看起來好像它沒有保存在一個變量中用於進一步的代碼,也不會發布任何C#代碼... 我也更新我的問題,並顯示你一點點我已經創建的代碼。 如果你正在使用C#代碼進行調用,那麼你很有禮貌地發佈它,並告訴我你正在使用哪個引用?! 參考7z SDK我需要找到一個很好的tuorial whick解釋如何安裝和使用它在我的C#請求。有什麼建議麼? – user3730009

+0

上面顯示的示例輸出由7z.exe生成,該命令行工具隨附7-zip一起提供。沒有涉及C#編碼。這個想法是,從程序中調用7z.exe並解釋輸出列表可能會更容易。這可以幫助您安裝和理解7z SDK。 –