~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to python/plugins/plugin_installer/unzip.py

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" unzip.py
 
2
Version: 1.1
 
3
By Doug Tolton (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252508)
 
4
"""
 
5
 
 
6
import zipfile
 
7
import os
 
8
 
 
9
class unzip:
 
10
    """ unzip.py
 
11
    Version: 1.1
 
12
 
 
13
    Extract a zipfile to the directory provided
 
14
    It first creates the directory structure to house the files
 
15
    then it extracts the files to it.
 
16
 
 
17
    import unzip
 
18
    un = unzip.unzip()
 
19
    un.extract(r'c:\testfile.zip', 'c:\testoutput')
 
20
 
 
21
    By Doug Tolton (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252508)
 
22
    """
 
23
    def __init__(self, verbose = True, percent = 10):
 
24
        self.verbose = verbose
 
25
        self.percent = percent
 
26
 
 
27
    def extract(self, file, dir):
 
28
        if not dir.endswith(':') and not os.path.exists(dir):
 
29
            os.makedirs(dir)
 
30
 
 
31
        zf = zipfile.ZipFile(file)
 
32
 
 
33
        # create directory structure to house files
 
34
        #print "Creating plugin structure:"
 
35
        self._createstructure(file, dir)
 
36
 
 
37
        num_files = len(zf.namelist())
 
38
        percent = self.percent
 
39
        divisions = 100 / percent
 
40
        perc = int(num_files / divisions)
 
41
 
 
42
        # extract files to directory structure
 
43
        for i, name in enumerate(zf.namelist()):
 
44
 
 
45
            if self.verbose == True:
 
46
                pass
 
47
                #print "Extracting %s" % name
 
48
            elif perc > 0 and (i % perc) == 0 and i > 0:
 
49
                complete = int (i / perc) * percent
 
50
                #print "%s%% complete" % complete
 
51
 
 
52
            if not name.endswith('/'):
 
53
                outfile = open(os.path.join(dir, name), 'wb')
 
54
                outfile.write(zf.read(name))
 
55
                outfile.flush()
 
56
                outfile.close()
 
57
 
 
58
    def _createstructure(self, file, dir):
 
59
        self._makedirs(self._listdirs(file), dir)
 
60
 
 
61
    def _makedirs(self, directories, basedir):
 
62
        """ Create any directories that don't currently exist """
 
63
        #print "Processing directories contained in the zip file: %s" % directories
 
64
        for dir in directories:
 
65
            curdir = os.path.join(basedir, dir)
 
66
            # normalize the path
 
67
            curdir = os.path.normpath(curdir)
 
68
            #print "Checking to see if we should create %s" % curdir
 
69
            if not os.path.exists(curdir):
 
70
                # use makedirs to create parent directories as well
 
71
                #print "Creating %s" % curdir
 
72
                os.makedirs(curdir)
 
73
 
 
74
    def _listdirs(self, file):
 
75
        """ Grabs all the directories in the zip structure
 
76
        This is necessary to create the structure before trying
 
77
        to extract the file to it. """
 
78
        zf = zipfile.ZipFile(file)
 
79
 
 
80
        dirs = []
 
81
 
 
82
        for name in zf.namelist():
 
83
          (path, filename) = os.path.split(name)
 
84
 
 
85
          if path not in dirs:
 
86
            dirs.append(path)
 
87
 
 
88
        dirs.sort()
 
89
        return dirs