7zip is award winning open source file archiver. Although mainly employ its own archive format – 7z format – it also support many other popular archive formats and can seamlessly work on them. 7zip claims to have the highest compression ratio. The application itself is distributed under LGPL license as a free software to use and available to Windows, Linux, and MacOS.
In this article we will discuss about basic tutorial on 7zip and use them on command line. We also cover installation from source code which can be applied to any other Linux distribution.
You might skip this chapter if you have installed 7zip before.
7zip installation is straightforward. First download the latest source code from here which is hosted on SourceForge. This version is used for Posix compliance Operating System. Save it on a working directory (create one if you don’t have it yet) and then extract it.
For compilation purpose, 7zip has shipped some Makefiles. Just running make command is enough to initiate compilation.
Here is the complete commands to do it:
tar -xf p7zip_9.20.1_src_all.tar.bz2 cd p7zip_9.20.1 make all3 make 7zG
At this point, we will have three new binaries: 7z, 7za, 7zr. As described by 7zip, 7z uses plugins (7z.so and Codecs/Rar29.so) to handle archives while 7z is stand-alone eecutable which handles less archive formats than 7z. The third is 7zr which is light stand-alone that supports only 7z/LZMA/BCJ/BCJ2.
The last command is used for building GUI front-end for 7zip.
Now come to installation. There are several methods provided by 7zip but we will use simple one. Edit ./install.sh and change DEST_HOME to suit your favorite path. For example, it can be “/usr/local” but you need root privileges to install into such location.
The following is the basic syntax for 7z:
7z [adeltux] [-][SWITCH] ...
Create an Archive
This can be done by using the function letter ‘a’. Here is the demonstration :
7z a basic.7z basic
Extract an Archive
This can be done using the function letter ‘e’. Using earlier basic.7z, here’s the command:
7z e basic.7z
The archive file will be extracted into same folder location
List Archive Details
Listing archive details means show metadata and list of content of the archive. It also gives us clue what archiving method and other useful information. This can be done by using the function letter ‘l’.
7z l basic.7z
Test Archive Integrity
This can be done using the function letter ‘t’. Here is an example :
7z t basic.7z basic
Updating means modify archive. This can be done using the function letter ‘u’. Here is an example :
7z u basic.7z basic
cp bufferoverflow.c basic/ 7z u basic.7z basic
Delete a file from the archive
This can be done using the function letter ‘d’ along with the switch -r. This switch tells the 7zip utility to traverse the subdirectories. Here is an example :
7z d basic.7z helloword -r
Let’s head to a bit complex usage.
7za -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archive.7z dir1
Here we want to archiving dir1 and use those switches as arguments. The compression using “ultra settings” which each switch has different meaning which can be seen here:
- Using 7z archive
- Using LZMA method
- The level of compression (level 9 = ultra)
- Number of fast bytes for LZMA = 64
- Dictionary size is 32 megabytes.
- Solid archive is on.
Although 7zip is handy, there are some limitation and something we should know.
As pointed by 7zip, 7zip does not store the owner/group of the file. Therefore, we should avoid using 7-zip format for backup purpose on Linux/Unix. Use tar instead. If you want to use 7zip, we should stage the compression.
tar cf - directory | 7za a -si directory.tar.7z
7za x -so directory.tar.7z | tar xfsoftware