我想读取存储在 ZIP 文件中的文本文件。目前我使用 7Zip 提取所需的文件,读取它们并再次删除它们。有没有办法在不将它们解压到硬盘上的情况下读取它们?
是的,有办法。但是有什么好的方法吗?不,绝对不是。
有一种方法。但这在很大程度上取决于您的操作系统。PowerShell 5 具有Expand-Archive,这使得使用 7Zip 已过时,但即使使用,Expand-Archive您也必须提取整个存档才能读取文件的内容。
使用 Windows 机器,您可以使用shell.applicationCom Object 或system.io.compression.filesystem从该线程引用的类似对象完成它:
How to read content of a csv file inside zip file using PowerShell:
4. 还有一种使用原生方式的方法:
Run Code Online (Sandbox Code Playgroud)Add-Type -assembly "system.io.compression.filesystem" $zip = [io.compression.zipfile]::OpenRead("e:\E.zip") $file = $zip.Entries | where-object { $_.Name -eq "XMLSchema1.xsd"} $stream = $file.Open() $reader = New-Object IO.StreamReader($stream) $text = $reader.ReadToEnd() $text $reader.Close() $stream.Close() $zip.Dispose()
XMLSchema1.xsd 是您的文件名所在的位置。
链接的答案提到了其他一些方式(主要与外部依赖项和 Windows 操作系统相关联)。他们中的大多数仍然至少提取一个文件,但准确地说: