DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

python unicode字符编解码问题参见【Python笔记2.1】
python中用zipfile解压zip包网上资料一堆,这里就不多说了。
下面使用【Python笔记2.1】中总结出来的字符编解码函数来解决zipfile解压zip包的问题。时间仓促,直接上代码。

完整示例代码(含【Python笔记2.1】中的代码)

# -*- coding: utf-8 -*-
#!/usr/bin/env python2
"""
Created on Mon Jul 23 15:40:00 2018

@author: 
"""
import os
import zipfile


# UTF_8_BOM = b'\xef\xbb\xbf'


def un_zip(filepath, dst_dir):
    try:
        encoding = 'utf-8'
        dst_dir = try_encode(dst_dir, encoding)
        zip_file = zipfile.ZipFile(filepath)
        for names in zip_file.namelist():
            unzip_file = zip_file.extract(names, dst_dir)
            rename_file = os.path.join(dst_dir, try_encode(str_decode(names), encoding))
            os.rename(unzip_file, rename_file)
        zip_file.close()
        return 0, 'unzip Success'
    except Exception as err:
        print('[un_zip]: Exception.ERROR: ', err)
        return -1, 'ERROR, unzip error! please upload the zip package named in utf-8 format.'


def try_encode(s, encoding="utf-8"):
    if s is None:
        print('[tryEncode]: input param None!')
        return s
    try:
        return s.encode(encoding)
    except UnicodeEncodeError as err:
        print(err)


def try_decode(s, decoding="utf-8"):
    try:
        return s.decode(decoding)
    except UnicodeDecodeError as err:
        print(err)


def str_decode(string):
    while True:
        dec = try_decode(string, "utf-8")
        if dec is not None:
            break
        dec = try_decode(string, "ascii")
        if dec is not None:
            break
        dec = try_decode(string, "GB2312")
        if dec is not None:
            break
        dec = try_decode(string, "GBK")
        if dec is not None:
            break
        dec = try_decode(string, "Big5")
        if dec is not None:
            break
        print('[str_decode]: unknown encoding')
        dec = None
        break

    return dec
 
posted on   DoubleLi  阅读(136)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2019-01-30 双网卡单IP实现网卡冗余与负载均衡
2018-01-30 live555源码分析----RSTPServer创建过程分析
点击右上角即可分享
微信分享提示