2013-08-28 37 views
2

請告訴我如何統計ZIP文件中的文件數量。如何統計cfile中zipfile文件的數量

我需要一個C#代碼在Visual Studio中完成這項工作。當我谷歌搜索但嘗試了很多代碼,但得到一個錯誤說:

未找到ZIPENTRY/ZIPFILE命名空間或程序集。

任何人都可以告訴我我應該包括什麼/需要安裝什麼/提供給我任何代碼來計算文件數量?

+0

見http://stackoverflow.com/questions/15241889/i-didnt-find-zipfile-class-in-the-system-io - 壓縮命名空間 – Vadim

+0

謝謝vadmin ...我也在這裏搜索,但couldnt得到這個鏈接 – praveen

+0

可能的重複[計數一個Zip文件與C#中的文件數量](http://stackoverflow.com/questions/4785391/count-文件數量在與一個c-sharp) –

回答

-1
int count; 
using (ZipFile zip = ZipFile.Read(path)) 
count = zip.Count; 

你可以嘗試這樣的事情!

你可以參考this

+1

對不起,但根據MSDN http://msdn.microsoft.com/en-us/library/system.io.compr ession.zipfile.aspx ZipFile是一個靜態類,沒有ZipFile zip = ...有可能 –

+0

你是否從[this](http://stackoverflow.com/a/4785405/21567)得到了這段代碼?如果是這樣,請指出(並添加關於「DotNetZip」的部分,至少使您的代碼有效,關於@ DmitryBychenko的評論) –

3

由於MSDN所說的那樣(.NET 4.5),您可以使用ZipArchive的ZipFile類:無論是在

http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx

類是System.IO.Compression命名空間在不同的程序集中 System.IO.Compression and System.IO.Compression.FileSystem雖然。

所以你可能集添加引用System.IO.CompressionSystem.IO.Compression.FileSystem到您的項目,並嘗試這樣的事:

... 
using System.IO.Compression; 
... 


    // Number of files within zip archive 
    public static int ZipFileCount(String zipFileName) { 
    using (ZipArchive archive = ZipFile.Open(zipFileName, ZipArchiveMode.Read)) { 
     int count = 0; 

     // We count only named (i.e. that are with files) entries 
     foreach (var entry in archive.Entries) 
     if (!String.IsNullOrEmpty(entry.Name)) 
      count += 1; 

     return count; 
    } 
    } 

另一種可能性是使用DotNetZip庫,請參閱:

Count number of files in a Zip File with c#

0

您必須添加引用System.IO.CompressionSystem.IO.Compression.FileSystem到項目

using (var archive = System.IO.Compression.ZipFile.Open(filePath, ZipArchiveMode.Read)) 
{ 
    var count = archive.Entries.Count(x => !string.IsNullOrWhiteSpace(x.Name)); 
} 
相關問題