Recently in the process of automating the use of 7z command-line tools, found its parameters in the path is very interesting, in this summary. The 7z version used for all demos in this article is: 15.14 x64.
Compress a folder
The following command packages all the files in the G:\temp\ directory and directory into the compressed file abc.zip.
7z.exe a ABC. Zip g:\temp\
Note that in this case, the temp directory is included in the compressed package.
Something interesting happens when we unzip the Abc.zip file. Either the "Extract all ..." provided by Windows in the right-click menu or the "Extract files ..." provided by the 7z will add the first-level directory, ABC, to the extracted files by default.
The extra level of this directory may be what we want, maybe not. If not, we can remove it from the UI interface above.
Or you can use the command line to extract it:
7z.exe x ABC. Zip –og:\
Can you believe that the extracted file path does not have the ABC level directory!
Compress all files under a folder
Because a directory named after a compressed file name is generated by default when decompressed through the UI, in order to not change the structure of the directory tree after decompression. We can consider that when compressing, only all files under the root directory are added without adding the root directory, and then the compressed file is named with the root directory name.
7z.exe a g:\temp. Zip g:\temp\*
At this point, the temp directory will not be included in the compressed package.
This way, the file path that we extracted by default through the UI is consistent with the pre-compression.
However, if you use the command line decompression you have to manually add the previous level of directory:
7z.exe x g:\temp. Zip –og:\temp
The next unexpected thing happens again when we use the relative directory to execute the compression command:
G:\> 7z.exe a g:\temp. Zip temp\*
The temp directory still appears in the compressed package. After querying the 7z document, it is not known, in this case, to use a relative path, you need to write:
G:\> 7z.exe a g:\temp. zip . \temp\*
Include the parent directory of the compressed directory in the compressed package
Another situation is that when we compress a directory, we want to save its parent directory information in a compressed package. Note that the compressed package contains only the parent directory and does not contain any other files in the parent directory. In this case, the relative directory must be used:
G:\>7z.exe a g:\temp. Zip temp\test\
This way, the root directory of the files in the compressed package is temp.
File list
When you execute a compression command, you can specify one or more files in addition to the specified directory. If you want to specify multiple files/directories, the best way is to write the names of these files/directories into a text file, and then use the text file as a command line argument. This text file is called a list of files and you need to add a special symbol ' @ ' before the file name when referencing it in the parameters.
G:\>7z.exe a g:\abc. Zip
-I means that the files and directories listed in the List.txt are added to the archive package.
You can write the absolute path to the file in the file list, or you can write a relative path. Note that if you write a relative path, it is relative to the current working directory's path, not the path relative to List.txt.
The file list can be either a single file path or a directory path. Note the relative path and absolute path are distinguished, and the rules are the same as described earlier.
In addition to containing the specified directory/file, you can also exclude the specified directory/file.
G:\>7z.exe a g:\temp. zip g:\temp\ [email protected]:\temp\list.txt
-X indicates that the files and directories listed in the List.txt file are not added in the compressed package.
Note: Only relative directories can be used in the exclusion list.
Paths in 7z command-line arguments