Skip to content

Instantly share code, notes, and snippets.

@codeif
Created April 20, 2017 18:04
Show Gist options
  • Save codeif/c8f4bfdf723a32a8b26052e37205ce13 to your computer and use it in GitHub Desktop.
Save codeif/c8f4bfdf723a32a8b26052e37205ce13 to your computer and use it in GitHub Desktop.
unzip.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
解决mac下解压zip文件乱码问题
代码参考:
http://stackoverflow.com/questions/9431918/extracting-zip-file-contents-to-specific-directory-in-python-2-7
http://www.zhihu.com/question/20523036/answer/35225920
http://stackoverflow.com/questions/4917284/extract-files-from-zip-without-keeping-the-structure-using-python-zipfile
编码: https://docs.python.org/2.4/lib/standard-encodings.html
"""
from __future__ import unicode_literals
import sys
import os.path
import zipfile
zip_file_name = sys.argv[1]
if not zip_file_name.endswith('.zip'):
print('文件名需要zip结尾')
# 解压到文件名的同名文件夹
target_root_path = zip_file_name[:-4]
if not os.path.exists(target_root_path):
print('创建目录')
os.makedirs(target_root_path)
zip_file = zipfile.ZipFile(zip_file_name)
for name in zip_file.namelist():
# fix mac 下乱码
try:
unicode_name = name.encode('cp437').decode('gbk')
except UnicodeDecodeError:
unicode_name = name.decode('gbk')
file_name = os.path.basename(unicode_name)
if not file_name:
continue
target_name = os.path.join(target_root_path, unicode_name)
target_path = os.path.dirname(target_name)
if not os.path.exists(target_path):
os.makedirs(target_path)
print('解压到: {0}'.format(target_name))
data = zip_file.read(name)
with open(target_name, 'wb') as f:
f.write(data)
zip_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment