Python3 ZipFile how to write non-utf8 filename to archive by using writestr function

372 Views Asked by At

I am writing something like auto-compress files and upload to server, and I'm using writestr to directly write bytes to ZipFile from memory. But for historical problems, I need to make it display properly on some Windows PC with GBK encoding.

Python3's str encoding is default utf-8. I want to know how to write the correct byte stream for the filename, the content can work with utf-8 so no need to care file content.

I need a code sample. Thanks. Any solution is acceptable like new a class inherit it.

1

There are 1 best solutions below

0
On BEST ANSWER

Thanks to @tripleee.

Override _encodeFilenameFlags method works:

class GBKZipInfo(zipfile.ZipInfo):
    # override _encodeFilenameFlags method, change encoding to GBK
    def _encodeFilenameFlags(self):
        try:
            return self.filename.encode('gbk'), self.flag_bits
        except UnicodeEncodeError:
            return self.filename.encode('utf-8'), self.flag_bits | 0x800