Extracting a 7-Zip file “silently” - command line option

点点圈 提交于 2019-12-03 14:35:40

问题


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

回答1:


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).




回答2:


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.




回答3:


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




回答4:


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" ...



回答5:


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).




回答6:


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




回答7:


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.




回答8:


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



回答9:


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.




回答10:


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).




回答11:


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.



来源:https://stackoverflow.com/questions/3774278/extracting-a-7-zip-file-silently-command-line-option

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!