主题:使用 7z 压缩文件

7z 官网:https://www.7-zip.org/

环境:下载并安装 7z 后,将安装路径加入系统环境变量 path 中

 

命令行参数:

C:\Users\Administrator>7z -h

7-Zip 21.03 beta (x64) : Copyright (c) 1999-2021 Igor Pavlov : 2021-07-20

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 停止开关和@listfile解析
  -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 设置CPU线程数
    -mx[N] : set compression level: -mx1 (fastest) ... -mx9 (ultra) 设置压缩级别:-mx1(最快)-mx9(超高)
  -o{Directory} : set Output directory 设置输出目录
  -p{Password} : set Password 设置密码
  -r[-|0] : Recurse subdirectories for name search 递归用于名称搜索的子目录
  -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 为x、e、h命令设置哈希函数
  -sdel : delete files after compression 压缩后删除文件
  -seml[.] : send archive by email 通过电子邮件发送存档
  -sfx[{name}] : Create SFX archive 创建SFX存档
  -si[{name}] : read data from stdin 从标准输入读取数据
  -slp : set Large Pages mode 设置大页面模式
  -slt : show technical information for l (List) command 显示l(列表)命令的技术信息
  -snh : store hard links as links 将硬链接存储为链接
  -snl : store symbolic links as links 将符号链接存储为链接
  -sni : store NT security information 存储NT安全信息
  -sns[-] : store NTFS alternate streams 存储NTFS备用流
  -so : write data to stdout 将数据写入标准输出
  -spd : disable wildcard matching for file names 禁用文件名的通配符匹配
  -spe : eliminate duplication of root folder for extract command 为extract命令消除根文件夹的重复
  -spf : use fully qualified file paths 使用完全限定的文件路径
  -ssc[-] : set sensitive case mode 设置敏感案例模式
  -sse : stop archive creating, if it can't open some input file 如果无法打开某些输入文件,请停止创建存档
  -ssp : do not change Last Access Time of source files while archiving 存档时不更改源文件的上次访问时间
  -ssw : compress shared files 压缩共享文件
  -stl : set archive timestamp from the most recently modified file 从最近修改的文件设置存档时间戳
  -stm{HexMask} : set CPU thread affinity mask (hexadecimal number) 设置CPU线程关联掩码(十六进制数)
  -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 假设所有查询都是

 

具体命令详解可以查看 7z 帮助文档:

 

 

 

示例:

# 删除 a.7z 文件
erase a.7z

# 将 bin 目录下的所有文件打包成 a.7z,使用密码 123 加密,并过滤掉 *.ilk *.pdb x.lib
7z a -r a.7z ".\bin" -p123 -x"!*.ilk" -x"!*.pdb" -x"!x.lib"