2009-12-27 84 views
6

根據gz的具體情況,文件大小保存在.gz文件的最後4個字節中。在64位平臺上獲取非常大的.gz文件的文件大小

我已創建了

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

2個文件我gziped他們

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的/零而不是/ dev/urandom的

+0

'的dd尋求= 10G,如果=的/ dev/=零out.dat數= 0'對於大多數文件系統更得心應手 – nodakai 2015-08-11 08:00:08

回答

8

沒有一個。

獲得壓縮流的確切大小的唯一方法是實際去解壓縮它(即使將所有內容寫入/ dev/null並只計算字節數)。

其值得注意的是ISIZE被定義爲

ISIZE(輸入大小)
這包含原始(未壓縮)的輸入的大小
數據模2^32。

中的gzip RFC

所以它實際上不是在32位屏障破,你看到的是預期的行爲。

2

我沒有試過用大小的文件,你提到,但我經常與

zcat file.gz | wc -c 
找到 未壓縮大小的.gz文件

當我不想離開未壓縮的文件時,或者再次壓縮它。

很明顯,數據是未壓縮的,但是然後通過管道傳送到wc

無論如何,這是值得一試的。

編輯:當我試圖從/ dev創建帶有數據的5G文件/隨機它生產規模51.2億的文件5G,雖然我的文件管理器報告以此爲4.8G

然後,我gzip 5G壓縮它,結果5G.gz是相同的大小(沒有太多的壓縮隨機數據)。

然後zcat 5G.gz | wc -c報告與原始文件大小相同:5120000000字節。無論如何,我的建議似乎對這個試驗有效。

感謝你等着

+0

是感謝, 但我的問題更多的是在某種意義上。 如何在沒有實際解壓縮的情況下獲得未壓縮的文件大小? 對於小於32位文件的文件。你可以提取最後4個字節。對於較大的文件來說這是不可能的,正如你所做的那樣,唯一的辦法就是做一個解壓縮。 – monkeyking 2009-12-28 07:52:56

+0

但我的方法執行的解壓縮不影響原始壓縮文件,並沒有創建額外的未壓縮文件。之後沒有清理。我認爲值得注意的是,你接受的答案是說減壓是獲得確切大小的唯一方法。這是有道理的,*找出盒子裏的東西的唯一方法就是打開它*。 – pavium 2009-12-28 08:36:22

+0

是的,它並不影響原始文件,但我的擔心並不是「不接觸」文件,而僅僅是速度問題。如果我想爲整個數據分配一個數組,那麼我應該知道它的大小。這需要進行解壓縮,然後再對實際數據複製進行解壓縮。 如果文件小於2.1 gig,則這不是必需的。 STD gunzip也能可以解壓縮到stdout,做 gunzip解-c文件|廁所-c 但感謝您的輸入:) – monkeyking 2009-12-28 15:24:38

0

的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. 
+0

此解決方案僅適用於磁盤文件,而不是流(原始問題未指定流,因此在這方面它是可行的答案)。 不幸的是,對於大於2^32-1字節的文件大小,未壓縮大小以模2^32顯示,因此不可靠。 – Curt 2016-02-06 01:14:54