[Windows]_[初级]_[使用7z命令行工具进行打包备份]
场景在开发程序或做一些自动化脚本时, 经常用压缩格式来进行打包文件夹为zip,或解压zip,rar,tar文件。如果压缩的是exe二进制文件,是不需要再进行压缩的,只需要存储即可,这样可以增加打包的速度。比如,在*nux系统可以通过tar命令打包持续集成的备份,可以过滤掉二进制安装包,只备份配置文件,这样备份大小就会小很多。而在Windows上可以使用开源免费的7-Zip软件的命令行工具做到同样的
场景
- 在开发程序或做一些自动化脚本时, 经常用压缩格式来进行打包文件夹为
zip,或解压zip,rar,tar文件。如果压缩的是exe二进制文件,是不需要再进行压缩的,只需要存储即可,这样可以增加打包的速度。比如,在*nux系统可以通过tar命令打包持续集成的备份,可以过滤掉二进制安装包,只备份配置文件,这样备份大小就会小很多。而在Windows上可以使用开源免费的7-Zip软件的命令行工具做到同样的效果,怎么做?
说明
7z(7-Zip)支持很多常见的压缩格式创建和解压,其中开源压缩格式基本都支持创建7z,BZIP2,GZIP,TAR,WIM,XZ,ZIP,其他商业格式只支持解压。注意,7z使用LZMA / LZMA2作为默认压缩格式,即对lzma自然支持压缩和解压。
Format Creation Filename Extensions
7z X 7z
BZIP2 X bz2 bzip2 tbz2 tbz
GZIP X gz gzip tgz
TAR X tar
WIM X wim swm esd
XZ X xz txz
ZIP X zip zipx jar xpi odt ods docx xlsx epub
APM apm
AR ar a deb lib
ARJ arj
CAB cab
CHM chm chw chi chq
COMPOUND msi msp doc xls ppt
CPIO cpio
CramFS cramfs
DMG dmg
Ext ext ext2 ext3 ext4 img
FAT fat img
HFS hfs hfsx
HXS hxs hxi hxr hxq hxw lit
iHEX ihex
ISO iso img
LZH lzh lha
LZMA lzma
MBR mbr
MsLZ mslz
Mub mub
NSIS nsis
NTFS ntfs img
MBR mbr
RAR rar r00
RPM rpm
PPMD ppmd
QCOW2 qcow qcow2 qcow2c
SPLIT 001 002 ...
SquashFS squashfs
UDF udf iso img
UEFIc scap
UEFIs uefif
VDI vdi
VHD vhd
VMDK vmdk
XAR xar pkg
Z z taz
-
7z是开源的,协议是LGPL协议,因此它的dll动态库可用于商业程序的分发. 如果要使用它的SDK开发程序,通过SDK网址[2]下载使用 -
在
7z的安装目录下,也有一个7-zip.chm文档,这里介绍了命令行工具7z.exe的一些用法. 当然可以在命令行输入7z -h来获取简单的命令介绍. 大概就是两个关键参数,command命令类型,switches是关于选择命令的补充参数.
Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] [@listfile]
<Commands>
a : Add files to archive
b : Benchmark
d : Delete files from archive
e : Extract files from archive (without using directory names)
h : Calculate hash values for files
i : Show information about supported formats
l : List contents of archive
rn : Rename files in archive
t : Test integrity of archive
u : Update files to archive
x : eXtract files with full paths
<Switches>
-- : Stop switches and @listfile parsing
-ai[r[-|0]]{@listfile|!wildcard} : Include archives
-ax[r[-|0]]{@listfile|!wildcard} : eXclude archives
-ao{a|s|t|u} : set Overwrite mode
-an : disable archive_name field
-bb[0-3] : set output log level
-bd : disable progress indicator
-bs{o|e|p}{0|1|2} : set output stream for output/error/progress line
-bt : show execution time statistics
-i[r[-|0]]{@listfile|!wildcard} : Include filenames
-m{Parameters} : set compression Method
-mmt[N] : set number of CPU threads
-mx[N] : set compression level: -mx1 (fastest) ... -mx9 (ultra)
-o{Directory} : set Output directory
-p{Password} : set Password
-r[-|0] : Recurse subdirectories
-sa{a|e|s} : set Archive name mode
-scc{UTF-8|WIN|DOS} : set charset for for console input/output
-scs{UTF-8|UTF-16LE|UTF-16BE|WIN|DOS|{id}} : set charset for list files
-scrc[CRC32|CRC64|SHA1|SHA256|*] : set hash function for x, e, h commands
-sdel : delete files after compression
-seml[.] : send archive by email
-sfx[{name}] : Create SFX archive
-si[{name}] : read data from stdin
-slp : set Large Pages mode
-slt : show technical information for l (List) command
-snh : store hard links as links
-snl : store symbolic links as links
-sni : store NT security information
-sns[-] : store NTFS alternate streams
-so : write data to stdout
-spd : disable wildcard matching for file names
-spe : eliminate duplication of root folder for extract command
-spf : use fully qualified file paths
-ssc[-] : set sensitive case mode
-sse : stop archive creating, if it can't open some input file
-ssw : compress shared files
-stl : set archive timestamp from the most recently modified file
-stm{HexMask} : set CPU thread affinity mask (hexadecimal number)
-stx{Type} : exclude archive type
-t{Type} : Set type of archive
-u[-][p#][q#][r#][x#][y#][z#][!newArchiveName] : Update options
-v{Size}[b|k|m|g] : Create volumes
-w[{path}] : assign Work directory. Empty path means a temporary directory
-x[r[-|0]]{@listfile|!wildcard} : eXclude filenames
-y : assume Yes on all queries
例子
常见用法
-
压缩当前目录的所有文件到
1.zip文件,指定格式为zip, 这是创建压缩文件,注意,如果已存在同名的文件,那么回添加到同名文件里,不会覆盖。7z a -tzip 1.zip * -
同上,也是压缩当前目录,只是过滤掉后缀为
exe的文件。注意x表示排除,r表示递归子目录,!用于通配符*.exe.7z a -tzip 1.zip . -xr!*.exe -
同1,也是压缩当前目录,但是使用压缩级别
0,因此-mx设置压缩级别的,在chm文档里有介绍zip格式的压缩方法.0是存储,即-mx0为存储,可以通过压缩后判断前后大小一致来判定,目前7z命令没有查看zip的压缩级别的方式.7z a -tzip -mx0 2.zip * -
压缩多个不同目录到一个
chm.zip
7z a -tzip chm.zip "E:\桌面\软件\HTML Help WorkShop" "E:\桌面\软件\depends22_x64"
-
目录过多过长时,可以通过创建一个
utf-8编码的文件来列出需要压缩的目录, 这里创建了一个listfile.txt文件,并且每个目录路径一行,可中文。7z a -tzip listfile.zip @listfile.txt -
排除
exe文件, 同样也要外部列表文件。7z a -tzip -xr!*.exe listfile-noexe.zip @listfile.txt -
解压
zip到新建文件夹目录,没有会创建,保留压缩文件里的目录层级和文件位置。7z x listfile.zip -o新建文件夹
listfile.txt
E:\桌面\软件\HTML Help WorkShop
E:\桌面\软件\depends22_x64
参考
更多推荐


所有评论(0)