# full list of codec: https://docs.python.org/2/library/codecs.html # note: # - input from command line is using commandline system default locale encoding # - it read the zip file path in unicode format with the given decode method # - if you use python print method to print those unicode path in window command windows, # it may error when system default locale codec can't print those unicode characters import zipfile import os.path import os import sys class ZFile(object): def __init__(self, filename, mode='r', basedir=''): self.filename = filename self.mode = mode if self.mode in ('w', 'a'): self.zfile = zipfile.ZipFile(filename, self.mode, compression=zipfile.ZIP_DEFLATED) else: self.zfile = zipfile.ZipFile(filename, self.mode) self.basedir = basedir if not self.basedir: self.basedir = os.path.dirname(filename) def addfile(self, path, arcname=None): path = path.replace('//', '/') if not arcname: if path.startswith(self.basedir): arcname = path[len(self.basedir):] else: arcname = '' self.zfile.write(path, arcname) def addfiles(self, paths): for path in paths: if isinstance(path, tuple): self.addfile(*path) else: self.addfile(path) def close(self): self.zfile.close() def extract_to(self, path, decode): for p in self.zfile.namelist(): self.extract(p, path, decode) def extract(self, filename, path, decode): if not filename.endswith('/'): f = os.path.join(path, filename.decode(decode)) #gbk,gb18030, GB2312, utf-8 dir = os.path.dirname(f) if not os.path.exists(dir): os.makedirs(dir) file(f, 'wb').write(self.zfile.read(filename)) def create(zfile, files): z = ZFile(zfile, 'w') z.addfiles(files) z.close() def extract(zfile, path, decode): z = ZFile(zfile) z.extract_to(path, decode) z.close() if __name__=="__main__": extract(unicode(sys.argv[1]), u'.', sys.argv[2])