Sie sind auf Seite 1von 9

7-Zip Command-Line Examples

7-Zip is an amazing compression program. The 7za.exe program is used to compress,


extract, and update files through the command line. It provides superior compression and is
open-sourcethis makes it easy to obtain and use. There are many ays to use 7-!ip on the
"indos console.
Key points: #et started ith 7-!ip on the command line. $ompress, extract, archive and
optimize ith 7-!ip. The executable is usually called 7za.exe.
Get started
%irst you need to donload the 7-!ip command line executable, 7za.exe. This is the exe you
ill use to run commands on archives. #o to 7-zip.org and get the &command line version.
%or convenience and so you don't need to change environment paths, put the 7za.exe file in
your user directory. (pen the "indos console and test the 7za.exe program out ith a fe
commands. Type in the exe name 7za and the first part ill look like this.
7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavov 2008-08-19
!"age# 7$a %co&&a'() *%"+itche")..., %archive-'a&e) *%.ie-'a&e")...,
*%/i"t.ie"...),
What that means. That's the grammar of the commands you use on 7za.exe. The first part
&command& is the main verb. Then you specify optional sitches, the archive name )either
source or destination archives* and then files. +y user directory is $,-.sers-/am-.
Example "a" command
0ere e look at ho you can use the &a& command ith the single letter a. This command
stands for 'archive' or 'add'. .se it to put files in an archive. 1ou have to specify the
destination archive, and the source files )in that order*. The directory $,-.sers-/am contains
to files )file2.txt and file3.txt*. The command puts those to files in an archive, and you
need to type it into the command prompt.
C#0!"er"01a&)7$a a -t7$ .ie".7$ 2.t3t
7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavov 2008-08-19
1ca''i'g
Creati'g archive .ie".7$
Co&pre""i'g .ie1.t3t
Co&pre""i'g .ie2.t3t
4verythi'g i" 56
C#0!"er"01a&)
Description of the screenshot. To open your archive, right click on it in the file manager
and select 7-!ip -4 (pen archive. The screenshot shos the to text files compressed in
files.7z.
Example "d" command
0ere e see an example of the &d& command in 7-!ip command lines. This stands for 'delete'
and is used much less often. It allos you to remove a certain file )or set of files* from inside
an archive. 1ou ill need this if you use huge archives and need to save time. %rom the
manual,
7$ ( archive.$ip 2.ba6 -r
7$# 7"e e3ec7tabe
(# (eete .ie"
archive.$ip# (eete .ro& thi" archive
2.ba6# o'y &atch ba6 .ie"
-r# traver"e a "7b(irectorie"
Example uses. 1ou can also remove only a single file from an archive ith &d&. This is much
more useful hen you do not have a solid archive. I find d to be of very limited use in normal
situations.
Example "e" command
0ere e look at ho you can use the &e& command in your console indo. &e& stands for
extract, and it means to 'unzip' or expand an archive. 1ou must specify the source archive
alays, and may also specify a destination. &e& extracts everything to a specified directory.
5nother command &x& can preserve directory structures in archives.
7$ e archive.$ip
7$# e3ec7tabe
e# 7"e e3tract co&&a'(
archive.$ip# "o7rce archive yo7 +a't to e3pa'(
Oer!rite prompts. 7-!ip ill alays prompt you if there is a file it needs to overrite to
extract the ne file. This can be problematic if you are scripting or embedding 7za.exe. In
that case, see the -y sitch.
Example "l" command
0ere e see ho you can use the single-letter &l& )loercase letter ell* command. The
loercase 6 is used to list the contents of archives and you probably ill not need to use it
often. I thought I ould test it and sho an example.
C#0!"er"01a&)7$a .ie".7$
7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavov 2008-08-19
8i"ti'g archive# .ie".7$
9etho( : 8Z9A
1oi( : ;
<oc6" : 1
Phy"ica 1i$e : 1202
=ea(er" 1i$e : 172
>ate ?i&e Attr 1i$e Co&pre""e( @a&e
------------------- ----- ------------ ------------ ------------------------
2008-10-02 1A#48#01 ....A 27216 10B0 .ie1.t3t
2008-10-02 1A#47#4A ....A B888 .ie2.t3t
------------------- ----- ------------ ------------ ------------------------
B1104 10B0 2 .ie"C 0 .o(er"
Description of the output. This shos the listing of a solid archive. The original sizes of the
files are 37327 bytes and 8999 bytes. They compress don to 2:8: bytes.
Example "t" command
0ere e use the &t& command in the 7z program. This command allos you to test the
integrity of archives. It stands for 'test' and is much less useful than the &-t& sitch. ;on't
confuse the to. This one is used for diagnostics.
7$ t archive.$ip 2.(oc -r
7$# 7"e thi" e3ec7tabe
t# te"t the "peci.ie( archive
archive.$ip# the archive yo7 +a't to te"t
2.(oc# te"t a the"e .ie" i' the archive
-r# rec7r"e a chi( (irectorie"
Example "u" command
0ere e look at the &u& command in 7-!ip, hich stands for update. This is a very useful
command and is great hen you ant to replace old files in your archive ith neer files.
This prevents needing to decompress and recompress the entire archive.
7$ 7 archive.$ip 2.(oc
7$# e3ec7tabe 'a&e
7# 7p(ate co&&a'(
archive.$ip# archive yo7 +a't to 7p(ate .ie" i'
2.(oc# o'y 7p(ate the"e .ie" (Dor( (oc7&e't")
"ro#lem !ith "u". The &u& command doesn't ork ith solid archives. 5 solid archive is one
here all the files are compressed together. This means that you can't update specific files
ith the &u& command.
Example "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 ould be most useful for system
backups or really big backups. 0ere's the example syntax,
7$ 3 archive.$ip
7$# e3ec7tabe 'a&e
3# 7"e the e3tract co&&a'(
archive.$ip# the archive yo7 +a't to e3tract a the .ie" .ro&
Example -m s!itch
0ere e look at ho you can change the optimization settings in 7-!ip on the command line.
This is the most important and useful option you can use. It specifies the method of
compression. 0ere I ill sho a bunch of options, and also some examples both from my
on ork and from the manual.
Table that shows compression options
Co&pre""io' "+itch# -&30
Dhat it &ea'"# >o'Et co&pre"" at a.
I" cae( Fcopy &o(e.F
Co&pre""io' "+itch# -&31
Dhat it &ea'"# Gery o+ co&pre""io'.
It i" cae( F.a"te"tF &o(e.
Co&pre""io' "+itch# -&3B
Dhat it &ea'"# Ha"t co&pre""io' &o(e.
Di "et vario7" para&eter" a7to&aticay.
Co&pre""io' "+itch# -&3A
Dhat it &ea'"# 1a&e a" aboveC b7t F'or&a.F
Co&pre""io' "+itch# -&37
Dhat it &ea'"# F&a3i&7&F co&pre""io'.
Co&pre""io' "+itch# -&39
Dhat it &ea'"# F7traF co&pre""io'.
(Io7 probaby +a't to 7"e thi".)
$ore a#out -m% s!itches
0ere are a bunch more interesting compression method )-m* sitches. The first three are of
very limited use the vast ma<ority of the time, but you might benefit from teaking them. +y
experience is that manual optimizations to these options doesn't produce big benefits.
Table that shows -m switches
1+itch# -&.b
H7'ctio'# 1peci.ie" J o. .a"t byte".
1o&eti&e" hep +ith very F"par"eF .ie".
>o'Et bother.
1+itch# -&pa""
H7'ctio'# @7&ber o. pa""e" .or (e.ate co&pre""io'.
>o'Et bother +ith thi".
A7to&aticay "et +ith eve".
1+itch# -&(
H7'ctio'# 1peci.ie" (ictio'ary "i$e.
A7to&aticay "etC "o (o'Et bother.
1+itch# -&&t
H7'ctio'# 4'abe &7tithrea(i'g.
!"e i.# yo7 have K7a(-core a'( a reay h7ge archive.
1peci.y Fo'F or Fo..F.
?hi" &ay be e'abe( by (e.a7tL chec6 the hep .ie.
Example -t type s!itches
0ere I sho ho you can specify the precise archive type you ant to create. =ote that you
can specify any filename you ant for any type. /ome extensions are recommended,
hoever.
Table that shows compression format switches
?ype "+itch# -t7$
Hor&at# 7Z
43a&pe .ie'a&e# archive.7$ ((e.a7t optio')
?ype "+itch# -tg$ip
Hor&at# MZIP
43a&pe .ie'a&e# archive.g$ip
archive.g$
?ype "+itch# -t$ip
Hor&at# ZIP
43a&pe .ie'a&e# archive.$ip (very co&patibe)
?ype "+itch# -tb$ip2
Hor&at# <ZIP2
43a&pe .ie'a&e# archive.b$ip2
?ype "+itch# -ttar
Hor&at# ?AN
43a&pe .ie'a&e# tarba.tar (!@IO a'( 8i'73)
?ype "+itch# -ti"o
Hor&at# I15
43a&pe .ie'a&e# i&age.i"o
?ype "+itch# -t7(.
Hor&at# !>H
43a&pe .ie'a&e# (i"6.7(.
&#out -t s!itch usa'e
The 7-!ip manual provides some useful examples for type sitches. It shos the -tiso and
-tudf sitches. These are not the most common. 5lmost all of the examples in this document
)both original ones and the ones from the 7-!ip manual* use -t sitches.
7$ a -ti"o archive.i"o
7$ a -t7(. archive.7(.
7$# e3ec7tabe 'a&e
a# a(( to archive
-ti"o or -t7(.# .or&at o. archive to create
archive.i"o or archive.7(.# 'a&e o. archive to create
(olid archies
7z is the only file format in 7-!ip that you can specify hether the archive is solid or not.
/olid means all the files are compressed as one. It makes it impossible to use the &u&
command to update individual files, among other commands.
1+itch# -&":o'
H7'ctio'# 4'abe "oi( &o(e.
?hi" i" the (e.a7t "o yo7 +o'Et o.te' 'ee( thi".
1+itch# -&":o..
H7'ctio'# >i"abe "oi( &o(e.
?hi" i" 7"e.7 +he' yo7 'ee( to 7p(ate i'(ivi(7a .ie".
Di re(7ce co&pre""io' ratio" 'or&ay.
$ore a#out 7) archies
1ou can change many values and sitches on 7z archives, ith endless permutations. /ome
things you can change are dictionary sizes, %ast>ytes values, +atch%inder values, and filters.
=ormally you don't need to deal ith these.
""$d compression al'orithms
"ith the 7z format, you can actually specify the algorithm used. ??+d is a very fast and
effective algorithm for compressing plain text files. This is ideal for large collections of "ord
documents. In many cases, ??+d does not perform as ell on files containing binary data.
PP9( "+itch# -&&e&:24b
-&&e&:246
-&&e&:24&
H7'ctio'# Co'tro the a&o7't o. &e&ory yo7 7"e.
!"e.7 a'( higher i" 'or&ay better.
PP9( "+itch# -&o:2
-&o:B2
H7'ctio'# 1peci.y &o(e or(er i' PP9(.
@ot 'or&ay 7"e.7.
Example compression methods
0ere I ill sho the example compression commands from the 7-!ip manual. I demonstrated
the most simple ones at the start of this document, and these are more complex.
7$ a -t$ip archive.$ip 2.Ppg -&30
7$# 'a&e o. e3ec7tabe
a# a(( to archive co&&a'(
-t$ip# "peci.y a ZIP archive (7"e.7 .or co&patibiity)
archive.$ip# (e"ti'atio' archive
2.Ppg# o'y a(( Ppg .ie" to archive
-&30# (o'Et co&pre""C P7"t copy
7"e.7 .or area(y-co&pre""e( .ie"
Example of 7) format. This next command line shos ho you can create a solid 7z
archive of program files )executables*. It uses multithreading mode, hich means it ill be
very fast on a dual core machine.
7$ a -t7$ archive.7$ 2.e3e 2.( -&" -&&t
7$# 'a&e o. e3ec7tabe
a# archive co&&a'( "peci.ie(
-t7$# 7"e 7$ .ie type (e"" co&patibe a'( "&aer re"7t")
archive.7$# (e"ti'atio' archive .ie
2.e3e# i'c7(e a 2.e3e .ie" i' (irectory i' 'e+ archive
2.(# i'c7(e a 2.( .ie" i' 'e+ archive
-&"# create "oi( archive ((e.a7t)
-&&t# &7tithrea( the operatio' (.a"ter)
*reate ""$d archie
??+d is an extraordinary algorithm for compressing text and is relatively ne. 0ere I sho a
command in the 7-!ip manual that compresses all the text files in the orking directory into
a ??+d archive. The command is useful because you ill normally ant to only compress
@.txt files ith ??+d. )@.html and @.doc are useful too.* 1ou need to use 7z to use ??+d.
??+d $ompression >enchmark in 7-!ip
7$ a -t7$ archive.7$ 2.t3t -&0:PP9(
7$# e3ec7tabe 'a&eQpath
a# a(( co&&a'( "peci.ie(
-t7$# 7"e the 7$ .or&at ('ee(e( .or PP9()
archive.7$# (e"ti'atio' archive .ie
2.t3t# "eect a te3t .ie"
-&o:PP9(# co&pre"" +ith thi" agorith&
Example -o s!itch
0ere e look at the &o& sitch on the 7-!ip command line. /ometimes you do not ant to
extract to the current directory. This is here -o can come in handy. .se this to set the
destination directory.
7$ 3 archive.$ip -oC#0>oc
7$# e3ec7tabe 'a&e
3# e3tract archive +ith path" i'tact
archive.$ip# archive to e3tract .ie" .ro&
-oC#0>oc# e3tract a .ie" to the >oc .o(er o' the C# (rive
Example -p s!itch
0ere e look at ho you can use the &-p& sitch, hich refers to the ord &passord&. This
is really helpful hen security and encryption is involved. 1ou can specify a passord on the
command line. The syntax is a bit funky, so the next couple examples might help.
7$a a p+.7$ 2.t3t -p14CN4?
7$a# 'a&e a'( path o. 7-Zip e3ec7tabe
a# a(( to archive
p+.7$# 'a&e o. (e"ti'atio' archive
2.t3t# a(( a te3t .ie" to (e"ti'atio' archive
-p14CN4?# "peci.y the pa""+or( F14CN4?F
Openin' pass!ord-protected archies. This next console output shos hat happens
hen you try to open the passord-protected archive. The passord here is /A$BAT, hich
ill allo the archive to be extracted.
C#0!"er"01a&)7$a 3 p+.7$
7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavov 2008-08-19
Proce""i'g archive# p+.7$
4'ter pa""+or(#
+eader encryption. 5dd -mhe to encrypt headers. The passord command ill
automatically deal ith encrypted headers. Bemember, encrypted headers ill hide the
names of the files in your archive.
$ore s!itches
0ere e take a closer look at many sitches that are of limited use to most people. They are
useful to kno, hoever, in case you ever need to use them. .sually you can do better <ust
by using the defaults that are slightly ad<usted for your reCuirement.
1+itch# -""c
H7'ctio'# 1peci.y ca"e-"e'"itive &o(e.
!"e.7 .or goi'g bet+ee' 8i'73 a'( Di'(o+".
>e.a7t# -""c- o' Di'(o+" (i'"e'"itive)
>e.a7t# -"cc o' 8i'73 ("e'"itive)
1+itch# -""+
H7'ctio'# Co&pre"" oc6e( .ie".
!"e i.# yo7 have probe&" +ith ope'i'g .ie".
1+itch# -+
H7'ctio'# 1et +or6i'g (irectory.
!"e +he' yo7 +a't to "peci.y te&p .o(er".
*ase-sensitie example
0ere e look at ho you can use case-insensitive file names in the 7-!ip command line. %or
those of you ho use both 6inux and "indos, the case-sensitive option is useful. I ill sho
my on example here ith some explanation.
7$a.e3e a archive.7$ Z2.2 -""c
7$a.e3e# 7-Zip co&&a'(-i'e e3ec7tabe path a'( 'a&e
a# archive co&&a'(
archive.7$# a(( .ie" to thi" target archive
Z2.2# "eect o'y .ie" +ho"e .ir"t etter i" a capita Z
(!itch -
0ere e note ho you can use the &v& sitch on the command line. In data compression, a
volume is a segment of a dataset that is a certain number of bytes long. The volume sitch
in 7za.exe allos you to specify the exact size in bytes, kilobytes, or megabytes. 5dditionally,
you can specify seCuential volumes.
Example -ao s!itch
0ere e look at the &ao& sitch, hich allos you to specify hether you to to overrite old
files. >e very careful here because you cannot restore an overritten file normally. It takes
another argument. +ake sure to back up your data by copying the files in your file manager
first.
Table that shows -ao switches
1+itch# -aoa
5ver+rite a (e"ti'atio' .ie".
1+itch# -ao"
16ip over e3i"ti'g .ie" +itho7t over+riti'g.
!"e thi" .or .ie" +here the earie"t ver"io' i" &o"t i&porta't.
1+itch# -ao7
Avoi( 'a&e coi"io'".
@e+ .ie" e3tracte( +i have a '7&ber appe'(i'g to their 'a&e".
(Io7 +i have to (ea +ith the& ater.)
1+itch# -aot
Ne'a&e e3i"ti'g .ie".
?hi" +i 'ot re'a&e the 'e+ .ie"C P7"t the o( o'e" area(y there.
!"e +he' the 'e+ .ie" are &ore i&porta't.
Example of the switches
7$ 3 te"t.$ip -aoa
7$# 7"e the 7-$ip e3ec7tabe
3# 7"e the e3tract co&&a'(
te"t.$ip# e3tract .ie" .ro& thi" archive
-aoa# over+rite a e3i"ti'g .ie". ri"6yR
,uestions
There are many more possibilities and usages of the 7-!ip program on the command line in
both "indos and 6inux. This section ansers some Cuestions I had hen doing this
research, and also some Cuestions that you may have.
+o! do - add many files to one archie. .se the &a& command and the ildcard @
symbol. /pecify the name of the destination archive file and the source files afterards. Bead
more in the section Information, &a& command.
+o! do - add many files !ith a specific extension. .se the &a& command and the
ildcard @ symbol, but specify the extension after the ildcard. @.txt means all text files. 1ou
can use the ildcard anyhere, even matching all files of a certain name ith any extension.
+o! can - add many files from an entire su#directory. /pecify <ust the directory name.
1ou do not need to use a ildcard at all. The 7-!ip manual helpfully shos this example,
hich specifies an entire directory called &subdir&.
7$ a -t$ip archive.$ip "7b(ir0
7$# 7"e e3ec7tabe
a# a(( to archive
-t$ip# 7"e $ip co&pre""io'
archive.$ip# create thi" archive
"7b(ir0# "o7rce (irectory
Why can/t - update my archie. It is probably a solid archive. 7z archives are by default
solid archives, hich mean all the files are compressed together. $hange the archive to not
be solid if you ant to update it. )/earch this page for &solid&.*
When should - use ""$d. 1ou should use ??+d hen you have a large corpus )body* of
text. This could include 0T+6 or other formatting, but plain text should dominate. +y past
research has shon that it can improve ratios by around 8:D on some datasets.
+o! can - stop 7-Zip from promptin' me. .se the -y sitch. This ill assume a yes
anser to all prompts. .se this only hen you are really confident that you are not going to
lose any data.
+o! can - specify the output directory. >y using the &e& command and combining it ith
the -o sitch. The syntax ith -o is a bit funny so I ill sho the example from the 7-!ip
help file. 0ere's ho it orks.
7$ e archive.$ip -oC#0"o.t 2.cpp -r
7$# e3ec7tabe
e# 7"e e3tract co&&a'(
archive.$ip# "o7rce archive yo7 +a't to e3tract .ro&
-oC#0"o.t# the (e"ti'atio' .o(er (-o i" the "+itch a'( C#0"o.t i" the arg7&e't)
2.cpp# o'y e3tract cpp .ie" (C;;)
-r# traver"e a "7b(irectorie"
+o! do - use GZip compression. >y specifying the &-tgzip& option for the type sitch.
=ote, this makes a really great ay to compress files on your eb server for 0TT?
compression. /ee &Is this document compressedE& section belo.
+o! do - use 0Zip1. 1ou can use >!ip3 by specifying the &-tbzip3& sitch. This can be
combined ith any compression level in the above charts. The different modes in 7-!ip use
many different settings, automatically.
+o! do - use 7) format. >y specifying the &-t7z& sitch for type. (r you can simply omit
the type sitch and that ill default to 7z. This format offers the greatest compression
rations, but it doesn't ork in all places.
+o! can - see !hat/s inside an archie. .se the &l& command as shon above. 1ou
might ant to use &l& in a utility that you run from a command line to make sure your batch
archiving orks properly.
+o! can - exclude certain files. =ear the start e sa ho to add files based on filters,
but sometimes you ant to exclude certain files manually. .se the -x sitch )folloed
immediately ith an exclamation mark and then the filename*. /o if you ant to exclude
&file2.txt&, use the sitch &-xFfile2.txt&. It it important to remember to use the exclamation
mark as a separator beteen the -x part and the filename.
+o! can - replace files already on dis2 !ith ne! files. >y using the -ao sitch,
described above. There are other options, and it is usually a better idea to use one of the
renaming options )-aou or -aot*.
+o! can - i'nore extractin' files already on dis2. /pecify the -aos option, hich means
&skip overriting files.& This ill cause 7za.exe to not copy the neer files out of the archive.
.se if your files don't change over time and overriting ould <ust be a aste.
+o! can - chan'e internal settin's. Bead the 7-!ip manual about compression options.
=ote that you do not need to do this normally, as they are set automatically. I really
recommend <ust using the -mxG:, -mxG8, -mxGH, -mxG7, and -mxGI settings. 5n in-depth
study ould be fascinating.
What alues can - chan'e in the internals. 1ou can change compression filters, hich
change behaviors on executable files such as @.exe and @.dll. 1ou can enable header
compression and encryption )-mhcGon and -mheGon*. 0eader compression is by default
enabled.
+o! can - em#ed 7-Zip. 1ou can embed 7-!ip in a "indos .=AT program using the
tutorial in my article about .=AT 7-!ip. This yields the same great compression but in your
on #.I. The link shos some compression ratios.
7-!ip Axecutable Tutorial
*an - use &dance*O$" to improe compression ratios. 1es, but the improvement is
often very small, less than 2D. 7-!ip and 5dvance$(+? use the same ;eflate encoder, but
5dvance$(+? has more options and is more fine-grained.
Besource page
*redits: $orrections by ?aul /mith and +atthe >ruce /mith.
(ummary
"e sa ays you can invoke 7-!ip on the command line, providing superior compression
ith an open-source tool. "e sa ays to create ne archives, add to existing archives, use
different formats for compression, and use various strengths of compression. I suggest you
try the different options, balancing time reCuired for compression and strength of
compression.
$ompression Tips

Das könnte Ihnen auch gefallen