Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### facebook.com/pymobile ####
- ''' Tetap hargai karya orang lain key... '''
- import zipfile
- import os
- class Zip :
- __module__ = __name__
- __module__ = __name__
- def jipfolder(s, path, relnem, arch):
- paths = os.listdir(path)
- for p in paths:
- p1 = os.path.join(path, p)
- p2 = os.path.join(relnem, p)
- if os.path.isdir(p1) :
- s.jipfolder(p1, p2, arch)
- else :
- arch.write(p1, p2)
- def pack(s, path, namajip):
- arch = zipfile.ZipFile(namajip, 'w', zipfile.ZIP_DEFLATED)
- if os.path.isdir(path) :
- relnem = ''
- s.jipfolder(path, relnem, arch)
- else :
- relnem = os.path.basename(path)
- arch.write(path, relnem)
- arch.close()
- def unpack(self, file, dir):
- try :
- if not dir.endswith(':') and not os.path.exists(dir) :
- os.mkdir(dir)
- else :
- pass
- zf = zipfile.ZipFile(file)
- self._createstructure(file, dir)
- num_files = len(zf.namelist())
- for name in zf.namelist():
- if not name.endswith('/') :
- try :
- path, name1 = os.path.split(os.path.join(dir, name))
- os.makedirs(path)
- except :
- pass
- zi = zf.getinfo(name)
- if zi.file_size > 3145728 :
- continue
- outfile = open(os.path.join(dir, name), 'wb')
- outfile.write(zf.read(name))
- return True
- except :
- return False
- def _createstructure(self, file, dir):
- self._makedirs(self._listdirs(file), dir)
- def create_necessary_paths(filename):
- try :
- path, name = os.path.split(filename)
- os.makedirs(path)
- except :
- pass
- def _makedirs(self, directories, basedir):
- for dir in directories:
- curdir = os.path.join(basedir, dir)
- if not os.path.exists(curdir) :
- os.mkdir(curdir)
- def _listdirs(self, file):
- zf = zipfile.ZipFile(file)
- dirs = []
- for name in zf.namelist():
- if name.endswith('/') :
- dirs.append(name)
- dirs.sort()
- return dirs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement