Extracting a 7-Zip file "silently" - command line option
Asked Answered
E

13

36

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
Efflorescent answered 22/9, 2010 at 23:9 Comment(0)
U
12

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

Upholsterer answered 22/9, 2010 at 23:24 Comment(3)
Thanks. That helped. I tried subprocess.Popen. But then my parent process is done before the child returns. I would like to check the returncode of the child before continuing. Am going through docs but let me know if you know how to stall (especially with stdout=PIPE) off hand.Efflorescent
What's the equivalent solution on Windows? #5966563...Constantan
subprocess.PIPE, see the examples #25079640. Look also at 7Za return codes sevenzip.sourceforge.jp/chm/cmdline/exit_codes.htm from the subprocess object - can be handy.Interlineate
G
45

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.

Generality answered 24/7, 2012 at 11:18 Comment(2)
nice solution. if you don't like Igor like I do, you can remove hom too with: ... some_archive.7z | FIND /V "ing " | FIND /V "Igor Pavlov"Acord
It works as expected, but it can be improved. In the newer version of 7-Zip v 19.00 you could add the command -bsp0 and will remove the status bar progress and will have a good impact on performance. Example command: 7-Zip_19.0\7za.exe -bsp0 x MyArchive.7z -oMyArchiveOutputPathChildbearing
U
12

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

Upholsterer answered 22/9, 2010 at 23:24 Comment(3)
Thanks. That helped. I tried subprocess.Popen. But then my parent process is done before the child returns. I would like to check the returncode of the child before continuing. Am going through docs but let me know if you know how to stall (especially with stdout=PIPE) off hand.Efflorescent
What's the equivalent solution on Windows? #5966563...Constantan
subprocess.PIPE, see the examples #25079640. Look also at 7Za return codes sevenzip.sourceforge.jp/chm/cmdline/exit_codes.htm from the subprocess object - can be handy.Interlineate
C
7

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

Canister answered 26/10, 2012 at 20:9 Comment(0)
R
4

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.

Raasch answered 1/12, 2017 at 18:46 Comment(0)
A
3

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" ...
Alphosis answered 3/12, 2012 at 19:24 Comment(0)
A
3

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

Abcoulomb answered 28/3, 2017 at 14:17 Comment(0)
H
2

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

Hylophagous answered 24/7, 2012 at 11:41 Comment(0)
G
2

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.

Gelatinoid answered 19/9, 2018 at 8:13 Comment(0)
P
2

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

Personable answered 16/10, 2019 at 22:33 Comment(0)
B
1

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
Bluestocking answered 19/9, 2018 at 7:25 Comment(0)
L
0

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.

Lian answered 13/9, 2020 at 13:7 Comment(0)
O
0

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.

Outfoot answered 13/10, 2020 at 15:54 Comment(0)
O
-1

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.

Orvil answered 17/4, 2018 at 20:3 Comment(1)
Here we are speaking of normal display 17% 1000 + directory\file_name.ext , not a question to the user. Is your solution for the same problem?Bluestocking

© 2022 - 2024 — McMap. All rights reserved.