繁体   English   中英

在存档 7z 中打开文件以读取 Powershell

[英]Open file in archive 7z to read in Powershell

我需要在 7z 存档中打开包含巨大备份文件和日志文件的日志文件。 我需要通过读取日志文件的最后几行来确认备份是否成功。 我找到了如何使用 zip 文件执行此操作的解决方案:

 Add-Type -assembly "system.io.compression.filesystem"
$zip = [io.compression.zipfile]::OpenRead("C:\Users\USER\Documents\Documents.zip")
$file = $zip.Entries | where-object { $_.Name -eq "backup.log"}
$stream = $file.Open() 


$text = Get-Content $file -tail 5
$text

#$reader.Close()
$stream.Close()
$zip.Dispose()

如何使用 7zip 存档做同样的事情?

只需使用 7z 命令行工具并将其传递给正确的 arguments、 https://superuser.com/questions/321829/extract-a-certain-file-from-an-archive-with-7-zip-from -the-command-line对此有一些详细信息。 按照那里的说明使用 7z cmd 行工具从存档中提取该日志,然后使用您现在拥有的相同结构对其进行解析。

https://sevenzip.osdn.jp/chm/cmdline/index.htm详细介绍了如何使用 7z 命令行工具。

在你的情况下,这 7z 部分应该是这样的:

$PathtoExecutable\7z.exe e C:\Users\USER\Documents\Documents.7z -oC:\users\user\temp\ backup.log

只需将 PathtoExecutable 替换为您机器上 7z 的实际路径,并按照 7z 文档对其他路径执行相同操作。 然后,您将在指定为 output 目录的任何目录中获取 .log 文件,之后您应该可以使用 Get-Content 对其进行解析。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM