Let's say you want to compress a file with gzip. That's easy:
gzip foo.txt
Now foo.txt is gone and you have a (presumably smaller) foo.txt.gz instead.
You've heard that 7-zip can produce files in gzip format, but with better compression. What would an equivalent command look like if you were to use 7z?
7z is an archiver, not a compressor. As far as it is concerned, gzip is an archive format that can only store a single file. To create a .gz file, we need to add a file to it:
7z a -tgzip foo.txt.gz foo.txt
The -tgzip flag tells 7z to create an "archive" in gzip format. (It is technically redundant here because 7z can derive the type of the archive from its filename, but I like to be explicit.)
However, this leaves foo.txt untouched. To delete the input file after the .gz file has been written successfully, pass -sdel:
7z a -tgzip -sdel foo.txt.gz foo.txt
This works! But unlike gzip, 7z is very chatty by default and outputs all kinds of nonsense like "Copyright (c) 1999-2023 Igor Pavlov" or "Scanning the drive" or "Everything is Ok". To shut it up, you add a few more flags:
7z a -tgzip -sdel -bso0 -bsp0 foo.txt.gz foo.txt
This tells 7z to redirect regular output ("o") and progress ("p") messages to stream "0", which is not a real stream, but suppresses all output. Error messages are left at their default setting, -bse2, which means they are written to stream "2" (standard error).
Finally, you realize that maybe you didn't have to switch to a completely different program. You could have told gzip to expend more effort to compress better:
gzip -9 foo.txt
But 7z also has tunable compression parameters. Cranking everything to the max for gzip gives:
7z a -tgzip -sdel -bso0 -bsp0 -mx=9 -mfb=258 -mpass=15 foo.txt.gz foo.txt
#gzip #7zip