Объединение и сжатие с использованием «tar czf» и «tar + gzip». Результирующий файл в обоих случаях - packname.tar.gz, но почему размеры разные? - PullRequest
0 голосов
/ 28 января 2020

Есть три текстовых файла. test1, test2 и test3 с размерами файлов как: test1 - 121 B test2 - 4 B test3 - 26 B Я пытаюсь объединить и сжать эти файлы, используя разные методы. * * Метод тысячу один-А Объедините файлы, используя 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 означают байты.

1 Ответ

0 голосов
/ 29 января 2020

Если вы распакуете архив, созданный вашим шагом 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 
...