1 | import os |
---|
2 | |
---|
3 | def link2path(filename): |
---|
4 | return os.path.realpath(filename) |
---|
5 | |
---|
6 | class PckgDefaultFile: |
---|
7 | def __init__(self,pckgname,mode,compression=None): |
---|
8 | self.pckgname=pckgname |
---|
9 | self.mode=mode |
---|
10 | self.compression=compression |
---|
11 | self.debug=0 |
---|
12 | |
---|
13 | def close(self): |
---|
14 | try: return self.pckg.close() |
---|
15 | except: return -1 |
---|
16 | |
---|
17 | class PckgZipFile(PckgDefaultFile): |
---|
18 | def __init__(self,pckgname,mode,compression=""): |
---|
19 | PckgDefaultFile.__init__(self,pckgname,mode,compression) |
---|
20 | import zipfile |
---|
21 | self.pckg=zipfile.ZipFile(self.pckgname,self.mode) |
---|
22 | |
---|
23 | def debug(self,state=1): |
---|
24 | self.debug=state |
---|
25 | |
---|
26 | def add(self,filename,arcname=None): |
---|
27 | try: return self.pckg.write(filename,arcname) |
---|
28 | except: return -1 |
---|
29 | |
---|
30 | def extract(self,filename,path=''): |
---|
31 | outfile = open(os.path.join(path,filename),"wb") |
---|
32 | outfile.write(self.pckg.read(filename)) |
---|
33 | outfile.close() |
---|
34 | return 0 |
---|
35 | |
---|
36 | def extractall(self,path=''): |
---|
37 | for name in self.pckg.namelist(): |
---|
38 | if self.debug: print name |
---|
39 | if not name.endswith('/'): |
---|
40 | directory = os.path.normpath(os.path.join(path,os.path.dirname(name))) |
---|
41 | if not os.path.exists(directory): os.makedirs(directory) |
---|
42 | self.extract(name,path) |
---|
43 | elif not os.path.exists(name): |
---|
44 | os.makedirs(os.path.join(path,name)) |
---|
45 | return 0 |
---|
46 | |
---|
47 | def read(self,filename): |
---|
48 | try: return self.pckg.read(filename) |
---|
49 | except: return -1 |
---|
50 | |
---|
51 | def check(self): |
---|
52 | try: self.pckg.namelist() |
---|
53 | except: return -1 |
---|
54 | return 0 |
---|
55 | def list(self): |
---|
56 | return self.pckg.namelist() |
---|
57 | |
---|
58 | def getUncompressedSize(self): |
---|
59 | totalSize=0 |
---|
60 | try: |
---|
61 | for zipinfo in self.pckg.infolist(): |
---|
62 | totalSize+=zipinfo.file_size |
---|
63 | except: |
---|
64 | return-1 |
---|
65 | return totalSize |
---|
66 | |
---|
67 | class PckgTarFile(PckgDefaultFile): |
---|
68 | def __init__(self,pckgname,mode,compression=""): |
---|
69 | PckgDefaultFile.__init__(self,pckgname,mode,compression) |
---|
70 | import tarfile |
---|
71 | try: |
---|
72 | self.pckg=tarfile.open(self.pckgname,self.mode+":"+self.compression) |
---|
73 | except Exception,e: |
---|
74 | print (self.pckgname,self.mode+":"+self.compression) |
---|
75 | raise e |
---|
76 | |
---|
77 | |
---|
78 | def debug(self,state=1): |
---|
79 | self.debug=state |
---|
80 | self.pckg.debug=state |
---|
81 | |
---|
82 | def add(self,filename,arcname=None): |
---|
83 | if arcname==None: arcname=filename |
---|
84 | filename=link2path(filename) |
---|
85 | |
---|
86 | try: self.pckg.add(filename,arcname) |
---|
87 | except: return -1 |
---|
88 | |
---|
89 | def extract(self,filename,path=''): |
---|
90 | return self.pckg.extract(filename,path) |
---|
91 | |
---|
92 | def extractall(self,path=None): |
---|
93 | return self.pckg.extractall('') |
---|
94 | |
---|
95 | def read(self,filename): |
---|
96 | return self.pckg.extractfile(filename).read() |
---|
97 | |
---|
98 | def check(self): |
---|
99 | try: self.pckg.getnames() |
---|
100 | except: return -1 |
---|
101 | return 0 |
---|
102 | def list(self): |
---|
103 | return self.pckg.getnames() |
---|
104 | def getUncompressedSize(self): |
---|
105 | totalSize=0 |
---|
106 | try: |
---|
107 | for tarinfo in self.pckg: |
---|
108 | totalSize+=tarinfo.size |
---|
109 | except: |
---|
110 | return-1 |
---|
111 | return totalSize |
---|
112 | |
---|
113 | def validExt(filename,extlist): |
---|
114 | for ext in extlist: |
---|
115 | if filename.endswith(ext): |
---|
116 | return True |
---|
117 | return False |
---|
118 | |
---|
119 | class PckgFile: |
---|
120 | def __init__(self,pckgname,mode): |
---|
121 | compression='' |
---|
122 | if validExt(pckgname,['zip']): |
---|
123 | classobj=PckgZipFile |
---|
124 | elif validExt(pckgname,['tar']): |
---|
125 | classobj=PckgTarFile |
---|
126 | elif validExt(pckgname,['tar.gz','tgz']): |
---|
127 | classobj=PckgTarFile |
---|
128 | compression="gz" |
---|
129 | elif validExt(pckgname,['tar.bz2']): |
---|
130 | classobj=PckgTarFile |
---|
131 | compression="bz2" |
---|
132 | |
---|
133 | self.write=self.add |
---|
134 | self.pckg=classobj(pckgname,mode,compression) |
---|
135 | |
---|
136 | def add(self,filename,arcname=None): |
---|
137 | return self.pckg.add(filename,arcname) |
---|
138 | |
---|
139 | def extract(self,filename,path=''): |
---|
140 | return self.pckg.extract(filename,path) |
---|
141 | |
---|
142 | def extractall(self,path=''): |
---|
143 | return self.pckg.extractall(path) |
---|
144 | |
---|
145 | def read(self,filename): |
---|
146 | return self.pckg.read(filename) |
---|
147 | |
---|
148 | def close(self): |
---|
149 | return self.pckg.close() |
---|
150 | |
---|
151 | def check(self): |
---|
152 | return self.pckg.check() |
---|
153 | def list(self): |
---|
154 | return self.pckg.list() |
---|
155 | |
---|
156 | def getUncompressedSize(self): |
---|
157 | return self.pckg.getUncompressedSize() |
---|
158 | |
---|
159 | |
---|
160 | #if __name__=='__main__': |
---|
161 | # pckg=PckgFile("coucou2.tgz","w") |
---|
162 | # for x in ['aa','aa_z.tgz']: |
---|
163 | # pckg.add(x) |
---|
164 | # pckg.close() |
---|
165 | # |
---|
166 | # |
---|
167 | # #pckg=PckgFile("coucou.zip","r") |
---|
168 | # ##pckg.extract(x+".bak") |
---|
169 | # #pckg.extractall("extract") |
---|
170 | # #pckg.close() |
---|