我正在使用 python gzip 库来扩展文件,其中一些已损坏。确切的错误是这样的:
解压时出现错误-3:无效的 block 类型
是否可以读取文件断点之前的所有数据,或者以某种方式跳过断点并读取前后的内容?压缩文件基本上是一行行的文本,我想尽可能多地恢复数据。
谢谢
最佳答案
希望有人觉得这有用:
# http://stackoverflow.com/questions/2423866/python-decompressing-gzip-chunk-by-chunk
# http://stackoverflow.com/questions/3122145/zlib-error-error-3-while-decompressing-incorrect-header-check/22310760
def read_corrupted_file(filename, CHUNKSIZE=1024):
d = zlib.decompressobj(zlib.MAX_WBITS | 32)
with open(filename, 'rb') as f:
result_str = ''
buffer=f.read(CHUNKSIZE)
try:
while buffer:
result_str += d.decompress(buffer)
buffer=f.read(CHUNKSIZE)
except Exception as e:
print 'Error: %s -> %s' % (filename, e.message)
return result_str
关于python - 如何从 python 中损坏的 gzip 文件中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794514/