计算大 GZIPPED 文件未压缩大小的最快方法

djh*_*rld 31 compression gzip

一旦文件被 gzip 压缩,是否有一种方法可以快速查询它以说明未压缩文件的大小(无需解压缩),尤其是在未压缩文件的大小 > 4GB 的情况下。

根据 RFC https://tools.ietf.org/html/rfc1952#page-5,您可以查询文件的最后 4 个字节,但如果未压缩文件大于 4GB,则该值仅表示uncompressed value modulo 2^32

这个值也可以通过运行来检索gunzip -l foo.gz,但是“未压缩”列只是uncompressed value modulo 2^32再次包含,大概是因为它正在如上所述读取页脚。

我只是想知道是否有一种方法无需先解压缩即可获取未压缩文件的大小,这在 gzip 文件包含 50GB+ 数据并且需要一段时间才能使用以下方法解压缩的情况下特别有用 gzcat foo.gz | wc -c


编辑: OSX 附带mangzip实用程序页面中公开承认了 4GB 的限制( Apple gzip 242)

  BUGS
    According to RFC 1952, the recorded file size is stored in a 32-bit
    integer, therefore, it can not represent files larger than 4GB. This
    limitation also applies to -l option of gzip utility.
Run Code Online (Sandbox Code Playgroud)

Ste*_*itt 12

我相信最快的方法是修改,gzip以便在详细模式下测试输出解压缩的字节数;在我的系统上,有一个 7761108684 字节的文件,我得到

% time gzip -tv test.gz
test.gz:     OK (7761108684 bytes)
gzip -tv test.gz  44.19s user 0.79s system 100% cpu 44.919 total

% time zcat test.gz| wc -c
7761108684
zcat test.gz  45.51s user 1.54s system 100% cpu 46.987 total
wc -c  0.09s user 1.46s system 3% cpu 46.987 total
Run Code Online (Sandbox Code Playgroud)

要修改 gzip(1.6,在 Debian 中可用),补丁如下:

--- a/gzip.c
+++ b/gzip.c
@@ -61,6 +61,7 @@
 #include <stdbool.h>
 #include <sys/stat.h>
 #include <errno.h>
+#include <inttypes.h>

 #include "closein.h"
 #include "tailor.h"
@@ -694,7 +695,7 @@

     if (verbose) {
         if (test) {
-            fprintf(stderr, " OK\n");
+            fprintf(stderr, " OK (%jd bytes)\n", (intmax_t) bytes_out);

         } else if (!decompress) {
             display_ratio(bytes_in-(bytes_out-header_bytes), bytes_in, stderr);
@@ -901,7 +902,7 @@
     /* Display statistics */
     if(verbose) {
         if (test) {
-            fprintf(stderr, " OK");
+            fprintf(stderr, " OK (%jd bytes)", (intmax_t) bytes_out);
         } else if (decompress) {
             display_ratio(bytes_out-(bytes_in-header_bytes), bytes_out,stderr);
         } else {
Run Code Online (Sandbox Code Playgroud)


小智 -1

如果您需要压缩文件或文件集的大小,最好的选择是使用tar -ztar -j而不是包含未压缩文件大小的gzipas 。tar用于lesspipe查看文件列表:

aptitude install lesspipe
lesspipe <compressed file> | less
Run Code Online (Sandbox Code Playgroud)

如果less配置为使用lesspipe

less <compressed file>
Run Code Online (Sandbox Code Playgroud)

请记住,这可能需要很长时间。然而,您的系统仍然保持响应,这允许您终止解压过程。

另一种方法是记录压缩率并查询该 [text] 文件:

gzip --verbose file 2>&1 | tee file.gz.log
file:    64.5% -- replaced with file.gz
Run Code Online (Sandbox Code Playgroud)

不过,它需要计算才能找到真实的文件大小。

您也可以对 执行相同的操作tar,这实际上是我对大型备份所做的操作,因为它可以防止运行整个解压缩过程以仅获取文件大小或名称等。

  • tar.gz 是否也必须完全解压才能获取所有文件的列表? (2认同)