python - 解码字节

标签 python byte decode porting

以下代码来自 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

在其他文件中:

  // Some other codes.....


  zip_file = zip_dir(self.upload_dir)

  zip_file.getvalue()

我的问题:

'zip_file.getvalue()'的结果如下:

...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\x00索引htmlPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x008\x00\x00\x00.\x00\x00\x00\x00\x00'

如果我尝试解码即

zip_file.getvalue().decode()

它说:

“utf8”编解码器无法解码位置 14 中的字节 0x9f:意外代码字节

是否可以解码以上内容?

最佳答案

没有什么可以解码的。这些字节组成一个 zip 文件,您可以创造性地使用 zipfile 来提取存档的内容。

关于python - 解码字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5004201/

相关文章:

python - 如何获取 SGDClassifier(线性 SVM)的概率

c# - 如何在逐字节读取 CSV 文件时检测字节是否为换行符 - C#

scala - 使用 Scala 将 base64 解码为 ASCII

c# - 从 .NET 应用程序中读取和解码存储在图像或 PDF 文件中的 PDF-417 条码

c++ - 将 8 个 bool 值转换为一个字节的最佳方法?

ios - 解码具有不同类型的 Json 对象

c++ - Boost::Python- 可以从 dict 自动转换 --> std::map?

python - 如何使用 Python 访问受 IAP 保护的资源?

python - 从 Python 运行 Expect 脚本的最简单方法

php - Windows Phone中上传图片和php POST方法