使用“ tar czf”和“ tar + gzip”进行组合和压缩。在两种情况下,生成的文件都是packname.tar.gz,但是为什么大小不同?

问题描述 投票:0回答:1

有三个文本文件。 test1,test2和test3,文件大小为:测试1-121 B测试2-4 B测试3-26 B我正在尝试使用不同的方法来合并和压缩这些文件。方法-A使用tar合并文件,然后使用gzip压缩文件。$tar cf testpack1.tar test1 test2 test3$gzip testpack1.tar输出为testpack1.tar.gz,大小为276 B方法B使用tar合并并压缩文件。$tar czf testpack2.tar.gz test1 test2 test3输出为testpack2.tar.gz,大小为262 B为什么两个文件的大小不同?B平均字节。

linux compression gzip tar archive
1个回答
0
投票

如果您解压缩由步骤B创建的档案,我敢打赌它将是10240字节。这种大小差异的原因是tar将压缩档案与块大小对齐(使用零字符),但不会将未压缩档案对齐。这是GNU tar文档的摘录:

-b blocks
--blocking-factor=blocks 
Set record size to blocks * 512 bytes. 
This option is used to specify a blocking factor for the archive. When
reading or writing the archive, tar, will do reads and writes of the
archive in records of block*512 bytes. This is true even when the
archive is compressed. Some devices requires that all write operations
be a multiple of a certain size, and so, tar pads the archive out to
the next record boundary. The default blocking factor is set when tar
is compiled, and is typically 20. Blocking factors larger than 20
cannot be read by very old versions of tar, or by some newer versions
of tar running on old machines with small address spaces. With a
magnetic tape, larger records give faster throughput and fit more data
on a tape (because there are fewer inter-record gaps). If the archive
is in a disk file or a pipe, you may want to specify a smaller
blocking factor, since a large one will result in a large number of
null bytes at the end of the archive.

您可以这样创建相同的压缩tar归档文件:

tar -b 20 -cf test.tar test1 test2 test3
gzip test.tar 
© www.soinside.com 2019 - 2024. All rights reserved.