Linux 文件压缩

点击访问 Linux 命令大全 >>

一个较大的文件经压缩后,产生了另一个较小容量的文件。而这个较小容量的文件,我们就叫它是这些较大容量的(可能一个或一个以上的文件)的压缩文件。而压缩此文件的过程称为文件压缩。

为什么我们需要压缩文件呢?很简单,压缩后的文件占用磁盘较小,进行网络传输可以节省带宽、传输较快;由于压缩文件时可以指定密码,也可以用来对文件进行加密,增加安全性。

gzip 命令

gzip 是目前 Linux 下使用最广泛的压缩工具。语法如下:

Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.

gzip 的使用非常方便,只要简单的在 gzip 命令后跟上一个想要压缩的文件作为参数就可以了。例如:

## 查看我们带压缩的文本文件
[root@localhost ~]# ll hello.txt
-rw-r--r--. 1 root root 0 7月  28 18:13 hello.txt

## 压缩 hello.txt 文件
[root@localhost ~]# gzip hello.txt

## 查看压缩后的文件 hello.txt.gz
[root@localhost ~]# ll hello.txt.gz
-rw-r--r--. 1 root root 30 7月  28 18:13 hello.txt.gz

在默认情况下,gzip 命令会给被压缩的文件加上“gz”扩展名。进过 gzip 压缩处理后 hello.txt 文件就变成了 hello.txt.gz 文件。

如果要解压 gz 文件,可以使用 gzip -d 命令。如下:

## 查看文件 hello.txt.gz
[root@localhost ~]# ll
总用量 8
-rw-------. 1 root root 1259 7月  28 17:48 anaconda-ks.cfg
drwxr-xr-x. 5 root root  149 8月   6 06:09 bak
-rw-r--r--. 1 root root   30 7月  28 18:13 hello.txt.gz

## 解压 hello.txt.gz 文件
[root@localhost ~]# gzip -d hello.txt.gz

## 再次查看解压出来的 hello.txt 文件
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1259 7月  28 17:48 anaconda-ks.cfg
drwxr-xr-x. 5 root root  149 8月   6 06:09 bak
-rw-r--r--. 1 root root    0 7月  28 18:13 hello.txt

你也可以使用 gunzip 命令解压 gz 文件。如下:

## 查看文件 hello.txt.gz
[root@localhost ~]# ll
总用量 8
-rw-------. 1 root root 1259 7月  28 17:48 anaconda-ks.cfg
drwxr-xr-x. 5 root root  149 8月   6 06:09 bak
-rw-r--r--. 1 root root   30 7月  28 18:13 hello.txt.gz

## 解压 hello.txt.gz 文件
[root@localhost ~]# gunzip hello.txt.gz

## 解压结果
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1259 7月  28 17:48 anaconda-ks.cfg
drwxr-xr-x. 5 root root  149 8月   6 06:09 bak
-rw-r--r--. 1 root root    0 7月  28 18:13 hello.txt

注意:gzip 命令支持的扩展名有 .gz、.Z、-gz、.z、和 z。

使用 -l 选项可以查看压缩文件的压缩效果。如下:

[root@localhost ~]# gzip -l hello.txt.gz
         compressed        uncompressed  ratio uncompressed_name
             684530           131392000  99.5% hello.txt

可以看到,文件 hello.txt 在压缩前 131392000 字节,压缩后 684530 字节,压缩率 99.5%。

使用 -t 选项来测试压缩文件的完整性。如果文件正常,gzip 命令不会给出任何显示,可以使用 -v 给出命令操作成功后的提示。如下:

[root@localhost ~]# gzip -t hello.txt.gz
[root@localhost ~]# gzip -tv hello.txt.gz
hello.txt.gz:    OK

rar 命令

在 Windows 下 rar 已经取代 zip 成为了标准压缩格式,因为 rar 相比较 zip 最大的优势在于压缩效果更好。在 Linux 下处理 rar 文件可以使用 RAR for Linux,这个命令行工具可以从 https://www.rarlab.com/ 上下载。

先下载 rarlinux-x64-5.9.1.tar.gz 文件,下载地址 https://www.rarlab.com/rar/rarlinux-x64-5.9.1.tar.gz ,安装步骤如下:

## 使用 tar -xvzf 解压文件
[root@localhost ~]# tar -xvzf rarlinux-x64-5.9.1.tar.gz

## 进入 rar 目录,查看目录情况,rar 文件就可以用来压缩解压文件
root@localhost rar]# ll
总用量 1316
-rw-r--r--. 1 snow snow   4188 6月  25 18:44 acknow.txt
-rwxr-xr-x. 1 snow snow 199528 6月  25 18:44 default.sfx
-rw-r--r--. 1 snow snow   6753 6月  25 18:44 license.txt
-rw-r--r--. 1 snow snow    428 6月  25 18:44 makefile
-rw-r--r--. 1 snow snow   3324 6月  25 18:44 order.htm
-rwxr-xr-x. 1 snow snow 630536 6月  25 18:44 rar
-rw-r--r--. 1 snow snow   1177 6月  25 18:44 rarfiles.lst
-rw-r--r--. 1 snow snow 104763 6月  25 18:44 rar.txt
-rw-r--r--. 1 snow snow    692 6月  25 18:44 readme.txt
-rwxr-xr-x. 1 snow snow 347152 6月  25 18:44 unrar
-rw-r--r--. 1 snow snow  26069 6月  25 18:44 whatsnew.txt

接下来,我们就可以使用 rar 命令将 hello.txt 文件压缩为 my.rar 文件。例如:

## 压缩文件
[root@localhost ~]# ./rar/rar a my.rar hello.txt

RAR 5.91   Copyright (c) 1993-2020 Alexander Roshal   25 Jun 2020
Trial version             Type 'rar -?' for help

Evaluation copy. Please register.

Creating archive my.rar

Adding    hello.txt                                                   OK
Done

## 查看压缩后的文件
[root@localhost ~]# ll my.rar
-rw-r--r--. 1 root root 75498 8月   6 06:38 my.rar

使用 x 选项解压 rar 文件。例如:

[root@localhost ~]# ./rar/rar x my.rar

RAR 5.91   Copyright (c) 1993-2020 Alexander Roshal   25 Jun 2020
Trial version             Type 'rar -?' for help


Extracting from my.rar


Would you like to replace the existing file hello.txt
131392000 bytes, modified on 2020-08-06 06:16
with a new one
131392000 bytes, modified on 2020-08-06 06:16

[Y]es, [N]o, [A]ll, n[E]ver, [R]ename, [Q]uit Y

Extracting  hello.txt                                                 OK
All OK

使用 t 选项检查 rar 文件的完整性。例如:

[root@localhost ~]# ./rar/rar t my.rar

RAR 5.91   Copyright (c) 1993-2020 Alexander Roshal   25 Jun 2020
Trial version             Type 'rar -?' for help


Testing archive my.rar

Testing     hello.txt                                                 OK
All OK
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号