7z command line compression and decompression detailed explanation - Chinese version

1 Introduction

7z, full name 7-Zip, is an open source software. It is currently recognized as the compression and decompression software with the largest compression ratio.

Homepage: http://www.7-zip.org/

Chinese homepage: http://7z.sparanoid.com/

Command line version download: http://7z.sparanoid.com/download.html

For Windows, go to the official website to download the installation package and install
linux using the command: sudo apt install p7zip-full
Note:
The difference between p7zip, p7zip-full and p7zip-rar: The difference
between p7zip and p7zip-full is that p7zip is a lighter version and only supports .7z , while the full version supports more 7z compression algorithms (for audio files, etc.).
The p7zip-rar package provides support for 7z RAR files, and installing p7zip-full should be sufficient in most cases.

The usage format of the 7z command line:  7z <command> [<switch>...] <base_archive_name> [<arguments>...]
here are two parameters that are required, command and base_archive_name, corresponding to the operation and the path of the generated compressed file (or decompressed file)

Main features:

# The new LZMA algorithm increases the compression ratio of the 7z format

# Supported formats:

* Compression/decompression: 7z, XZ, BZIP2, GZIP, TAR, ZIP

* Only compression: ARJ, CAB, CHM, CPIO, DEB, DMG, FAT, HFS, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, RAR, RPM, UDF, VHD, WIM, XAR, Z

2) exit code

0 : normal, no errors;

1 : warning, no fatal errors, such as some files are in use, not compressed;

2 : fatal error;

7: command line error;

8: Not enough memory;

255: The user stopped the operation;

3) Use the syntax

7z <command line> [<options>...] <base file name> [<parameter variables>...]

Expressions enclosed in square brackets (characters between "[" and "]") are optional.

The expressions inside the book title (characters between "<" and ">") are expressions that must be replaced (and the parentheses must be removed).

7-Zip supports wildcards similar to Windows:

"*" can use an asterisk instead of zero or more characters. 

"?" can replace a single character in the name with a question mark.

If only * is used, 7-Zip will treat it as all files of any extension.

4) Commands and examples

a Add files to archive.

7z a archive1.zip subdir\ : Add all files and subfolders under the subdir folder to archive1.zip, and the file names in archived1.zip include the subdir\ prefix.

7z a archive2.zip .\subdir\* : Add all files and subfolders under the subdir folder to archive1.zip, and the file names in archived2.zip do not contain the subdir\ prefix.

cd /D c:\dir1\ 

7z ac:\archive3.zip dir2\dir3\ : Filenames in archive3.zip will contain the dir2\dir3\ prefix, but not the c:\dir1 prefix.

7z a Files.7z *.txt -r: Add all txt files in the current folder and its subfolders to Files.7z.

b Test CPU speed and check memory errors.

d deletes files from the archive.

7z d archive.zip *.bak -r : Delete all bak files from archive.zip.

e Extract the files from the compressed archive into the current directory. Or to the specified output folder. The output folder setting can be changed with the -o (set output folder) option. This command will put all the extracted files into a folder. If you want to extract the file using the full path, you must use the x (extract full path) command. 

7z e archive.zip : extracts all files from the compressed archive archive.zip to the current folder.

7z e archive.zip -oc:\soft *.cpp : extracts *.cpp files from archive.zip to c:\soft folder.

l List the compressed archive contents.

7z l archive.zip: List the contents of the archive archive.zip.

t Tests the integrity of compressed archive files.

7z t archive.zip *.doc : Test the integrity of the *.doc files in the compressed archive archive.zip.

u Replace older files with newer files in compressed archives.

7z u archive.zip *.doc : Update *.doc files in the compressed archive archive.zip.

x Use the full path to extract the file from the archive in the current directory. Or to the specified output folder.

7z x archive.zip : Extracts all files from the compressed archive archive.zip to the current folder.

7z x archive.zip -oc:\soft *.cpp : extracts *.cpp files from archive.zip to c:\soft folder.

5) More options

-- In the command line, the option switch "-" after "--" is invalid. This allows files whose filenames start with "-" to be used on the command line.

7z t -- -ArchiveName.7z : Test the -ArchiveName.7z compressed archive.

-i Specifies to attach a file or a class of files when compressing. This option can add multiple types of attachments.

i[<recurse_type>]<file_ref> where <recurse_type> can be r[- | 0] (see -r later for the -r option), and <file_ref> can be @{listfile} | !{wildcard}.

7z a -tzip src.zip *.txt -ir!DIR1\*.cpp : Add *.txt files from the current directory, and *.cpp files in the DIR1 directory and its subdirectories to the src.zip archive.

-x Specifies that a file or class of files be excluded from the operation. This option excludes multiple types at the same time.

x[<recurse_type>]<file_ref> where <recurse_type> can be r[- | 0] (see -r later for the specific -r option), and <file_ref> can be @{listfile} | !{wildcard}.

7z a -tzip archive.zip *.txt -x!temp.* : Add all *.txt files except temp.* files to archive.zip.

-o Specifies the output folder for the extracted files. This option can only be used with release command.

7z x archive.zip -oc:\Doc : Extracts all files from archive.zip archive to c:\Doc folder.

-r Recurse subdirectories option.

-r Turn on recursive subdirectories. This option is used by default for e (extract), l (list), t (test), x (full path extract) commands that operate on archives. 

-r- Turn off recursive subdirectories. For a (add), d (delete), u (update) and all commands that need to scan disk files, this option will be used by default. 

-r0 Turn on recursive subdirectories. But only for wildcards. 

7z l archive.zip -r- *.doc : List *.doc files in the root directory of the archive.zip archive. 

7z a -tzip archive.zip -r src\*.cpp src\*.h : Add *.cpp and *.h files in the src directory and its subdirectories to the archive.zip archive.

-t Specifies the compressed archive format. Specifies the archive format. They can be in: zip, 7z, rar, cab, gzip, bzip2, tar or other formats. The default is the 7z format. 

7z a -tzip archive.zip *.txt : Add all *.txt files from the current directory to the compressed archive archive.zip in zip format.

-y Disables most prompts from 7-Zip when executing commands. You can use this option to suppress prompts for file overwrites in the e (extract) and x (extract full path) commands.

7z x src.zip -y : Extract all files from src.zip. All overwrite prompts will be blocked and all files with the same filename will be overwritten.

-v specifies the volume size.

{Size}[b | k | m | g] 

Specify the volume size, you can use bytes, KB (1 KB = 1024 bytes), MB (1 MB = 1024 KB) or GB (1 GB = 1024 MB). If you specify only {Size}, 7-zip will treat it as word. 

7z a a.7z *.txt -v10k -v15k -v2m : Create a.7z sub-volume compressed archive. The first volume is 10 KB, the second is 15 KB, and the rest are all 2 MB.

-p specifies the password.

7z x archive.zip -psecret : unpack all files in archive.zip with password "secret".

-ao Specifies how to overwrite existing files of the same name on hard disk during release.

Syntax: -ao[a | s | u]

-aoa directly overwrites existing files without any prompt. 

-aos Skip existing files, they will not be overwritten. 

-aou Automatically rename the freed file if a file with the same filename already exists. For example, the file file.txt will be automatically renamed to file_1.txt. 

-aot If a file with the same filename already exists, automatically rename the existing file. For example, the file file.txt will be automatically renamed to file_1.txt. 

7z x test.zip -aoa : extracts all files from archive test.zip and overwrites existing files without prompting.

-an Do not parse the archive_name field on the command line. This option must be used with the -i (append file) switch. For example if you use listing files for archives, you need to specify the -ai option, so you need to disable parsing of the archive_name field on the command line.

For examples, see the following -ai and -ax.

-ai Specifies additional files, including archive filenames and wildcards. This option allows multiple types to be attached at the same time.

ai[<recurse_type>]<file_ref> where <recurse_type> can be r[- | 0] (see -r later for the specific -r option), and <file_ref> can be @{listfile} | !{wildcard}.

7z t -an -air!*.7z : Test *.7z compressed files in the current directory and subdirectories.

-ax Specifies archives that must be excluded from the operation. This option excludes multiple types at the same time.

ax[<recurse_type>]<file_ref> where <recurse_type> can be r[- | 0] (see -r later for the specific -r option), and <file_ref> can be @{listfile} | !{wildcard}.

7z t -an -ai!*.7z -ax!a*.7z : test *.7z archives except a*.7z.

For more uncommon options, you can check the help.

For example: -m sets the compression algorithm;

-scs set the character set of the list file of files to be compressed;

-seml email zip file;

-sfx creates a self-release file;

-si reads data from standard input,

-so from output to standard output;

-slp set large memory mode;

-slt display technical information;

-ssc setting is case sensitive;

-ssw compresses the file being written;

-u update option.

 a test.zip ./test/* -mx0

Function: Compress all files in the test folder into zip format files in storage compression mode, and the compressed file is test.zip

Parameter Description:

a is to add option

-tZip for compressed format (-t7z...)

The specific parameters of the compression rate are as follows:

-mx0 only store without compression ----------------- the fastest, no compression, zip size is equal to the original folder

-mx1 extreme compression

-mx3 fast compression

-mx5 standard compression

-mx7 maximum compression

-mx9 extreme compression

Mandatory parameters: the most commonly used commands are a (compression) and x (decompression). Other commands, such as delete and decompression without retaining the directory structure, are generally not used.

Optional parameters: the most commonly used is -t{Type}

The default value of Type is 7z, and there are others such as:

Type        Format        Example filename

-t7z 7Z archive.7z (default)

-tgzip        GZIP        archive.gzip,archive.gz

-tzip ZIP archive.zip (compatible)

-tbzip2        BZIP2        archive.bzip2

-ttar TAR tarball.tar (Unix/Linux)

-tiso ISO image.iso (may not be supported)

-tudf        UDF        disk.udf

The commonly used ones are -t{Type} and -r, -o, -y

For example

7z.exe e -tgzip  *.gz

7z.exe and E:\Report\Process\Decote\*.zip -o E:\Report\Process\Decote\

Decompression command: 7z x -tzip -y xx-13.zip (unzip to the current directory, if you need to change the output directory, you need to use -o)

Compression command: 7z a -tzip -r xx.zip a\* b\*

Compress directory a and directory b into one xx.zip

For more details about parameters, please refer to here: http://www.dotnetperls.com/7-zip-examples

Under the Windows 64-bit operating system, in addition to 7z.exe, 7z.dll is also required. I package them into a zip file, otherwise you may encounter the above error when executing the command.

Guess you like

Origin blog.csdn.net/simon4055/article/details/130104993