-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbk_unzip.py
47 lines (37 loc) · 1.18 KB
/
gbk_unzip.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# unzip-gbk.py
import os
import sys
import zipfile
def dirName(dir, fileName):
return dir + os.path.sep + fileName
reload(sys)
sys.setdefaultencoding('utf-8')
print sys.getdefaultencoding()
def doUnzip(target):
print "Processing Zip:" + target
targetZip = zipfile.ZipFile(target, "r")
targetDir = os.path.dirname(targetZip.filename)
print targetZip.filename
print targetDir
file = zipfile.ZipFile(target, "r")
for name in file.namelist():
utf8name = name.decode("gb18030", errors='ignore')
print "Processing zipFile:" + utf8name
pathname = os.path.dirname(utf8name)
dir = dirName(targetDir, pathname)
if not os.path.exists(dir) and dir != "":
os.makedirs(dir)
data = file.read(name)
if not os.path.exists(dirName(targetDir, utf8name)):
fo = open(dirName(targetDir, utf8name), "w")
fo.write(data)
fo.close()
file.close()
count = 0
for f in sys.stdin:
print "----------------do unzip----------------"
doUnzip(f.strip().replace("\n", ""))
count = count + 1
print "unzip finish,file count:" + count.__str__()