-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzippy.py
32 lines (27 loc) · 918 Bytes
/
zippy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from zipfile import ZipFile, ZIP_STORED
from os.path import isdir
from os import walk, sep
class Zip(ZipFile):
def __init__(self, filename, noisy=False, compression=ZIP_STORED):
self.file = ZipFile(filename, "w", compression)
self.noisy = noisy
def add_files(self, files=[]):
for filename in files:
if self.noisy:
print "Adding file %s" % filename
if isdir(filename):
for (dirname,dirs,filenames) in walk(filename):
self.add_files(map(lambda x: "%s%s%s" % (dirname,sep,x),filenames))
else:
self.file.write(filename)
class Unzip(ZipFile):
def get_contents(self):
return self.namelist()
contents = property(get_contents)
def to_path(self, path):
try:
self.extractall(path)
return True
except (IOError, WindowsError):
print "Could not extract %s to %s" % (self.filename, path)
return False