Advertisement
phjoe

zipman

Oct 27th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #### facebook.com/pymobile ####
  2. ''' Tetap hargai karya orang lain key... '''
  3. import zipfile
  4. import os
  5. class Zip :
  6.     __module__ = __name__
  7.     __module__ = __name__
  8.     def jipfolder(s, path, relnem, arch):
  9.         paths = os.listdir(path)
  10.         for p in paths:
  11.             p1 = os.path.join(path, p)
  12.             p2 = os.path.join(relnem, p)
  13.             if os.path.isdir(p1) :
  14.                 s.jipfolder(p1, p2, arch)
  15.             else :
  16.                 arch.write(p1, p2)
  17.  
  18.  
  19.     def pack(s, path, namajip):
  20.         arch = zipfile.ZipFile(namajip, 'w', zipfile.ZIP_DEFLATED)
  21.         if os.path.isdir(path) :
  22.             relnem = ''
  23.             s.jipfolder(path, relnem, arch)
  24.         else :
  25.             relnem = os.path.basename(path)
  26.             arch.write(path, relnem)
  27.         arch.close()
  28.  
  29.  
  30.     def unpack(self, file, dir):
  31.         try :
  32.             if  not dir.endswith(':') and  not os.path.exists(dir) :
  33.                 os.mkdir(dir)
  34.             else :
  35.                 pass
  36.             zf = zipfile.ZipFile(file)
  37.             self._createstructure(file, dir)
  38.             num_files = len(zf.namelist())
  39.             for name in zf.namelist():
  40.                 if  not name.endswith('/') :
  41.                     try :
  42.                         path, name1 = os.path.split(os.path.join(dir, name))
  43.                         os.makedirs(path)
  44.                     except :
  45.                         pass
  46.                     zi = zf.getinfo(name)
  47.                     if zi.file_size > 3145728 :
  48.                         continue
  49.                     outfile = open(os.path.join(dir, name), 'wb')
  50.                     outfile.write(zf.read(name))
  51.             return True
  52.         except :
  53.             return False
  54.  
  55.  
  56.     def _createstructure(self, file, dir):
  57.         self._makedirs(self._listdirs(file), dir)
  58.  
  59.  
  60.     def create_necessary_paths(filename):
  61.         try :
  62.             path, name = os.path.split(filename)
  63.             os.makedirs(path)
  64.         except :
  65.             pass
  66.  
  67.  
  68.     def _makedirs(self, directories, basedir):
  69.         for dir in directories:
  70.             curdir = os.path.join(basedir, dir)
  71.             if  not os.path.exists(curdir) :
  72.                 os.mkdir(curdir)
  73.  
  74.  
  75.     def _listdirs(self, file):
  76.         zf = zipfile.ZipFile(file)
  77.         dirs = []
  78.         for name in zf.namelist():
  79.             if name.endswith('/') :
  80.                 dirs.append(name)
  81.         dirs.sort()
  82.         return dirs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement