在python中获取.gz文件的未压缩大小 [英] Get uncompressed size of a .gz file in python

查看:180
本文介绍了在python中获取.gz文件的未压缩大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gzip,tell()返回未压缩文件中的偏移量.
为了显示进度条,我想知道文件的原始(未压缩)大小.
有一种简单的方法可以找出答案吗?

Using gzip, tell() returns the offset in the uncompressed file.
In order to show a progress bar, I want to know the original (uncompressed) size of the file.
Is there an easy way to find out?

推荐答案

gzip格式指定一个名为ISIZE的字段,

The gzip format specifies a field called ISIZE that:

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

This contains the size of the original (uncompressed) input data modulo 2^32.

gzip.py 中,我假设这是您用于gzip支持的内容,有一个名为_read_eof的方法定义如下:

In gzip.py, which I assume is what you're using for gzip support, there is a method called _read_eof defined as such:

def _read_eof(self):
    # We've read to the end of the file, so we have to rewind in order
    # to reread the 8 bytes containing the CRC and the file size.
    # We check the that the computed CRC and size of the
    # uncompressed data matches the stored values.  Note that the size
    # stored is the true file size mod 2**32.
    self.fileobj.seek(-8, 1)
    crc32 = read32(self.fileobj)
    isize = U32(read32(self.fileobj))   # may exceed 2GB
    if U32(crc32) != U32(self.crc):
        raise IOError, "CRC check failed"
    elif isize != LOWU32(self.size):
        raise IOError, "Incorrect length of data produced"

您可以看到正在读取ISIZE字段,但这只是为了将其与self.size进行比较以进行错误检测.然后,这意味着GzipFile.size存储实际的未压缩大小.但是,我认为它没有公开公开,因此您可能必须破解它才能公开.不太确定,对不起.

There you can see that the ISIZE field is being read, but only to to compare it to self.size for error detection. This then should mean that GzipFile.size stores the actual uncompressed size. However, I think it's not exposed publicly, so you might have to hack it in to expose it. Not so sure, sorry.

我现在只是看了所有这些,而我还没有尝试过,所以我可能是错的.我希望这对您有用.抱歉,如果我误解了您的问题.

I just looked all of this up right now, and I haven't tried it so I could be wrong. I hope this is of some use to you. Sorry if I misunderstood your question.

这篇关于在python中获取.gz文件的未压缩大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆