【问题标题】:get the filesize of very large .gz file on a 64bit platform在 64 位平台上获取非常大的 .gz 文件的文件大小
【发布时间】:2010-12-30 06:00:00
【问题描述】:

根据 gz 的规范,文件大小保存在 .gz 文件的最后 4 个字节中。

我创建了 2 个文件

dd if=/dev/urandom of=500M bs=1024 count=500000
dd if=/dev/urandom of=5G bs=1024 count=5000000

我压缩了它们

gzip 500M 5G

我检查了最后 4 个字节

tail -c4 500M|od -I      (returns 512000000 as expected)
tail -c4 5G|od -I        (returns 825032704 as not expected)

似乎击中了不可见的 32 位屏障,使得写入 ISIZE 的值完全是无稽之谈。这比他们使用一些错误位更烦人。

有谁知道从 .gz 中获取未压缩的 .gz 文件大小而不提取它的方法?

谢谢

规格:http://www.gzip.org/zlib/rfc-gzip.html

编辑: 如果有人想试试,你可以使用 /dev/zero 代替 /dev/urandom

【问题讨论】:

  • dd seek=10G if=/dev/zero of=out.dat count=0 对于大多数文件系统来说更方便

标签: 64-bit 32-bit filesize gunzip gzip


【解决方案1】:

没有。

获得压缩流的确切大小的唯一方法是实际去解压缩它(即使您将所有内容都写入 /dev/null 并只计算字节数)。

值得注意的是,ISIZE被定义为

ISIZE(输入尺寸)
这包含原始(未压缩)输入的大小
数据模 2^32。

在 gzip RFC 中,因此它实际上并没有在 32 位障碍处打破,您所看到的是预期行为。

【讨论】:

    【解决方案2】:

    我没有用你提到的大小的文件尝试过这个,但我经常发现 .gz 文件的未压缩大小

    zcat file.gz | wc -c
    

    当我不想把未压缩的文件放在一边,或者不想再压缩它时。

    显然,数据未压缩,但随后通过管道传送到wc

    无论如何,值得一试。

    编辑:当我尝试使用来自 /dev/random 的数据创建一个 5G 文件时,它生成了一个大小为 5120000000 的文件 5G,尽管我的文件管理器将此报告为 4.8G

    然后我用gzip 5G压缩它,结果5G.gz大小相同(随机数据压缩不多)。

    然后zcat 5G.gz | wc -c 报告与原始文件相同的大小:5120000000 字节。所以无论如何,我的建议似乎对这次试验奏效了。

    感谢您的等待

    【讨论】:

    • 是的,谢谢,但我的问题更多是在某种意义上。如何在不实际进行解压缩的情况下获得未压缩的文件大小。对于小于 32 位文件的文件。您可以只提取最后 4 个字节。对于较大的文件,这是不可能的,正如您所做的那样,唯一的方法是进行解压缩。
    • 但是我的方法进行了解压,不影响原始压缩文件,也没有创建额外的未压缩文件。之后就没有清理的余地了。而且我认为值得注意的是,您接受的答案说解压缩是获得确切大小的唯一方法。 找出盒子里有什么的唯一方法就是打开它,这是有道理的。
    • 是的,它没有影响原始文件,但我担心的不是“不接触”文件,而仅仅是速度问题。如果我想为整个数据分配一个数组,那么我应该知道大小。这需要进行解压缩,然后对实际数据副本进行另一次解压缩。如果文件小于 2.1 gig,则不需要这样做。 std gunzip 也可以解压到stdout,做gunzip -c file |wc -c 不过谢谢你的输入:)
    • 抛开所有 cmets:如果所有其他方法都失败了,一个实用的解决方案。
    【解决方案3】:

    gzip 确实有一个 -l 选项:

           -l --list
              For each compressed file, list the following fields:
    
                  compressed size: size of the compressed file
                  uncompressed size: size of the uncompressed file
                  ratio: compression ratio (0.0% if unknown)
                  uncompressed_name: name of the uncompressed file
    
              The uncompressed size is given as -1 for files not in gzip format, such as compressed .Z files. To
              get the uncompressed size for such a file, you can use:
    
                  zcat file.Z | wc -c
    
              In combination with the --verbose option, the following fields are also displayed:
    
                  method: compression method
                  crc: the 32-bit CRC of the uncompressed data
                  date & time: time stamp for the uncompressed file
    
              The compression methods currently supported are deflate, compress, lzh (SCO compress -H) and pack.
              The crc is given as ffffffff for a file not in gzip format.
    
              With --name, the uncompressed name,  date and time  are those stored within the compress  file  if
              present.
    
              With --verbose, the size totals and compression ratio for all files is also displayed, unless some
              sizes are unknown. With --quiet, the title and totals lines are not displayed.
    

    【讨论】:

    • 此解决方案仅适用于磁盘文件,不适用于流(原始问题未指定流,因此在这方面它是一个可行的答案)。不幸的是,对于大于 2^32-1 字节的文件大小,未压缩的大小显示为模 2^32,因此不可靠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 2020-10-17
    • 2016-01-29
    • 1970-01-01
    相关资源
    最近更新 更多