在上一篇教程 Linux压缩和解压rar 中写了 rar 的压缩和解压,这里继续来写 7z 的压缩和解压。7z 是现在主流的一种压缩格式,它拥有极高的压缩比,我做过一个测试,用 rar 和 7z 压缩同一个文件,都选择最高质量,最终经过 7z 压缩的文件体积确实要比 rar 小一些,而且 7z 是开源免费的,现在很多压缩工具也都能解压 7z。

我用来演示的 Linux 版本是 CentOS7,因为 CentOS 的 yum 没有 p7zip,所以只能手动下载 rpm 安装。

下载 p7zip:

wget https://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/7/x86_64/Packages/p/p7zip-16.02-10.el7.x86_64.rpm

下载 p7zip-plugins:

wget https://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/7/x86_64/Packages/p/p7zip-plugins-16.02-10.el7.x86_64.rpm

如果要下载其它版本 的p7zip rpm 可以上 https://rpmfind.net/linux/rpm2html/search.php?query=p7zip ,p7zip-plugins rpm 可以上 https://rpmfind.net/linux/rpm2html/search.php?query=p7zip-plugins

安装 p7zip:

rpm -ivh p7zip-16.02-10.el7.x86_64.rpm

安装 p7zip-plugins:

rpm -ivh p7zip-plugins-16.02-10.el7.x86_64.rpm

如果要更详细的了解 rpm 包管理可以看 Linux rpm包管理,安装完成后输入 7z,如果出现帮助信息说明安装成功。

如果是 Ubuntu 这一类系统的话,直接输入下面的两条命令自动下载安装:

apt-get install p7zip
apt-get install p7zip-full

关于使用方法 CentOS 和 Ubuntu 都是一样的。

压缩

压缩文件: 7z a 压缩包名 要压缩的文件名 ,下面把 1.php 压缩为 1.7z

[root@localhost home]# 7z a 1.7z 1.php

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)

Scanning the drive:
1 file, 35 bytes (1 KiB)

Creating archive: 1.7z

Items to compress: 1

    
Files read from disk: 1
Archive size: 153 bytes (1 KiB)
Everything is Ok

下面把 1.php2.php 压缩为 file.7z

[root@localhost home]# 7z a file.7z 1.php 2.php

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)

Scanning the drive:
2 files, 224 bytes (1 KiB)

Creating archive: file.7z

Items to compress: 2

    
Files read from disk: 2
Archive size: 318 bytes (1 KiB)
Everything is Ok

下面把 dir 目录压缩为 dir.7z

[root@localhost home]# 7z a dir.7z dir

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)

Scanning the drive:
1 folder, 2 files, 224 bytes (1 KiB)

Creating archive: dir.7z

Items to compress: 3

    
Files read from disk: 2
Archive size: 338 bytes (1 KiB)
Everything is Ok

给压缩包添加文件: 7z u 压缩包名 要添加的文件名 ,下面把 2.php 添加到 1.7z

[root@localhost home]# 7z u 1.7z 2.php

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)

Open archive: 1.7z
--
Path = 1.7z
Type = 7z
Physical Size = 153
Headers Size = 114
Method = LZMA2:12
Solid = -
Blocks = 1

Scanning the drive:
1 file, 189 bytes (1 KiB)

Updating archive: 1.7z

Items to compress: 1

    
Files read from disk: 1
Archive size: 336 bytes (1 KiB)
Everything is Ok

删除压缩包内的文件: 7z d 压缩包名 要删除的文件名 ,下面删除 1.7z 中的 2.php

[root@localhost home]# 7z d 1.7z 2.php

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)

Open archive: 1.7z
--
Path = 1.7z
Type = 7z
Physical Size = 336
Headers Size = 161
Method = LZMA2:12
Solid = -
Blocks = 2

Updating archive: 1.7z

Items to compress: 0

    
Files read from disk: 0
Archive size: 153 bytes (1 KiB)
Everything is Ok

加密压缩: 7z a -p 压缩包名 要压缩的文件名 ,执行命令后会提示输入密码,输入密码的时候终端不会显示任何内容,下面把 1.php 加密压缩为 1.7z

[root@localhost home]# 7z a -p 1.7z 1.php

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)

Scanning the drive:
1 file, 35 bytes (1 KiB)

Creating archive: 1.7z

Items to compress: 1


Enter password (will not be echoed):
Verify password (will not be echoed) :
    
Files read from disk: 1
Archive size: 178 bytes (1 KiB)
Everything is Ok

解压

把文件解压到当前目录: 7z e 压缩包名 ,下面解压 1.7z

[root@localhost home]# 7z e 1.7z

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)

Scanning the drive for archives:
1 file, 153 bytes (1 KiB)

Extracting archive: 1.7z
--
Path = 1.7z
Type = 7z
Physical Size = 153
Headers Size = 114
Method = LZMA2:12
Solid = -
Blocks = 1

Everything is Ok

Size:       35
Compressed: 153

查看压缩包内的文件信息: 7z l 压缩包名 ,下面查看 1.7z 中的文件信息:

[root@localhost home]# 7z l 1.7z

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)

Scanning the drive for archives:
1 file, 240 bytes (1 KiB)

Listing archive: 1.7z

--
Path = 1.7z
Type = 7z
Physical Size = 240
Headers Size = 158
Method = LZMA2:12
Solid = -
Blocks = 2

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2019-02-24 10:41:29 ....A           35           39  1.php
2019-02-24 12:25:13 ....A           41           43  2.php
------------------- ----- ------------ ------------  ------------------------
2019-02-24 12:25:13                 76           82  2 files

下面是 p7zip 常用的一些参数:

参数说明
a压缩文件
d删除压缩包内的文件
e把文件解压到当前目录
h计算文件的哈希值
i显示有关支持格式的信息
l列出压缩包内的内容
m重命名压缩包内的文件
t测试压缩包的完整性
u添加文件到压缩包

p7zip 除了上面列出的参数外还有很多可选的选项,下面是 p7zip 的帮助说明:

<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 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
  -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
  -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

到这里关于 Linux 的压缩基本上就已经写完了。