python压缩4Gb bz2 EOFError:流的末尾已经找到了嵌套的子文件夹

问题描述 投票:0回答:1

我正在尝试使用python从压缩文件bz2中读取特定文件。

tar = tarfile.open(filename, "r|bz2", bufsize=57860311)

for tarinfo in tar:
    print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
    if tarinfo.isreg():
        print "a regular file."
        # read the file
        f = tar.extractfile(tarinfo)
        #print f.read()
    elif tarinfo.isdir():
        print "a directory."
    else:
        print "something else."
tar.close()

但最后我得到了错误:

/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.pyc in read(self, size)
    577             buf = "".join(t)
    578         else:
--> 579             buf = self._read(size)
    580         self.pos += len(buf)
    581         return buf

/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.pyc in _read(self, size)
    594                 break
    595             try:
--> 596                 buf = self.cmp.decompress(buf)
    597             except IOError:
    598                 raise ReadError("invalid compressed data")

EOFError: end of stream was already found

我还尝试通过'tar.list()'再次列出tar中的文件...

-rwxr-xr-x lindauer/or3uunp          0 2013-05-21 00:58:36 r3.2/
-rw-r--r-- lindauer/or3uunp       6057 2012-01-05 14:41:00 r3.2/readme.txt
-rw-r--r-- lindauer/or3uunp      44732 2012-01-04 10:08:54 r3.2/psychometric.csv
-rw-r--r-- lindauer/or3uunp   57860309 2012-01-04 09:58:20 r3.2/logon.csv

/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.pyc in _read(self, size)
    594                 break
    595             try:
--> 596                 buf = self.cmp.decompress(buf)
    597             except IOError:
    598                 raise ReadError("invalid compressed data")

EOFError: end of stream was already found

我使用tar命令列出了存档中的文件。结果如下:

tar -tvf r3.2.tar.bz2
drwxr-xr-x  0 lindauer or3uunp     0 May 21  2013 r3.2/
-rw-r--r--  0 lindauer or3uunp  6057 Jan  5  2012 r3.2/readme.txt
-rw-r--r--  0 lindauer or3uunp 44732 Jan  4  2012 r3.2/psychometric.csv
-rw-r--r--  0 lindauer or3uunp 57860309 Jan  4  2012 r3.2/logon.csv
-rw-r--r--  0 lindauer or3uunp 12494829865 Jan  5  2012 r3.2/http.csv
-rw-r--r--  0 lindauer or3uunp  1066622500 Jan  5  2012 r3.2/email.csv
-rw-r--r--  0 lindauer or3uunp   218962503 Jan  5  2012 r3.2/file.csv
-rw-r--r--  0 lindauer or3uunp    29156988 Jan  4  2012 r3.2/device.csv
drwxr-xr-x  0 lindauer or3uunp           0 May 20  2013 r3.2/LDAP/
-rw-r--r--  0 lindauer or3uunp      140956 Jan  4  2012 r3.2/LDAP/2011-01.csv
-rw-r--r--  0 lindauer or3uunp      147370 Jan  4  2012 r3.2/LDAP/2010-05.csv
-rw-r--r--  0 lindauer or3uunp      149221 Jan  4  2012 r3.2/LDAP/2010-02.csv
-rw-r--r--  0 lindauer or3uunp      141717 Jan  4  2012 r3.2/LDAP/2010-12.csv
-rw-r--r--  0 lindauer or3uunp      148931 Jan  4  2012 r3.2/LDAP/2010-03.csv
-rw-r--r--  0 lindauer or3uunp      147370 Jan  4  2012 r3.2/LDAP/2010-04.csv
-rw-r--r--  0 lindauer or3uunp      149793 Jan  4  2012 r3.2/LDAP/2009-12.csv
-rw-r--r--  0 lindauer or3uunp      143979 Jan  4  2012 r3.2/LDAP/2010-09.csv
-rw-r--r--  0 lindauer or3uunp      145591 Jan  4  2012 r3.2/LDAP/2010-07.csv
-rw-r--r--  0 lindauer or3uunp      139444 Jan  4  2012 r3.2/LDAP/2011-03.csv
-rw-r--r--  0 lindauer or3uunp      142347 Jan  4  2012 r3.2/LDAP/2010-11.csv
-rw-r--r--  0 lindauer or3uunp      138285 Jan  4  2012 r3.2/LDAP/2011-04.csv
-rw-r--r--  0 lindauer or3uunp      149793 Jan  4  2012 r3.2/LDAP/2010-01.csv
-rw-r--r--  0 lindauer or3uunp      146008 Jan  4  2012 r3.2/LDAP/2010-06.csv
-rw-r--r--  0 lindauer or3uunp      144711 Jan  4  2012 r3.2/LDAP/2010-08.csv
-rw-r--r--  0 lindauer or3uunp      137967 Jan  4  2012 r3.2/LDAP/2011-05.csv
-rw-r--r--  0 lindauer or3uunp      140085 Jan  4  2012 r3.2/LDAP/2011-02.csv
-rw-r--r--  0 lindauer or3uunp      143420 Jan  4  2012 r3.2/LDAP/2010-10.csv
-r--r--r--  0 lindauer or3uunp        3923 Jan  4  2012 r3.2/license.txt

我认为这是因为存档有子文件夹,并且由于某种原因python库在处理子文件夹提取时遇到问题?

我也尝试手动打开tar文件,我没有问题所以我认为文件没有损坏。任何帮助赞赏。

python compression tar eof bz2
1个回答
0
投票

评论:我尝试了debug = 3,我得到:ReadError:错误的校验和

找到以下相关信息:

tar: directory checksum error

原因 来自tar(1)的此错误消息表示目录的校验和及其从磁带读取的文件与标头块中公布的校验和不匹配。通常此消息表示错误的阻塞因素,尽管它可能表示磁带上的数据已损坏。 行动 要解决此问题,请确保在命令行上指定的阻止因子(在-b之后)与最初指定的阻塞因子匹配。如果有疑问,请省略块大小,让tar(1)自动确定它。如果该补救措施无效,则磁带数据可能已损坏。

SE:tar-ignore-or-fix-checksum 我会尝试-i开关,看看你是否可以忽略和有关EOF的消息。

 -i, --ignore-zeros         ignore zeroed blocks in archive (means EOF)

 $ tar xivf backup.tar

bugs.python.org:tarfile-headererror tarfile.py中的注释读取(不知道文件的日期!):

-           # We shouldn't rely on this checksum, because some tar programs
-           # calculate it differently and it is merely validating the
-           # header block.

ReadError:意外的数据结束


来自tarfile Documentation

tarfile模块定义了以下例外: exception tarfile.ReadError 在打开tar存档时引发,tar文件模块无法处理或者某种程度上无效。

首先,尝试使用另一个tar存档文件来验证您的python环境。 其次,检查tar archiv文件是否符合以下格式:

tarfile.DEFAULT_FORMAT

创建存档的默认格式。这是目前的GNU_FORMAT。

第三,而不是使用tarfile.open(...),创建一个tarfile实例,尝试使用以下,设置debug=3

tar = tarfile.TarFile(name=filename, debug=3)
tar.open()
...

class tarfile.TarFile(name = None,mode ='r',fileobj = None,format = DEFAULT_FORMAT,tarinfo = TarInfo,dereference = False,ignore_zeros = False,encoding = ENCODING,errors ='surrogateescape',pax_headers = None,debug = 0,errorlevel = 0)

© www.soinside.com 2019 - 2024. All rights reserved.