6.22任务

6.1压缩打包介绍

Windows上经常会用到打包的文件,比如一些后缀.rar,.zip,.7z。

压缩的文件传输起来会增加效率。对于一些大公司的机房带宽很昂贵,压缩的文件上传下载可以节省很多带宽。因此,压缩是一个节省带宽的好方法。

linux下也有很多压缩包,后缀有.zip,.gz,.bz2,.xz,.tar.gz,.tar.bz2,.tar.xz。

虽然linux下的后缀大多是约定的,并不要求,但是压缩包的压缩和解压对后缀名还是有一定的要求的,遵循我们的约定,方便你我。

6.2 gzip压缩工具

gzip是linux下的常见的一种压缩工具,很简单,直接跟目录名就可以了

下面我们做一些准备工作,在/tmp下创建一个d6z(第六章)的目录,然后find查询/etc下的文件cat重定向到1.txt。

[root@localhost ~]# cd /tmp/
[root@localhost tmp]# mkdir d6z/
[root@localhost tmp]# cd !$
cd d6z/
[root@localhost d6z]# ls
[root@localhost d6z]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
[root@localhost d6z]# ls
1.txt
[root@localhost d6z]# ls -h 1.txt 
1.txt
[root@localhost d6z]# ls -lh 1.txt 
-rw-r--r--. 1 root root 210K Jun 22 15:07 1.txt
[root@localhost d6z]# du -sh 1.txt 
212K	1.txt
[root@localhost d6z]# du -sb 1.txt 
214469	1.txt
[root@localhost d6z]# cat 1.txt >> 1.txt 
cat: 1.txt: input file is output file
[root@localhost d6z]# cat 1.txt >> 2.txt 
[root@localhost d6z]# cat 2.txt >> 1.txt 
[root@localhost d6z]# cat 2.txt >> 1.txt 
[root@localhost d6z]# cat 2.txt >> 1.txt 
[root@localhost d6z]# cat 2.txt >> 1.txt 
[root@localhost d6z]# cat 2.txt >> 1.txt 
[root@localhost d6z]# cat 1.txt >> 2.txt 
[root@localhost d6z]# cat 1.txt >> 2.txt 
[root@localhost d6z]# cat 1.txt >> 2.txt 
[root@localhost d6z]# cat 1.txt >> 2.txt 
[root@localhost d6z]# cat 1.txt >> 2.txt 
[root@localhost d6z]# cat 1.txt >> 2.txt 
[root@localhost d6z]# cat 2.txt >> 1.txt 
[root@localhost d6z]# cat 2.txt >> 1.txt 
[root@localhost d6z]# wc -l 1.txt 
428560 1.txt
[root@localhost d6z]# du -sh 1.txt 
33M	1.txt

下面我们就用gzip工具压缩。

[root@localhost d6z]# ls
1.txt  2.txt
[root@localhost d6z]# gzip 1.txt 
[root@localhost d6z]# ls
1.txt.gz  2.txt
[root@localhost d6z]# du -sh 1.txt.gz 
4.1M	1.txt.gz

我们可以很明显的看到文件从33M压缩到了4.1M。

那么我们下载了一个.gz的文件可以用-d选项解压这个文件。

[root@localhost d6z]# gzip -d 1.txt.gz 
[root@localhost d6z]# ls
1.txt  2.txt
[root@localhost d6z]# du -sh 1.txt 
17M	1.txt

解压后文本文件变成了17M,原因是我们生成这个文本的时候使用的是重定向符,这样生成的文本会有一些空间是“虚的”,压缩以后这部分就消失了。

[root@localhost d6z]# du -sh 1.txt 
17M	1.txt
[root@localhost d6z]# gzip 1.txt 
[root@localhost d6z]# du -sh 1.txt.gz 
4.1M	1.txt.gz
[root@localhost d6z]# gzip -d 1.txt.gz 
[root@localhost d6z]# du -sh 1.txt 
17M	1.txt

重新做一遍,这个问题就消失了。

-#选项可以调整gzip的压缩级别,这里的#是数字[1-9],默认级别是6,也可以调整。

 -# --fast --best
   Regulate  the speed of compression using the specified digit #, where -1 or --fast indicates the fastest compression method (less compression) and -9 or --best indicates the slowest compression method (best compression).  The default compression level is -6 (that is, biased towards high compression at expense of speed).

解压的命令也可以使用gunzip + ×.gz命令

[root@localhost d6z]# gzip -9 1.txt 
[root@localhost d6z]# du -sh 1.txt.gz 
4.1M	1.txt.gz
[root@localhost d6z]# file 1.txt.gz 
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Jun 22 15:11:07 2018, max compression

压缩文件的内容使用cat等命令是不能查看内容的,如果想查看可以使用zcat配合管道符|的方法。

[root@localhost d6z]# zcat 1.txt.gz | head
# Generated by NetworkManager
nameserver 8.8.8.8
# The upstream Mozilla.org project tests all changes to the root CA
# list with the NSS (Network Security Services) library.
#
# Occassionally, changes might cause compatibility issues with
# other cryptographic libraries, such as openssl or gnutls.
#
# The package maintainers of the CA certificates package might decide
# to temporarily keep certain (legacy) root CA certificates trusted,

使用-c选项(Write output on standard output)可以生成一个新的压缩文件(不加这个选项会就地压缩)

[root@localhost d6z]# ls
1.txt.gz  2.txt
[root@localhost d6z]# gunzip 1.txt.gz 
[root@localhost d6z]# ls
1.txt  2.txt
[root@localhost d6z]# gzip -c 1.txt > 1.txt.gz
[root@localhost d6z]# ls
1.txt  1.txt.gz  2.txt

同样解压也是一个道理

[root@localhost d6z]# ls
1.txt  1.txt.gz 
[root@localhost d6z]# gzip -d -c 1.txt.gz > 2.txt
[root@localhost d6z]# ls
1.txt  1.txt.gz  2.txt
[root@localhost d6z]# diff 2.txt 1.txt
[root@localhost d6z]# 

最后,gzip不能压缩目录。

[root@localhost tmp]# gzip d6z/
gzip: d6z/ is a directory -- ignored

6.3 bzip2压缩工具

bzip2压缩工具的算法与gzip不同,会消耗更多的资源,压缩比更高。

[root@localhost d6z]# du -sh *
17M	1.txt
4.1M	1.txt.gz
17M	2.txt
[root@localhost d6z]# bzip2 2.txt 
[root@localhost d6z]# du -sh *
17M	1.txt
4.1M	1.txt.gz
1.4M	2.txt.bz2

对于相同的文件,使用gzip压缩到了4.1M而bzip2可以压缩到1.4M。

bzip2的选项和gzip差不多。

解压的时候也使用-d选项。

[root@localhost d6z]# bzip2 -d 2.txt.bz2 
[root@localhost d6z]# du -sh *
17M	1.txt
4.1M	1.txt.gz
17M	2.txt

也可以使用bunzip2解压。

同样也有标准输出-c选项。

同样也有压缩级别(默认为9)。

同样也不支持压缩目录。

如果这个压缩文件的后缀不是.bz2,解压的时候会重新命名这个文件(也是可以解压的)

[root@localhost d6z]# bzip2 2.txt 
ls[root@localhost d6z]# ls
1.txt  1.txt.gz  2.txt.bz2
[root@localhost d6z]# mv 2.txt.bz2 2.txt
[root@localhost d6z]# ls
1.txt  1.txt.gz  2.txt
[root@localhost d6z]# bzip2 -d 2.txt
bzip2: Can't guess original name for 2.txt -- using 2.txt.out
[root@localhost d6z]# ls
1.txt  1.txt.gz  2.txt.out
[root@localhost d6z]# less 2.txt.out 

到底是什么样的文件,使用file命令查看。

[root@localhost d6z]# file *
1.txt:     C source, UTF-8 Unicode text
1.txt.gz:  gzip compressed data, was "1.txt", from Unix, last modified: Fri Jun 22 15:11:07 2018
2.txt.out: C source, UTF-8 Unicode text

同样,查看bz2压缩文件内容使用bzcat命令。

6.4 xz压缩工具

xz压缩不常用,它消耗的资源更多,压缩比更高。

[root@localhost d6z]# cp 1.txt 3.txt
[root@localhost d6z]# mv 2.txt.out 2.txt
[root@localhost d6z]# du -sh *
17M	1.txt
4.1M	1.txt.gz
17M	2.txt
17M	3.txt
[root@localhost d6z]# bzip2 2.txt 
[root@localhost d6z]# ls
1.txt  1.txt.gz  2.txt.bz2  3.txt
[root@localhost d6z]# xz 3.txt 
[root@localhost d6z]# du -sh *
17M	1.txt
4.1M	1.txt.gz
1.4M	2.txt.bz2
52K	3.txt.xz

特别是文本文档有太多的重复内容,xz压缩是非常好用的。

参数与gzip相同,这里不再赘述。压缩级别默认是-6,有时候使用-9反而得到的文件比-6更大。

转载于:https://my.oschina.net/u/3866688/blog/1834297

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WinRAR 6.22 是一款常用的压缩文件工具。它具有简便易用的界面和多种功能,可以帮助用户对文件和文件夹进行快速、有效的压缩和解压缩操作。 WinRAR 6.22 支持的压缩文件格式多种多样,包括RAR、ZIP、CAB、ARJ、LZH、ACE、TAR等等。用户可以根据自己的需求选择适合的压缩格式,以提高存储效率和文件传输速度。 该软件还具备强大的压缩算法,可以实现高度的文件压缩率,节省存储空间。用户可以通过设置压缩级别、选择压缩方式等操作,根据需要平衡压缩比和压缩速度。 除了压缩文件,WinRAR 6.22 还具备解压缩功能。用户可以将压缩文件解压缩到指定位置,快速获取原始文件。同时,软件还支持密码保护功能,用户可以设置密码来保护压缩文件中的内容,加强文件的安全性。 此外,WinRAR 6.22 还提供了文件分卷压缩功能,用户可以将大文件分成多个部分进行压缩,方便存储和传输。解压缩时,只需解压一个分卷文件即可,便于操作和管理。 总之,WinRAR 6.22 是一款功能全面、操作简便的压缩文件工具。无论是压缩文件还是解压缩文件,它都能提供高效、安全的操作体验,满足用户的各种需求。无论是个人用户还是企业用户,都可以从中受益。 ### 回答2: WinRAR 6.22是一个流行的压缩工具。它可以帮助用户有效地压缩和解压缩文件,节省存储空间并方便文件传输。 WinRAR 6.22具有许多强大的功能。首先,它支持各种文件格式的压缩和解压缩,包括RAR、ZIP、ISO、7Z等。这意味着无论用户需要压缩哪种类型的文件,WinRAR都可以帮助他们完成任务。 其次,WinRAR 6.22具有高度的压缩率,可以显著减小文件大小,从而节省硬盘空间和带宽。用户可以使用不同的压缩级别来平衡压缩率和压缩时间。同时,WinRAR还支持多卷压缩,可以将大文件分割成多个小文件进行传输或存储。 此外,WinRAR 6.22还提供了强大的加密功能,可以保护用户的文件安全。用户可以为压缩文件设置密码,以确保只有授权的人可以访问其中的内容。 还有其他一些实用的功能,例如创建自解压缩文件、修复受损的压缩文件、预览压缩文件的内容等等。WinRAR还提供了直观的界面和易于使用的操作,使用户能够轻松地完成各种压缩和解压缩任务。 总之,WinRAR 6.22是一个功能强大且易于使用的压缩工具,适用于各种文件的压缩和解压缩需求。它提供了高度的压缩率、可靠的文件加密以及其他实用的功能,使用户能够方便地管理和传输文件。无论是个人用户还是商业用户,都会发现WinRAR 6.22是一个不可或缺的工具。 ### 回答3: WinRAR 6.22 是一款非常流行的压缩文件管理软件。它的主要功能是能够将多个文件或文件夹压缩成一个压缩文件,以减小文件的大小并方便传输。同时,它还可以解压缩各种类型的压缩文件。 WinRAR 6.22 具有以下一些主要特点和功能。首先,它支持创建和解压缩众多的压缩文件格式,包括RAR、ZIP、7z等等。无论是哪种格式,我们都可以在WinRAR中方便地进行操作。 其次,WinRAR 6.22 允许用户设置密码来保护压缩文件中的数据。这一功能对于包含私密信息的文件非常重要,可以确保文件只能被授权的人员访问。 另外,WinRAR 6.22 还支持分卷压缩,即将一个大文件分割成多个较小的压缩文件。这样做有利于存储和传输,特别是当需要将大文件拆分在多个存储设备或介质上时。 此外,WinRAR 6.22 还具备可靠的压缩和解压缩算法,可以保证数据的完整性和准确性。它能够压缩文件并还原其原始状态,同时最大限度地减小文件大小。 除此之外,WinRAR 6.22 还提供了一个直观友好的用户界面,使得用户可以轻松地操作和管理压缩文件。它还具备快速的压缩和解压缩速度,能够高效地处理大型文件。 总之,WinRAR 6.22 是一款高效、稳定和功能丰富的压缩文件管理软件。它提供了多种压缩和解压缩选项,支持众多文件格式,且操作简单便捷。无论从个人使用还是商业用途,WinRAR 都是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值