“静默”提取 7-Zip 文件- 命令行选项

发布于 2024-09-24 17:19:26 字数 261 浏览 10 评论 0原文

我想在 Python 脚本中提取 7-Zip 存档。它工作得很好,除了它会吐出提取细节(在我的例子中这是巨大的)。

有没有办法在提取时避免这些冗长的信息?我没有找到 7z.exe 的任何“静默”命令行选项。

我的命令是

7z.exe -o some_dir x some_archive.7z

I want to extract a 7-Zip archive in a Python script. It works fine except that it spits out the extraction details (which is huge in my case).

Is there a way to avoid this verbose information while extracting? I did not find any "silent" command line option to 7z.exe.

My command is

7z.exe -o some_dir x some_archive.7z

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

随风而去 2024-10-01 17:19:26

我刚刚在搜索相同内容时遇到了这个问题,但我自己解决了!假设使用 Windows/DOS 处理该命令,一个更简单的解决方案是将命令更改为:

7z.exe -o some_dir x some_archive.7z > nul

即,将输出定向到空文件而不是屏幕。

或者您可以将输出通过管道传输到 DOS“find”命令,以仅输出特定数据,也就是说,

7z.exe -o some_dir x some_archive.7z | FIND "ing archive"

这只会产生以下输出。

创建存档 some_archive.7z

更新存档 some_archive.7z**


我的最终解决方案是将命令更改为

... some_archive.7z | FIND /V "ing  "

“ing”后注意双空格。这导致了以下输出。

7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18

Scanning

Updating some_archive.7z


Everything is Ok

这消除了单个文件处理,但生成整体操作的摘要,无论操作类型如何。

I just came across this when searching for the same, but I solved it myself! Assuming the command is processed with Windows / DOS, a simpler solution is to change your command to:

7z.exe -o some_dir x some_archive.7z > nul

That is, direct the output to a null file rather than the screen.

Or you could pipe the output to the DOS "find" command to only output specific data, that is,

7z.exe -o some_dir x some_archive.7z | FIND "ing archive"

This would just result in the following output.

Creating archive some_archive.7z

or

Updating archive some_archive.7z**


My final solution was to change the command to

... some_archive.7z | FIND /V "ing  "

Note double space after 'ing'. This resulted in the following output.

7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18

Scanning

Updating some_archive.7z


Everything is Ok

This removes the individual file processing, but produces a summary of the overall operation, regardless of the operation type.

面如桃花 2024-10-01 17:19:26

一种可能是使用 popen 生成子进程,因此它的输出将返回到父级进行处理/显示(如果需要),否则完全被忽略(使用 stdout=PIPEstderr 创建您的 popen 对象=PIPE 以便能够检索子级的输出)。

One possibility would be to spawn the child process with popen, so its output will come back to the parent to be processed/displayed (if desired) or else completely ignored (create your popen object with stdout=PIPE and stderr=PIPE to be able to retrieve the output from the child).

醉南桥 2024-10-01 17:19:26

正如他们所说,要隐藏大部分屏幕填充消息,您可以使用... some_archive.7z | FIND /V“正在压缩”,但“FIND”也会删除包含该单词的错误消息。你不会被警告。由于更新的 7-zip 版本,“FIND”也可能需要更改。

7-zip 具有强制详细输出、无静默模式、混合 stderr 和 stdout(*)、不保存 Unix 权限等。与例如,“tar+bzip2”或“zip”。

(*) “上游(伊戈尔·巴甫洛夫)不想对消息进行不同的输出,尽管他已被多次要求这样做:(” http://us. Generation-nt.com/answer/bug-346463-p7zip-stdout-stderr-help-166693561 .html - “伊戈尔·巴甫洛夫不想改变这种行为”http://sourceforge.net/tracker/?func=detail&aid=1075294&group_id=111810&atid=660493

Like they said, to hide most of the screen-filling messages you could use ... some_archive.7z | FIND /V "Compressing" but that "FIND" would also remove the error messages that had that word. You would not be warned. That "FIND" also may have to be changed because of a newer 7-zip version.

7-zip has a forced verbose output, no silence mode, mixes stderr and stdout(*), doesn't save Unix permissions, etc. Those anti-standards behaviors together put "7-zip" in a bad place when being compared to "tar+bzip2" or "zip", for example.

(*) "Upstream (Igor Pavlov) does not want to make different outputs for messages, even though he's been asked several times to do so :(" http://us.generation-nt.com/answer/bug-346463-p7zip-stdout-stderr-help-166693561.html - "Igor Pavlov does not want to change this behaviour" http://sourceforge.net/tracker/?func=detail&aid=1075294&group_id=111810&atid=660493

捶死心动 2024-10-01 17:19:26

如果您从 Powershell 运行 7-zip.exe,并且只想查看错误,那么您可以尝试如下操作:

7-zip.exe u <Target> <Source> | Select-String "Error" -Context 10

这只会显示“错误”消息行和周围的 10 行(或任何数字)捕获错误特定的输出。

If you're running 7-zip.exe from Powershell, and you only want to see errors, then you could try something like this:

7-zip.exe u <Target> <Source> | Select-String "Error" -Context 10

This will only display the "Error" message line and the surrounding 10 lines (or whatever number) to capture the error specific output.

几度春秋 2024-10-01 17:19:26

7zip 没有用于命令行提取的显式“安静”或“静默”模式。

一种可能性是使用 popen 生成子进程,因此其输出将返回到父进程进行处理/显示(如果需要),否则完全被忽略(使用 stdout=PIPE 和 stderr=PIPE 创建 popen 对象,以便能够检索孩子的输出)。

否则尝试这样做:

%COMSPEC% /c "%ProgramFiles%\7-Zip\7z.exe" ...

7zip does not have an explicit "quiet" or "silent" mode for command line extraction.

One possibility would be to spawn the child process with popen, so its output will come back to the parent to be processed/displayed (if desired) or else completely ignored (create your popen object with stdout=PIPE and stderr=PIPE to be able to retrieve the output from the child).

Otherwise Try doing this:

%COMSPEC% /c "%ProgramFiles%\7-Zip\7z.exe" ...
对你再特殊 2024-10-01 17:19:26

扩展@Matthew的答案和这个答案https://superuser。 com/questions/194659/how-to-disable-the-output-of-7-zip
我使用 FINDSTR 而不是 find,因此我可以链接多行来排除和空行:

7za.exe a test1.zip .\foldertozip | FINDSTR /V /R /C:"^Compressing  " /C:"Igor Pavlov" /C:"^Scanning$" /C:"^$" /C:"^Everything is Ok$"
  • /V: 排除
  • /R: 正则表达式
  • /C:"^Compressing " : 行开头,压缩,2 个空格
  • /C:" ^Scanning$" :单词扫描在一行上(开始/结束)
  • /C:"^$" :开始和结束之间没有任何内容,即一个空行

我使用 /C 以便space 是一个空格,否则它是要排除的多个单词之间的分隔符,就像在这个更简单的版本中一样:(

FINDSTR /V "Compressing Pavlov Scanning Everytyhing"

存在相同的警告,如果新版本中的措辞发生变化,或者如果有用的行以单词“压缩”开头,它将没有按预期工作)。

Expanding on @Matthew 's answer and this answer https://superuser.com/questions/194659/how-to-disable-the-output-of-7-zip
I'm using FINDSTR instead of find so I can chain multiple lines to exclude and blank lines as well:

7za.exe a test1.zip .\foldertozip | FINDSTR /V /R /C:"^Compressing  " /C:"Igor Pavlov" /C:"^Scanning$" /C:"^$" /C:"^Everything is Ok$"
  • /V: exclude
  • /R: regex
  • /C:"^Compressing " : begining of line, Compressing, 2 spaces
  • /C:"^Scanning$" : the word Scanning on its own on a line (begining/end)
  • /C:"^$" : a begining and end without anything in between, ie, a blank line

I'm using /C so that a space is a space, otherwise it's a separator between multiple words to exlude as in this simpler version:

FINDSTR /V "Compressing Pavlov Scanning Everytyhing"

(the same caveats exist, if the wording changes in a new version, or if a useful line starts with the word "Compressing ", it will not work as expected).

明天过后 2024-10-01 17:19:26

<代码>| FIND 是一个很好的替代方案,可以在不显示无关紧要的文本的情况下显示发生的情况。

The | FIND is a good alternative to show what happened without displaying insignificant text.

人│生佛魔见 2024-10-01 17:19:26

检查 7zip 源代码我发现隐藏的 -ba 开关似乎可以解决问题。不幸的是它还没有完成。我设法通过对源代码的多次修改使其工作,但这只是一个 hack。如果有人感兴趣,选项变量称为 options.EnableHeaders ,并且需要在 CPP/7zip/UI/Console/Main.cpp 文件中进行更改。
或者,您也可以在 tracker 中联系 7Zip 的作者来完成该功能。对此有多个请求,其中之一位于此处

Examining 7zip source I found hidden -ba switch that seems to do the trick. Unfortunately it is not finished. I managed to make it work with several modifications of sources but it's just a hack. If someone's interested, the option variable is called options.EnableHeaders and changes are required in CPP/7zip/UI/Console/Main.cpp file.
Alternatively you can poke 7Zip's author to finish the feature in tracker. There are several requests on this and one of them is here.

对你的占有欲 2024-10-01 17:19:26

仅显示最后 4 行...

7z x -y some_archive.7z | tail -4

给我:

Everything is Ok

Size:       917519
Compressed: 171589

开关 -y 是对所有内容回答“是”(在我的情况下覆盖现有文件)。

To show just the last 4 lines...

7z x -y some_archive.7z | tail -4

gives me:

Everything is Ok

Size:       917519
Compressed: 171589

The switch -y is to answer yes to everything (in my case to override existing files).

听不够的曲调 2024-10-01 17:19:26

7-zip没有这样的选项。另外,在每个压缩文件中打印的行应该显示在同一位置,而无需换行,从而擦除前一个,这具有很酷的效果。不幸的是,在某些情况下(詹金斯......)它产生了几行☹️淹没控制台。

NUL(Windows)可能是一种解决方案。

7-zip.exe -o some_dir x some_archive.7z>NUL

7-zip has not such an option. Plus the lines printed at each file compressed are supposed to display at the same spot without newline, erasing the previous one, which has a cool effect. Unfortunatly, in some contexts (Jenkins...) it produced several lines ☹️ flooding the console.

NUL (windows) is maybe one solution.

7-zip.exe -o some_dir x some_archive.7z>NUL
翻了热茶 2024-10-01 17:19:26

在类 Unix 操作系统(Linux、BSD 等)上,shell 命令 7z ... >/dev/null 将丢弃由 7z 写入到 <强>标准输出。这应该涵盖 7z 编写的所有状态/信息消息。

似乎7z将错误消息写入标准错误,因此如果您执行>/dev/null,错误消息仍然会显示。

On Unix-like operating systems (Linux, BSD, etc.) the shell command 7z ... >/dev/null will discard all text written by 7z to standard output. That should cover all the status/informational messages written by 7z.

It seems that 7z writes error messages to standard error so if you do >/dev/null, error messages will still be shown.

话少心凉 2024-10-01 17:19:26

正如上面 Fr0sT 所说, -ba 开关仅输出有效的内容(至少在我尝试的列表选项中)。
7z.exe l archive_name.zip

7z.exe l -ba archive_name.zip

有很大的不同,特别是用于解析脚本中的输出。
无需修改任何内容,只需使用 version19 中的 -ba 开关即可。这一点上面也有人说过了。我将作为答案,因为我无法发表评论。

As told by Fr0sT above, -ba switch outputs only valid things (at least in list option on which I was trying).
7z.exe l archive_name.zip

7z.exe l -ba archive_name.zip

made great difference, esp for parsing the output in scripts.
There is no need to modify anything, just use -ba switch in version19. This was also told bysomeone above. I'm putting as answer as I can't comment.

挥剑断情 2024-10-01 17:19:26

您可以使用 -y 开关来阻止 7-Zip 显示提示。这将对所有提示回答“是”。仅当您有信心时才使用此功能。

You can stop 7-Zip from displaying prompts by using the -y switch. This will answer yes to all prompts. Use this only when you are confident.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文