Compressing Files with 7zip CLI
Basic Compression
To compress a file using the 7-Zip CLI, you can use the following command:
7z a output_file.7z input_file
This command will create a new 7z archive named output_file.7z
containing the contents of input_file
.
You can also compress multiple files or directories by listing them after the a
command:
7z a output_file.7z file1.txt file2.txt directory/
Compression Levels and Methods
7-Zip offers different compression levels and methods to optimize the balance between file size and compression/decompression speed. You can specify the compression level using the -mx
option, where x
is a value between 0 (fastest, lowest compression) and 9 (slowest, highest compression).
For example, to create a 7z archive with the maximum compression level:
7z a -mx=9 output_file.7z input_file
7-Zip also supports various compression methods, such as LZMA, LZMA2, and PPMd. You can choose the compression method using the -m
option followed by the method name. For instance, to use the LZMA2 method:
7z a -m=LZMA2 output_file.7z input_file
Compression of Specific File Types
7-Zip can optimize the compression for certain file types. For example, to compress image files more efficiently, you can use the following command:
7z a -timage output_file.7z *.jpg *.png
The -timage
option tells 7-Zip to use the best compression settings for image files.
Similarly, you can use other file type specifiers, such as -tzip
for ZIP files, -ttgz
for GZIP-compressed TAR archives, and so on.
Compression of Encrypted Files
7-Zip also supports encryption of the compressed files. To create an encrypted 7z archive, use the -p
option followed by the desired password:
7z a -p"mypassword" output_file.7z input_file
The password will be used to encrypt the contents of the 7z archive.