为什么 tar 存档占用 1MB?它不应该只占用 1KB 吗?
Why does tar archive take up 1MB? Shouldn't it take up only 1KB?
我正在阅读 difference between tar and zip and gz files 上的一篇文章。
我很难理解作者是如何想出 1MB 作为 tar 存档的大小的:
One key thing to remember is a plain tar file is just an archive whose data are not compressed. In other words, if you tar 100 files of 50kB, you will end up with an archive whose size will be around 5000kB. The only gain you can expect using tar alone would be by avoiding the space wasted by the file system as most of them allocate space at some granularity (for example, on my system, a one byte long file uses 4kB of disk space, 1000 of them will use 4MB but the corresponding tar archive “only” 1MB).
存档的大小不应该只有 1KB 左右吗?这是我的推理:
如果您保存文件系统保存的额外 space,那么 1000 个文件 X 每个文件 1 字节应该只消耗 1000 字节或 1 KB。所以 tar 存档的大小应该在 1KB 左右。为什么是1MB。
我也在我的系统(MACOSX 终端)上测试了这样的场景:
mkdir test
cd test
for i in {1..1000}; do echo "" > $i.txt; done
cd ..
tar -cf tarredFile.tar test
ls -l tarredFile.tar
甚至文件系统也显示 tar 存档有 1MB。总之,我肯定知道我的推理是不正确的,但我不知道为什么。我忽略了什么?
tar格式以512字节块写入。每个单字节文件占用 512 字节的文件头,其中包含文件名,512 字节的文件内容,其中只有一个字节是有效的。所以每个文件最少 1024 字节。
大部分是零,所以它压缩了很多。 gzip 将其压缩到大约 9K。请注意,这距离 1K 还很远,因为您还需要将文件的 名称 存储在存档中。
我正在阅读 difference between tar and zip and gz files 上的一篇文章。
我很难理解作者是如何想出 1MB 作为 tar 存档的大小的:
One key thing to remember is a plain tar file is just an archive whose data are not compressed. In other words, if you tar 100 files of 50kB, you will end up with an archive whose size will be around 5000kB. The only gain you can expect using tar alone would be by avoiding the space wasted by the file system as most of them allocate space at some granularity (for example, on my system, a one byte long file uses 4kB of disk space, 1000 of them will use 4MB but the corresponding tar archive “only” 1MB).
存档的大小不应该只有 1KB 左右吗?这是我的推理:
如果您保存文件系统保存的额外 space,那么 1000 个文件 X 每个文件 1 字节应该只消耗 1000 字节或 1 KB。所以 tar 存档的大小应该在 1KB 左右。为什么是1MB。
我也在我的系统(MACOSX 终端)上测试了这样的场景:
mkdir test
cd test
for i in {1..1000}; do echo "" > $i.txt; done
cd ..
tar -cf tarredFile.tar test
ls -l tarredFile.tar
甚至文件系统也显示 tar 存档有 1MB。总之,我肯定知道我的推理是不正确的,但我不知道为什么。我忽略了什么?
tar格式以512字节块写入。每个单字节文件占用 512 字节的文件头,其中包含文件名,512 字节的文件内容,其中只有一个字节是有效的。所以每个文件最少 1024 字节。
大部分是零,所以它压缩了很多。 gzip 将其压缩到大约 9K。请注意,这距离 1K 还很远,因为您还需要将文件的 名称 存储在存档中。