[英]how to count number of files in zipfile in c#
請告訴我如何計算ZIP文件中的文件數。
我需要C#代碼才能在Visual Studio中完成這項工作。 我在Google搜索時嘗試了許多代碼,但收到一條錯誤消息:
找不到ZIPENTRY / ZIPFILE的名稱空間或程序集。
誰能告訴我我應該包含的內容/需要安裝的所有內容/為我提供用於計數文件數量的任何代碼?
正如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和System.IO.Compression.FileSystem中 。
因此您可以在項目中添加對System.IO.Compression和System.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庫,請參見:
您必須將引用System.IO.Compression和System.IO.Compression.FileSystem添加到您的項目
using (var archive = System.IO.Compression.ZipFile.Open(filePath, ZipArchiveMode.Read))
{
var count = archive.Entries.Count(x => !string.IsNullOrWhiteSpace(x.Name));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.