大容量GZIPPEDファイルの圧縮されていないサイズを計算する最速の方法

大容量GZIPPEDファイルの圧縮されていないサイズを計算する最速の方法

ファイルをgzipで圧縮した後、特に圧縮されていないファイルサイズが4 GBを超える場合は、解凍せずに圧縮されていないファイルのサイズをすばやく照会する方法はありますか?

RFCによるとhttps://www.rfc-editor.org/rfc/rfc1952#page-5ファイルの最後の4バイトを照会できますが、圧縮されていないファイルが4 GBを超える場合、値はuncompressed value modulo 2^32

を実行して値を取得することもできますが、gunzip -l foo.gz「非圧縮」列がuncompressed value modulo 2^32再び含まれます。おそらく、上記のようにフッターを読んでいるからです。

まず、解凍せずに圧縮されていないファイルのサイズを取得する方法があるかどうか疑問に思います。これは、gzip圧縮ファイルに50 GB以上のデータが含まれていて、次の方法を使用して解凍するのに時間がかかる場合に特に便利です。gzcat foo.gz | wc -c


編集する:manOSXに付属のユーティリティページでは、4 GBの制限を公に認めます()。gzipApple 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.

ベストアンサー1

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

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 {

同様の方法1.11 以降のバージョンに実装され、gzip含まれる予定です。gzip -l次に、データを解凍してサイズを決定します。

おすすめ記事