Last active
December 20, 2015 07:59
-
-
Save zhasm/6097548 to your computer and use it in GitHub Desktop.
unzip gbk zipfile into utf8 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
# | |
# Author: Rex Zhang | |
# Create Time: 2013-07-28 12:32 | |
# File name: unzipgbk.py | |
""" | |
./unzipgbk | |
unzip gbk zipfile into utf8 files | |
usage: ./unzipgbk File.zip [File1.zip ... ] | |
./unzipgbk 吉卜力.-.\[街头自动风琴演奏宫崎骏的世界\].专辑.\(ape\).zip | |
creating path ストリートオルガンが奏でる宮崎駿の世界 | |
[1/16] ストリートオルガンが奏でる宮崎駿の世界/01.人生のメリーゴーランド.ape ... | |
[2/16] ストリートオルガンが奏でる宮崎駿の世界/02.世界の約束.ape ... | |
[3/16] ストリートオルガンが奏でる宮崎駿の世界/03.となりのトトロ.ape ... | |
[4/16] ストリートオルガンが奏でる宮崎駿の世界/04.さんぽ.ape ... | |
[5/16] ストリートオルガンが奏でる宮崎駿の世界/05.風のとおり道.ape ... | |
[6/16] ストリートオルガンが奏でる宮崎駿の世界/06.もののけ姫.ape ... | |
[7/16] ストリートオルガンが奏でる宮崎駿の世界/07.君をのせて.ape ... | |
[8/16] ストリートオルガンが奏でる宮崎駿の世界/08.晴れた日に.ape ... | |
[9/16] ストリートオルガンが奏でる宮崎駿の世界/09.やさしさに包まれたなら.ape ... | |
[10/16] ストリートオルガンが奏でる宮崎駿の世界/10.カントリーロード.ape ... | |
[11/16] ストリートオルガンが奏でる宮崎駿の世界/11.アドリアの海へ.ape ... | |
[12/16] ストリートオルガンが奏でる宮崎駿の世界/12.鳥の人 -エンディング-.ape ... | |
[13/16] ストリートオルガンが奏でる宮崎駿の世界/13.風の谷のナウシカ.ape ... | |
[14/16] ストリートオルガンが奏でる宮崎駿の世界/14.ふたたび.ape ... | |
[15/16] ストリートオルガンが奏でる宮崎駿の世界/15.いつも何度でも.ape ... | |
""" | |
from zipfile import ZipFile | |
import sys | |
import os | |
def unzip(zf): | |
source = ZipFile(zf, 'r') | |
total = len(source.filelist) | |
index = 0 | |
for file in source.filelist: | |
if '/' in file.filename: | |
dirname = os.path.sep.join(file.filename.split('/')[:-1]) | |
dirname = dirname.decode('gbk').encode('utf-8') | |
if not os.path.exists(dirname): | |
print 'creating path %s' % dirname | |
try: | |
os.popen('mkdir -p "%s"' % dirname) | |
except Exception as e: | |
print 'create path (%s) error: %s' % (dirname, str(e)) | |
#ignore path | |
if file.filename.endswith('/'): | |
continue | |
try: | |
index += 1 | |
new_name = file.filename.decode('gbk').encode('utf-8') | |
print '[%s/%s] %s ...' % (index, total, new_name) | |
fp = open(new_name, 'wb') | |
fp.write(source.read(file)) | |
fp.close() | |
except Exception as e: | |
print 'error: ', e | |
source.close() | |
if __name__ == '__main__': | |
if len(sys.argv) <= 1: | |
print '''unzip gbk zipfile into utf8 files\nusage: %s File.zip [File1.zip ... ]''' % sys.argv[0] | |
exit(0) | |
for f in sys.argv[1:]: | |
unzip(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment