程式扎記: [Windows 技巧] 7-zip Command line 使用介紹

標籤

2011年1月11日 星期二

[Windows 技巧] 7-zip Command line 使用介紹

轉載自 這裡 
前言 : 
You have 7za.exe and you want to compress, extract, and update files. The 7-Zip compression utility provides superior compression and is open-source, making it easy to obtain and use. Review this document for ways to use 7-Zip on the Windows console. There are three general parts to the article: tasks, information, and questions. The most common tasks are listed first : 

- Get started with 7-Zip on the command line.
- Compress, extract, archive and optimize with 7-Zip.
- The executable is usually called 7za.exe.


Getting started with 7za.exe : 
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. For convenience and so you don't need to change environment paths, put the 7za.exe file in your user directory. 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 : 
 

Using the "a" command : 
Here we look at how you can use the "a" command with the single letter a. 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). The current directory contains three files (copying.txt, license.txt and readme.txt). The command puts those three files in an archive, and you need to type it into the command prompt : 
C:\Users\John-Lee\Downloads\7z>7za a -tzip files.zip *.txt

7-Zip (A) 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03
Scanning

Creating archive files.zip

Compressing copying.txt
Compressing license.txt
Compressing readme.txt

Everything is Ok

Using the "d" command : 
Here we see an example of the "d" command in 7-Zip command lines. 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 : 
C:\Users\John-Lee\Downloads\7z>7za d files.zip license.txt #從壓縮檔 files.zip 移掉 license.txt 檔案

7-Zip (A) 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03

Updating archive files.zip


Everything is Ok

Using the "e" command : 
Here we look at how you can use the "e" command in your console window. "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 : 
C:\Users\John-Lee\Downloads\7z>7za e -y files.zip # -y 指有相同檔案存在時, 直接覆蓋. 不給的話會需要在Console 給 yes/no. 適用於Automation.

7-Zip (A) 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03

Processing archive: files.zip

Extracting copying.txt
Extracting readme.txt

Everything is Ok

Files: 2
Size: 28222
Compressed: 10060

Using the "l" command : 
Here we see how you can use the single-letter "l" (lowercase letter ell) command. 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 : 
 

Using the "t" command : 
Here we use the "t" command in the 7z program. 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. 
 

Using the "u" command : 
Here we look at the "u" command in 7-Zip, which 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 : 
C:\Users\John-Lee\Downloads\7z>7za u files.zip copying.txt #更新 copying.txt 到壓縮檔

7-Zip (A) 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03

Scanning

Updating archive files.zip

Compressing copying.txt

Everything is Ok

Using the "x" command : 
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 : 
C:\Users\John-Lee\Downloads\7z>7za x -y files.zip # -y 指有相同檔案存在時, 直接覆蓋. 不給的話會需要在Console 給 yes/no. 適用於Automation.

7-Zip (A) 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03

Processing archive: files.zip

Extracting copying.txt
Extracting readme.txt

Everything is Ok

Files: 2
Size: 28226
Compressed: 10061

Using the -m switch : 
Here we look at how you can change the optimization settings in 7-Zip on the command line. 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 : 
 

Understanding solid archives : 
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. 
Switch: -ms=on
Function: Enable solid mode.
This is the default so you won't often need this.

Switch: -ms=off
Function: Disable solid mode.
This is useful when you need to update individual files.
Will reduce compression ratios normally.

Using the -o switch : 
Here we look at the "o" switch on the 7-Zip command line. 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 : 

2 則留言:

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!