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.
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