如何计算c中zipfile中的文件数
本文关键字:zipfile 文件 何计算 计算 | 更新日期: 2023-09-27 18:29:27
请告诉我如何计算ZIP文件中的文件数。
我需要一个C#代码来在visual studio中完成这项工作。当我在谷歌上搜索时,尝试了很多代码,但出现了一个错误:
找不到ZIPENTRY/ZIPFILE的命名空间或程序集。
有人能告诉我应该包括什么/需要安装什么/给我提供任何代码来计算文件数量吗?
正如MSDN所说(.Net 4.5),您可以使用ZipArchive和ZipFile类:
http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspxhttp://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx
System.IO.Compression命名空间中的类都在不同的程序集中System.IO.Compression和System.IO.Ccompression.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库,请参阅:
使用c#计数Zip文件中的文件数
您必须将引用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));
}
您可以使用DotNetZip尝试类似的操作。
using DotNetZip;
int count;
using (ZipFile zip = ZipFile.Read(path))
count = zip.Count;
我在这里找到了这个解决方案。
使用以下内容:
using var zip = new ZipArchive(stream, ZipArchiveMode.Read);
var totalFiles = zip.Entries.Where(x=>x.Length>0).Count()
文件夹没有任何长度