sambha
sambha

Reputation: 1353

Extracting a 7-Zip file "silently" - command line option

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

Upvotes: 36

Views: 81157

Answers (14)

Tony
Tony

Reputation: 11

You could always use the switches

-bsp0 Show no progress

-bso0 Show no output - except Errors

-bse0 Show no errors (You'll have to be confident in your exception handling to use that one)

-bd Show no progress bar

Upvotes: 0

Siva
Siva

Reputation: 21

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.

Upvotes: 0

Lassi
Lassi

Reputation: 3960

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.

Upvotes: 0

pjw
pjw

Reputation: 345

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

Upvotes: 2

Sandburg
Sandburg

Reputation: 871

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

Upvotes: 1

Fr0sT
Fr0sT

Reputation: 3025

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.

Upvotes: 2

Doug J. Huras
Doug J. Huras

Reputation: 647

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.

Upvotes: 4

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.

Upvotes: -1

Thierry_S
Thierry_S

Reputation: 1616

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

Upvotes: 3

Matthew
Matthew

Reputation: 461

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.

Upvotes: 46

Bobb Dizzles
Bobb Dizzles

Reputation: 539

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

Upvotes: 3

bruno777
bruno777

Reputation: 1896

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

Upvotes: 2

Sys
Sys

Reputation: 79

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

Upvotes: 7

Jerry Coffin
Jerry Coffin

Reputation: 490278

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

Upvotes: 12

Related Questions