mismatching "local" filename while unzipping in python3.6 while the same works fine in python2.7

0

I am creating a zip file where two of the files are compressed using zlib and other two are uncompressed file. When I try to unzip the same I am getting below error and the content of one of the compressed file is getting overwritten by the content of uncompressed file.

zipinfo 2019-08-14T13_21_59-symptomreport.zip Archive: 2019-08-14T13_21_59-symptomreport.zip Zip file size: 2378 bytes, number of entries: 4 ?rw-r--r-- 2.0 unx 21959 b- defN 19-Aug-14 13:21 symptomreport/distro/networking_info/networking_info.output.log ?rw-r--r-- 2.0 unx 518 b- defN 19-Aug-14 13:21 symptomreport/distro/networking_info/networking_info.execution.log -rw-r--r-- 2.0 unx 140 b- stor 19-Aug-14 13:21 symptomreport/metadata.yaml ?rw------- 2.0 unx 1205 b- stor 19-Aug-14 13:21 symptomreport/execution.log 4 files, 23822 bytes uncompressed, 3534 bytes compressed: 85.2%

unzip 2019-08-14T13_21_59-symptomreport.zip Archive: 2019-08-14T13_21_59-symptomreport.zip symptomreport/distro/networking_info/networking_info.output.log: mismatching "local" filename (symptomreport/metadata.yaml), continuing with "central" filename version replace symptomreport/distro/networking_info/networking_info.output.log? [y]es, [n]o, [A]ll, [N]one, [r]ename:

The compressed files are being passed as stream data to zipfile and being compressed using zlib. The stream data is output of some command linux commands as tar object.

class 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

I want the content to be proper after unzipping without overwriting the content.

python
python-3.x
python-2.7
asked on Stack Overflow Aug 22, 2019 by Nithin CK • edited Aug 22, 2019 by Nithin CK

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0