Problem. Review ways to use 7-Zip on the Windows console. You have 7za.exe and you want to compress, extract, and update files. Solution. Review this document. There are three parts: Tasks, Information, and Questions. This document is the original work of Dot Net Perls.
First you need to download the 7-Zip command line executable, 7za.exe. This is the exe you will use to run commands on archives. Go to 7-zip.org and get the "command line version. [Download - www.7-zip.org]
For convenience and so you don't need to change environment paths, put the 7za.exe file in your user directory. My user directory is C:\Users\Sam\.
Open the Windows console and test the 7za.exe program out with a few commands. Type in the exe name 7za and the first part will look like this.
7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19
Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...]
[<@listfiles...>]What that means. That's the grammar of the commands you use on 7za.exe. The first part "command" is the main verb. Then you specify optional switches, the archive name (either source or destination archives) and then files.
This command stands for 'archive' or 'add'. Use it to put files in an archive. You have to specify the destination archive, and the source files (in that order).
C:\Users\Sam>7za a -t7z files.7z *.txt 7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19 Scanning Creating archive files.7z Compressing file1.txt Compressing file2.txt Everything is Ok C:\Users\Sam>
Two files compressed. The directory C:\Users\Sam contains two files (file1.txt and file2.txt). The command puts those two files in an archive. The above command needs to be typed into the command prompt.
What we see. To open your archive, right click on it in the file manager and select 7-Zip -> Open archive. The screenshot shows the two text files compressed in files.7z.
This stands for 'delete' and is used much less often. It allows you to remove a certain file (or set of files) from inside an archive. You will need this if you use huge archives and need to save time. From the manual:
7z d archive.zip *.bak -r 7z: use executable d: delete files archive.zip: delete from this archive *.bak: only match bak files -r: traverse all subdirectories
Example uses. You can also remove only a single file from an archive with "d". This is much more useful when you do not have a solid archive. I find d to be of very limited use in normal situations.
"e" stands for extract, and it means to 'unzip' or expand an archive. You must specify the source archive always, and may also specify a destination. "e" extracts everything to a specified directory. Another command "x" can preserve directory structures in archives.
Overwrite prompts. 7-Zip will always prompt you if there is a file it needs to overwrite to extract the new file. This can be problematic if you are scripting or embedding 7za.exe. In that case, see the -y switch.
7z e archive.zip 7z: executable e: use extract command archive.zip: source archive you want to expand
The lowercase L is used to list the contents of archives and you probably will not need to use it often. I thought I would test it and show an example.
C:\Users\Sam>7za l files.7z
7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19
Listing archive: files.7z
Method = LZMA
Solid = +
Blocks = 1
Physical Size = 1202
Headers Size = 172
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2008-10-02 15:48:01 ....A 27216 1030 file1.txt
2008-10-02 15:47:45 ....A 3888 file2.txt
------------------- ----- ------------ ------------ ------------------------
31104 1030 2 files, 0 foldersWhat "l" does. This shows the listing of a solid archive. The original sizes of the files are 27216 bytes and 3888 bytes. They compress down to 1030 bytes.
This command allows you to test the integrity of archives. It stands for 'test' and is much less useful than the "-t" switch. Don't confuse the two. This one is used for diagnostics.
7z t archive.zip *.doc -r 7z: use this executable t: test the specified archive archive.zip: the archive you want to test *.doc: test all these files in the archive -r: recurse all child directories
Here U stands for update. This is a very useful command and is great when you want to replace old files in your archive with newer files. This prevents needing to decompress and recompress the entire archive.
Doesn't work with solid archives. A solid archive is one where all the files are compressed together. This means that you can't update specific files with the "u" command.
7z u archive.zip *.doc 7z: executable name u: update command archive.zip: archive you want to update files in *.doc: only update these files (Word documents)
This command is exactly like "e" except it preserves the full paths. If you have an elaborate or important directory structure, use this option. This would be most useful for system backups or really big backups. Here's the example syntax:
7z x archive.zip 7z: executable name x: use the extract command archive.zip: the archive you want to extract all the files from
This is the most important and useful option you can use. It specifies the method of compression. Here I will show a bunch of options, and also some examples both from my own work and from the manual.
| Compression switch | What it means |
| -mx0 | Don't compress at all. Is called "copy mode" and "fastest." |
| -mx3 | Fast compression mode. Will set various parameters automatically. |
| -mx5 | Same as above, but "normal." |
| -mx7 | "maximum" compression. |
| -mx9 | "ultra" compression. You probably want to use this. |
Here are a bunch more interesting compression method (-m) switches. The first three are of very limited use 99% of the time, but you might benefit from tweaking them.
| -m switch | What it does |
| -mfb | Specifies # of fast bytes. Sometimes help with very "sparse" files. Don't bother. |
| -mpass | Number of passes for deflate compression. Don't bother with this. Automatically set with levels. |
| -md | Specifies dictionary size. Automatically set, so don't bother. |
| -mmt | Enable multithreading. Use if: you have quad-core and a really huge archive. Specify "on" or "off". |
Here I show how you can specify the precise archive type you want to create. Note that you can specify any filename you want for any type. Some extensions are recommended, however.
| Type switch | Format | Example filename |
| -t7z | 7Z | archive.7z (default option) |
| -tgzip | GZIP | archive.gzip archive.gz |
| -tzip | ZIP | archive.zip (very compatible) |
| -tbzip2 | BZIP2 | archive.bzip2 |
| -ttar | TAR | tarball.tar (UNIX and Linux) |
| -tiso | ISO | image.iso |
| -tudf | UDF | disk.udf |
The 7-Zip manual provides some useful examples for type switches. It shows the -tiso and -tudf switches. These are not the most common. Almost all of the examples in this document (both original ones and the ones from the 7-Zip manual) use -t switches.
7z a -tiso archive.iso 7z a -tudf archive.udf 7z: executable name a: add to archive -tiso or -tudf: format of archive to create archive.iso or archive.udf: name of archive to create
7z is the only file format in 7-Zip that you can specify whether the archive is solid or not. Solid means all the files are compressed as one. It makes it impossible to use the "u" command to update individual files, among other commands.
| Solid switch | What it means |
| -ms=on | Enable solid mode. This is the default so you won't often need this. |
| -ms=off | Disable solid mode. This is useful when you need to update individual files. Will reduce compression ratios normally. |
You can change many values and switches on 7z archives, with endless permutations. Some things you can change are dictionary sizes, FastBytes values, MatchFinder values, and filters. Normally you don't need to deal with these.
With the 7z format, you can actually specify the algorithm used. PPMd is a very fast and effective algorithm for compressing plain text files. This is ideal for large collections of Word documents.
| PPMd switch | What it does |
| -mmem=24b -mmem=24k -mmem=24m | Control the amount of memory you use. Useful and higher is normally better. |
| -mo=2 -mo=32 | Specify model order in PPMd. Not normally useful. |
Here I will show the example compression commands from the 7-Zip manual. I demonstrated the most simple ones at the start of this document, and these are more complex.
7z a -tzip archive.zip *.jpg -mx0
7z: name of executable
a: add to archive command
-tzip: specify a ZIP archive (useful for compatibility)
archive.zip: destination archive
*.jpg: only add jpg files to archive
-mx0: don't compress, just copy
useful for already-compressed filesExample of 7z format. This next command line shows how you can create a solid 7z archive of program files (executables). It uses multithreading mode, which means it will be very fast on a dual core machine.
7z a -t7z archive.7z *.exe *.dll -ms -mmt 7z: name of executable a: archive command specified -t7z: use 7z file type (less compatible and smaller results) archive.7z: destination archive file *.exe: include all *.exe files in directory in new archive *.dll: include all *.dll files in new archive -ms: create solid archive (default) -mmt: multithread the operation (faster)
PPMd is an extraordinary algorithm for compressing text and is relatively new. Here I show a command in the 7-Zip manual that compresses all the text files in the working directory into a PPMd archive.
7z a -t7z archive.7z *.txt -m0=PPMd 7z: executable name/path a: add command specified -t7z: use the 7z format (needed for PPMd) archive.7z: destination archive file *.txt: select all text files -mo=PPMd: compress with this algorithm
Notes on above example. The above command is useful because you will normally want to only compress *.txt files with PPMd. (*.html and *.doc are useful too.) You need to use 7z to use PPMd.
Sometimes you do not want to extract to the current directory. This is where -o can come in handy. Use this to set the destination directory.
7z x archive.zip -oC:\Doc 7z: executable name x: extract archive with paths intact archive.zip: archive to extract files from -oC:\Doc: extract all files to the Doc folder on the C: drive
This is really helpful when security and encryption is involved. You can specify a password on the command line. The syntax is a bit funky, so the next couple examples might help.
7za a pw.7z *.txt -pSECRET 7za: name and path of 7-Zip executable a: add to archive pw.7z: name of destination archive *.txt: add all text files to destination archive -pSECRET: specify the password "SECRET"
Opening password-protected archives. This next console output shows what happens when you try to open the password-protected archive. The password here is SECRET, which will allow the archive to be extracted.
C:\Users\Sam>7za x pw.7z 7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19 Processing archive: pw.7z Enter password:
Header encryption. Add -mhe to encrypt headers. The password command will automatically deal with encrypted headers. Remember, encrypted headers will hide the names of the files in your archive.
Here I collect a bunch of switches that are of limited use to most people. They are useful to know, however, in case you ever need to use them.
| Switch | What it's for |
| -ssc | Specify case-sensitive mode. Useful for going between Linux and Windows. Default: -ssc- on Windows (insensitive) Default: -scc on Linux (sensitive) |
| -ssw | Compress locked files. Use if: you have problems with opening files. |
| -w | Set working directory. Use when you want to specify temp folders. |
For those of you who use both Linux and Windows, the case-sensitive option is useful. I will show my own example here with some explanation.
7za.exe a archive.7z Z*.* -ssc 7za.exe: 7-Zip command-line executable path and name a: archive command archive.7z: add files to this target archive Z*.*: select only files whose first letter is a capital Z
In data compression, a volume is a segment of a dataset that is a certain number of bytes long. The volume switch in 7za.exe allows you to specify the exact size in bytes, kilobytes, or megabytes. Additionally, you can specify sequential volumes.
This is a switch that specifies you want to overwrite old files. Be very careful here because you cannot restore an overwritten file normally. It takes another argument.
| Overwrite switch | Description |
| -aoa | Overwrite all destination files. |
| -aos | Skip over existing files without overwriting. You might use this for files where the earliest version is most important. |
| -aou | Avoid name collisions. New files extracted will have a number appending to their names. You will have to deal with them later. |
| -aot | Rename existing files. This will not rename the new files, just the old ones already there. Use when the new files are more important. |
7z x test.zip -aoa 7z: use the 7-zip executable x: use the extract command test.zip: extract files from this archive -aoa: overwrite all existing files. risky!
Use the "a" command and the wildcard * symbol. Specify the name of the destination archive file and the source files afterwards. Read more in the section Information: "a" command.
Use the "a" command and the wildcard * symbol, but specify the extension after the wildcard. *.txt means all text files. You can use the wildcard anywhere, even matching all files of a certain name with any extension.
Specify just the directory name. You do not need to use a wildcard at all. The 7-Zip manual helpfully shows this example, which specifies an entire directory called "subdir".
7z a -tzip archive.zip subdir\ 7z: use executable a: add to archive -tzip: use zip compression archive.zip: create this archive subdir\: source directory
It is probably a solid archive. 7z archives are by default solid archives, which mean all the files are compressed together. Change the archive to not be solid if you want to update it. (Search this page for "solid".)
You should use PPMd when you have a large corpus (body) of text. This could include HTML or other formatting, but plain text should dominate. My past research has shown that it can improve ratios by around 30% on some datasets.
Use the -y switch. This will assume a yes answer to all prompts. Use this only when you are really confident that you are not going to lose any data.
By using the "e" command and combining it with the -o switch. The syntax with -o is a bit funny so I will show the example from the 7-Zip help file. Here's how it works.
7z e archive.zip -oC:\soft *.cpp -r 7z: executable e: use extract command archive.zip: source archive you want to extract from -oC:\soft: the destination folder (-o is the switch and C:\soft is the argument) *.cpp: only extract cpp files (C++) -r: traverse all subdirectories
By specifying the -tgzip option for the type switch. Note: this makes a really great way to compress files on your web server for HTTP compression. [ASP.NET - Compression Benchmarks - dotnetperls.com]
Specify the -tbzip2 switch. This can be combined with any compression level in the above charts. The different modes in 7-Zip use many different settings, automatically.
By specifying the -t7z switch for type. Or you can simply omit the type switch and that will default to 7z. This format offers the greatest compression rations, but it doesn't work in all places.
Use the "l" command as shown above. You might want to use "l" in a utility that you run from a command line to make sure your batch archiving works properly.
Near the start I showed how to add files based on filters, but sometimes you want to exclude certain files manually. Use the -x switch (followed immediately with the filename). So if you want to exclude "file1.txt", use the switch "-xfile1.txt".
By using the -ao switch, described above. There are other options, and it is usually a better idea to use one of the renaming options (-aou or -aot).
Specify the -aos option, which means "skip overwriting files." This will cause 7za.exe to not copy the newer files out of the archive. Use if your files don't change over time and overwriting would just be a waste.
Read the 7-Zip manual about compression options. Note that you do not need to do this normally, as they are set automatically. I really recommend just using the -mx=0, -mx=3, -mx=5, -mx=7, and -mx=9 settings. An in-depth study would be fascinating.
You can change compression filters, which change behaviors on executable files such as *.exe and *.dll. You can enable header compression and encryption (-mhc=on and -mhe=on). Header compression is by default enabled.
You can embed 7-Zip in a Windows .NET program using the tutorial in my article about .NET 7-Zip. This yields the same great compression but in your own GUI. The link shows some compression ratios. [.NET 7-Zip Executable Tutorial - dotnetperls.com]
Yes, but the improvement is often very small, less than 1%. 7-Zip and AdvanceCOMP use the same Deflate encoder, but AdvanceCOMP has more options and is more fine-grained. [Advance Projects - AdvanceCOMP - advancemame.sourceforge.net]
This document was written by Sam Allen after he studied the 7-Zip help manual for many hours. See his work with 7za.exe embedded in C# programs. There are hundreds of other quality articles at his site. [Sam Allen - Dot Net Perls - dotnetperls.com]