需要搜索目录并遍历 zip 文件并阅读每个 [重复]
Posted
技术标签:
【中文标题】需要搜索目录并遍历 zip 文件并阅读每个 [重复]【英文标题】:Need to search a directory and loop through zip files and read through each [duplicate] 【发布时间】:2021-10-24 19:21:11 【问题描述】:我将有一个编号为 123456 的目录,其中我将有一个或多个版本 1、版本 2、版本 3 等的 zip 文件。我需要从最后一个 zip 文件中读取(在这种情况下,版本 3),并从该文件中获取一些信息。我把那部分写下来了(即,如果我解压缩文件并从目录中读取,我的代码会按照我想要的方式读取目录),但到目前为止我还无法从 zip 文件中读取。如果我能帮上忙,我不想真正提取任何东西。
Dim path = "c:/MyZipFiles/"
Dim id = 123456
Dim dirInfo = New DirectoryInfo(String.Format("01", path, id))
If IO.Directory.Exists(path) Then
Dim dirList = dirInfo.GetDirectories("Version *.zip", SearchOption.TopDirectoryOnly)
For Each dirInfo In dirList
' read the zip file from here - do I need a using statement?
Next
End If
【问题讨论】:
【参考方案1】:您可以使用 ZipFile.OpenRead 方法 (documentation) 返回一个 ZipArchive。在这里,您可以通过访问 Entries (documentation) 或 GetEntry (documentation) 方法获取所有内容或单个文件。
这是一个可以在您的 For/Each
循环中使用的示例:
Using archive = ZipFile.OpenRead(dirInfo)
For Each entry = archive.Entries()
Console.WriteLine(entry.FullName)
Next
End Using
【讨论】:
以上是关于需要搜索目录并遍历 zip 文件并阅读每个 [重复]的主要内容,如果未能解决你的问题,请参考以下文章