命令行压缩解压


一 7z

1) 简介
7z,全称7-Zip, 是一款开源软件。是目前公认的压缩比例最大的压缩解压软件。
主页:http://www.7-zip.org/
中文主页:http://7z.sparanoid.com/
命令行版本下载:http://7z.sparanoid.com/download.html
主要特征:
# 全新的LZMA算法加大了7z格式的压缩比
# 支持格式:
* 压缩 / 解压缩:7z, XZ, BZIP2, GZIP, TAR, ZIP
* 仅解压缩:ARJ, CAB, CHM, CPIO, DEB, DMG, FAT, HFS, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, RAR, RPM, UDF, VHD, WIM, XAR, Z

2)退出代码
0 : 正常,没有错误;
1 : 警告,没有致命的错误,例如某些文件正在被使用,没有被压缩;
2 : 致命错误;
7 : 命令行错误;
8 : 没有足够的内存;
255 : 用户停止了操作;

3)使用语法
7z <命令行> [<选项>...] <基本档案名称> [<参数变量>...]

在方括号内的表达式(“[” 和 “]”之间的字符)是可选的。
在书名号内的表达式(“<” 和 “>”之间的字符)是必须替换的表达式(而且要去掉括号)。

7-Zip 支持和 Windows 相类似的通配符:
“*”可以使用星号代替零个或多个字符。
“?”可以用问号代替名称中的单个字符。
如果只用*,7-Zip 会将其视为任何扩展名的全部文件。

4)命令及实例

a 添加文件到压缩档案。
7z a archive1.zip subdir\  :增加subdir文件夹下的所有的文件和子文件夹到archive1.zip中,archived1.zip中的文件名包含subdir\前缀。
7z a archive2.zip .\subdir\* :增加subdir文件夹下的所有的文件和子文件夹到archive1.zip中,archived2.zip中的文件名不包含subdir\前缀。
cd /D c:\dir1\
7z a c:\archive3.zip dir2\dir3\  :archiive3.zip中的文件名将包含dir2\dir3\前缀,但是不包含c:\dir1前缀。
7z a Files.7z *.txt -r  : 增加当前文件夹及其子文件夹下的所有的txt文件到Files.7z中。

b 测试 CPU 运行速度及检查内存错误。

d 从压缩档案删除文件。
7z d archive.zip *.bak -r  :从archive.zip中删除所有的bak文件。

e 从压缩档案中释放文件到当前目录中。或者到指定的输出文件夹。输出文件夹设置可以通过 -o (设置输出文件夹) 选项来更改。此命令会将所有被释放的文件放置到一个文件夹。如果您想使用完整路径释放文件,您必须使用 x (完整路径释放) 命令。
7z e archive.zip :从压缩档案 archive.zip 中释放所有文件到当前文件夹。
7z e archive.zip -oc:\soft *.cpp :从压缩档案 archive.zip 中释放 *.cpp 文件到 c:\soft 文件夹。

l 列出压缩档案内容。
7z l archive.zip :列出压缩档案 archive.zip 的内容。

t 测试压缩档案文件的完整性。
7z t archive.zip *.doc  :在压缩档案 archive.zip 中测试 *.doc 文件的完整性。

u 在压缩档案文件中使用较新的文件替换掉较旧的文件。
7z u archive.zip *.doc :在压缩档案 archive.zip 中更新 *.doc 文件。

x 在当前目录中,使用完整路径从压缩档案中释放文件.或者到指定的输出文件夹。

7z x archive.zip :从压缩档案 archive.zip 中释放所有文件到当前文件夹。
7z x archive.zip -oc:\soft *.cpp :从压缩档案 archive.zip 中释放 *.cpp 文件到 c:\soft 文件夹。

5)更多的选项

-- 在命令行中使“--”后的选项开关“-”都失效。这样就允许在命令行中使用文件名以“-”开头的文件。
7z t -- -ArchiveName.7z :测试 -ArchiveName.7z 压缩档案.

-i指定压缩时附加文件或一类文件。此选项可附件添加多个类型。
i[<recurse_type>]<file_ref> 其中<recurse_type>为可以为r[- | 0](具体的-r选项见后面-r),<file_ref>可以为@{listfile} | !{wildcard}。
7z a -tzip src.zip *.txt -ir!DIR1\*.cpp :从当前目录中添加 *.txt 文件,和 DIR1 目录及其子目录中的 *.cpp 文件到 src.zip 压缩档案。

-x 指定某一文件或某一类文件从操作中排除。此选项可同时排除多个类型。
x[<recurse_type>]<file_ref> 其中<recurse_type>为可以为r[- | 0](具体的-r选项见后面-r),<file_ref>可以为@{listfile} | !{wildcard}。
7z a -tzip archive.zip *.txt -x!temp.* :添加除 temp.* 文件之外的所有 *.txt 文件到压缩档案 archive.zip。

-o 指定释放文件的输出文件夹。此选项只能和释放命令配合使用。
7z x archive.zip -oc:\Doc :从 archive.zip 压缩档案释放所有文件到 c:\Doc 文件夹。

-r 递归子目录选项。
-r 开启递归子目录。对于 e (释放)、l (列表)、t (测试)、x (完整路径释放) 这些在压缩档案中操作的命令, 会默认使用此选项。  
-r- 关闭递归子目录。对于 a (添加)、d (删除)、u (更新) 等所有需扫描磁盘文件的命令,会默认使用此选项。  
-r0 开启递归子目录。但只应用于通配符。 
7z l archive.zip -r- *.doc :列出在 archive.zip 压缩档案中根目录下的 *.doc 文件。
7z a -tzip archive.zip -r src\*.cpp src\*.h :将 src 目录及其子目录中的 *.cpp 及 *.h 文件添加到 archive.zip 压缩档案。

-t 指定压缩档案格式。指定压缩档案格式。它们可以是:zip、7z、rar、cab、gzip、bzip2、tar 或其它格式。而 默认值是 7z 格式。
7z a -tzip archive.zip *.txt :使用 zip 格式从当前目录中添加所有 *.txt 文件到压缩档案 archive.zip。

-y 使 7-Zip 执行命令时的大多数提示失效。您可以使用此选项来阻止在 e (释放) 和 x (完整路径释放) 命令中文件覆盖时的提示。
7z x src.zip -y :从 src.zip 释放所有文件。所有的覆盖提示将被阻止且所有相同文件名的文件将被覆盖。

-v指定分卷大小。
{Size}[b | k | m | g]
指定分卷大小,可以使用字节、KB(1 KB=1024 字节),MB(1 MB = 1024 KB)或 GB(1 GB = 1024 MB)。如果您只指定了 {Size},7-zip 将把它视为字。
7z a a.7z *.txt -v10k -v15k -v2m : 创建 a.7z 分卷压缩档案。第一个分卷为 10 KB,第二个为 15 KB,剩下全部为 2 MB。

-p 指定密码。
7z x archive.zip -psecret :将设有密码“secret”的压缩档案 archive.zip 中所有文件释放。

-ao 指定在释放期间如何覆盖硬盘上现有的同名文件。
语法:-ao[a | s | u ]
-aoa 直接覆盖现有文件,而没有任何提示。
-aos 跳过现有文件,其不会被覆盖。  
-aou 如果相同文件名的文件以存在,将自动重命名被释放的文件。举个例子,文件 file.txt 将被自动重命名为 file_1.txt。
-aot 如果相同文件名的文件以存在,将自动重命名现有的文件。举个例子,文件 file.txt 将被自动重命名为 file_1.txt。
7z x test.zip -aoa :从压缩档案 test.zip 中释放所有文件并却不做提示直接覆盖现有文件。

-an 不解析命令行中的 archive_name 区域。此选项必须和 -i (附加文件) 开关一起使用。比如您为压缩档案使用列表文件,您就需要指定 -ai 选项,所以您需要禁止解析命令行中的 archive_name 区域。
实例见后面的-ai和-ax中。

-ai 指定附加文件,包括压缩档案文件名及通配符。此选项可同时附加多个类型。
ai[<recurse_type>]<file_ref> 其中<recurse_type>为可以为r[- | 0](具体的-r选项见后面-r),<file_ref>可以为@{listfile} | !{wildcard}。
7z t -an -air!*.7z : 在当前目录及子目录下测试 *.7z 压缩档案。

-ax 指定必须从操作中排除的压缩档案。此选项可同时排除多个类型。
ax[<recurse_type>]<file_ref> 其中<recurse_type>为可以为r[- | 0](具体的-r选项见后面-r),<file_ref>可以为@{listfile} | !{wildcard}。
7z t -an -ai!*.7z -ax!a*.7z :测试除 a*.7z 之外的 *.7z 压缩档案。

更多的不常用的选项,可以查看帮助。例如:-m设置压缩算法;-scs 设置要压缩的文件的列表文件的字符集;-seml通过电子邮件发送压缩档;-sfx创建自释放档;-si从标准输入读入数据,-so从输出到标准输出;-slp设置大内存模式;-slt显示技术信息;-ssc设置区分大小写;-ssw压缩正在写入的文件;-u更新选项。


二 zip/unzip
1)简介
zip/unzip目前被广泛的使用,是类似于bsd协议的开源免费的压缩和解压工具。主要基于开源的压缩解压算法info-zip。几乎可以在所有的平台上使用。
主页:
http://www.info-zip.org/
http://www.info-zip.org/Zip.html
http://www.info-zip.org/UnZip.html
http://zlib.net/
下载:http://www.johntdow.com/download/zipunzip.exe
最新源码:http://www.johntdow.com/download/zipunzip.exe

2) zip
Copyright (C) 1990-2005 Info-ZIP
Type 'zip "-L"' for software license.
Zip 2.31 (March 8th 2005). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
  -q   quiet operation              -v   verbose operation/print version info
  -c   add one-line comments        -z   add zipfile comment
  -@   read names from stdin        -o   make zipfile as old as latest entry
  -x   exclude the following names  -i   include only the following names
  -F   fix zipfile (-FF try harder) -D   do not add directory entries
  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -!   use privileges (if granted) to obtain all aspects of WinNT security
  -R   PKZIP recursion (see manual)
  -$   include volume label         -S   include system and hidden files

  -e   encrypt                      -n   don't compress these suffixes

 

实例:打包c:\下的所有log,除了test,test2和windows目录。

cd /d c:\

C:\>d:\bresoft\zip\zip -r -9 c:\log.zip * -i *.log -x test\* -x test2\* -x "windows"\* >log.log


3)unzip
UnZip 5.52 of 28 February 2005, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir;
  file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).

  -p  extract files to pipe, no messages     -l  list files (short format)
  -f  freshen existing files, create none    -t  test compressed archive data
  -u  update files, create if necessary      -z  display archive comment
  -x  exclude files that follow (in xlist)   -d  extract files into exdir

modifiers:                                   -q  quiet mode (-qq => quieter)
  -n  never overwrite existing files         -a  auto-convert any text files
  -o  overwrite files WITHOUT prompting      -aa treat ALL files as text
  -j  junk paths (do not make directories)   -v  be verbose/print version info
  -C  match filenames case-insensitively     -L  make (some) names lowercase
  -$  label removables (-$$ => fixed disks)  -V  retain VMS version numbers
  -X  restore ACLs (-XX => use privileges)   -s  spaces in filenames => '_'
                                             -M  pipe through "more" pager
Examples (see unzip.txt for more info):
  unzip data1 -x joe   => extract all files except joe from zipfile data1.zip
  unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer

三 当然你可以使用商业的压缩加压工具,例如winzip,winrar

四 实例
压缩拷贝解压实例:

set source=d:\desk\
set zipfilename=temp00001.zip
set dest=d:\mydesk2
pause

echo start ziping
7za.exe a %zipfilename% %source%

pause

echo start copying
robocopy.exe //np ./ %dest% %zipfilename%

pause

echo start unziping
7za.exe x %dest%\%zipfilename% --o%dest%

pause

echo start delete temp zip file
del //%dest%\%zipfilename%
del //%zipfilename%

echo allcopyfinished > %dest%\allcopyfinished.txt

五 完!
posted @ 2009-08-31 14:58  iTech  阅读(11044)  评论(0编辑  收藏  举报