我正在创建一个zip文件,其中两个文件使用zlib压缩,而另两个是未压缩的文件。当我尝试将其解压缩时,出现以下错误,并且其中一个压缩文件的内容被未压缩文件的内容覆盖。
zipinfo 2019-08-14T13_21_59-symptomreport.zip 存档:2019-08-14T13_21_59-symptomreport.zip 压缩文件大小:2378字节,条目数:4 rw-r--r-- 2.0 unx 21959 b- defN 14年8月19日13:21 symptomreport / distro / networking_info / networking_info.output.log rw-r--r-- 2.0 unx 518 b- defN 14年8月19日13:21 symptomreport / distro / networking_info / networking_info.execution.log -rw-r--r-- 2.0 unx 140 bstor 14年8月19日13:21 symptomreport / metadata.yaml rw ------- 2.0 unx 1205 b-tor 14年8月19日13:21 symptomreport / execution.log 4个文件,23822字节未压缩,3534字节压缩:85.2%
解压缩2019-08-14T13_21_59-symptomreport.zip 存档:2019-08-14T13_21_59-symptomreport.zip symptomreport / distro / networking_info / networking_info.output.log:“本地”文件名不匹配(symptomreport / metadata.yaml), 继续以“中央”文件名版本 替换symptomreport / distro / networking_info / networking_info.output.log? [y] es,[n] o,[A] ll,[N] one,[r] ename:
压缩文件将作为流数据传递到zipfile,并使用zlib进行压缩。流数据是某些命令linux命令的输出作为tar对象。
ZipFile2(ZipFile)类:
"""Extends ZipFile so that it's possible to write files from stream input."""
def write_fp(self, fp, arcname, compress_type=ZIP_DEFLATED):
"""Put the bytes from filename into the archive under the name
arcname."""
if not self.fp:
raise RuntimeError('Attempt to write to ZIP archive that was already closed')
arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
while arcname[0x00] in (os.sep, os.altsep):
arcname = arcname[1:]
zinfo = ZipInfo(arcname)
if compress_type is None:
zinfo.compress_type = self.compression
else:
zinfo.compress_type = compress_type
zinfo.file_size = 0x00
zinfo.flag_bits = 0x00
zinfo.external_attr = 0o644 << 16
zinfo.date_time = time.localtime(time.time())
zinfo.header_offset = self.fp.tell() # Start of header bytes
self._writecheck(zinfo)
self._didModify = True
if True:
# Must overwrite CRC and sizes with correct data later
zinfo.CRC = crc = 0x00
zinfo.compress_size = compress_size = 0x00
# Compressed size can be larger than uncompressed size
zip64 = self._allowZip64 and zinfo.file_size * 1.05 \
> ZIP64_LIMIT
self.fp.write(zinfo.FileHeader(zip64))
if zinfo.compress_type == ZIP_DEFLATED:
cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
zlib.DEFLATED, -15)
else:
cmpr = None
file_size = 0x00
while 1:
buf = fp.read(1024 * 8)
if not buf:
break
file_size = file_size + len(buf)
crc = zlib.crc32(buf, crc) & 0xffffffff
if cmpr:
buf = cmpr.compress(buf)
compress_size = compress_size + len(buf)
self.fp.write(buf)
if cmpr:
buf = cmpr.flush()
compress_size = compress_size + len(buf)
self.fp.write(buf)
zinfo.compress_size = compress_size
else:
zinfo.compress_size = file_size
zinfo.CRC = crc
zinfo.file_size = file_size
if not zip64 and self._allowZip64:
if file_size > ZIP64_LIMIT:
raise RuntimeError('File size has increased during compressing')
if compress_size > ZIP64_LIMIT:
raise RuntimeError('Compressed size larger than uncompressed size')
# Seek backwards and write file header (which will now include
# correct CRC and file sizes)
position = self.fp.tell() # Preserve current position in file
self.fp.seek(zinfo.header_offset, 0x00)
self.fp.write(zinfo.FileHeader(zip64))
self.fp.seek(position, 0x00)
self.filelist.append(zinfo)
self.NameToInfo[zinfo.filename] = zinfo
我希望解压缩后内容正确而不覆盖内容。