Decode the bytes
Following piece of code is from zipfile.py.
self.fp.write(zinfo.FileHeader())
def FileHeader(self):
开发者_开发问答 header = struct.pack(structFileHeader, stringFileHeader,
self.extract_version, self.reserved, flag_bits,
self.compress_type, CRC, dosdate, CRC,
compress_size, file_size,
len(filename), len(extra))
return header + filename + extra
In some other file:
// Some other codes.....
zip_file = zip_dir(self.upload_dir)
zip_file.getvalue()
My Question:
The result of 'zip_file.getvalue()' is as follows:
...b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x00\x00O>\x9f\xec\x04\xd0\x06\x00\x00\x00\x06\x00\x00\x00\n\x00\x00\x00index.htmlyellowPK\x01\x02\x14\x03\x14\x00\x00\x00\x00\x00\x84\x93O>\x9f\xec\x04\xd0\x06\x00\x00\x00\x06\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00index.htmlPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x008\x00\x00\x00.\x00\x00\x00\x00\x00'
If I try to decode i.e.
zip_file.getvalue().decode()
it says:
'utf8' codec can't decode byte 0x9f in position 14: unexpected code byte
Is it possible to decode the above?
There's nothing to decode. The bytes make up a zip file, and you can extract the contents of the archive with creative use of zipfile
.
精彩评论