为什么在stdin上gzip压缩文件会产生比作为参数指定的文件小的输出?


13

当我做:

# gzip -c foo > foo1.gz 
# gzip < foo > foo2.gz

为什么foo2.gz最终会比缩小foo1.gz

Answers:


19

因为它会保存文件名和时间戳,以便稍后解压缩后可以尝试同时还原它们。由于在您的第二个示例中已将via foo提供给它,因此它无法存储文件名和时间戳信息。gzip<stdin>

从联机帮助页:

   -n --no-name
          When compressing, do not save the original file name and time stamp by default. (The original name is always saved if the name had
          to  be truncated.) When decompressing, do not restore the original file name if present (remove only the gzip suffix from the com-
          pressed file name) and do not restore the original time stamp if present (copy it from the compressed file). This  option  is  the
          default when decompressing.

   -N --name
          When compressing, always save the original file name and time stamp; this is the default. When decompressing, restore the original
          file name and time stamp if present. This option is useful on systems which have a limit on file name  length  or  when  the  time
          stamp has been lost after a file transfer.

我在这里重新创建了这个问题:

[root@xxx601 ~]# cat /etc/fstab > file.txt
[root@xxx601 ~]# gzip < file.txt > file.txt.gz
[root@xxx601 ~]# gzip -c file.txt > file2.txt.gz
[root@xxx601 ~]# ll -h file*
-rw-r--r--. 1 root root  465 May 17 19:35 file2.txt.gz
-rw-r--r--. 1 root root 1.2K May 17 19:34 file.txt
-rw-r--r--. 1 root root  456 May 17 19:34 file.txt.gz

在我的示例中,file.txt.gz与等效foo2.gz。当使用该-n选项可以访问信息时,禁用此行为:

[root@xxx601 ~]# gzip -nc file.txt > file3.txt.gz
[root@xxx601 ~]# ll -h file*
-rw-r--r--. 1 root root  465 May 17 19:35 file2.txt.gz
-rw-r--r--. 1 root root  456 May 17 19:43 file3.txt.gz
-rw-r--r--. 1 root root 1.2K May 17 19:34 file.txt
-rw-r--r--. 1 root root  456 May 17 19:34 file.txt.gz

如您在上方所见,由于文件名和日期都省略了名称和日期,因此它们的文件大小file.txt和文件大小file3.txt匹配。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.