-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipme.py
73 lines (63 loc) · 2.43 KB
/
zipme.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Courtsey of manatlan
# http://groups.google.com/group/google-appengine/browse_thread/thread/8f68d6a3e61fee8/aa5f0e44d14116ee?lnk=gst&q=zipme#aa5f0e44d14116ee
from google.appengine.ext import webapp
from google.appengine.api import users
import wsgiref.handlers
import zipfile
import os,re,sys,stat
from cStringIO import StringIO
def createZip(path):
def walktree (top = ".", depthfirst = True):
names = os.listdir(top)
if not depthfirst:
yield top, names
for name in names:
try:
st = os.lstat(os.path.join(top, name))
except os.error:
continue
if stat.S_ISDIR(st.st_mode):
for (newtop, children) in walktree (os.path.join(top,name),
depthfirst):
yield newtop, children
if depthfirst:
yield top, names
list=[]
for (basepath, children) in walktree(path,False):
for child in children:
f=os.path.join(basepath,child)
if os.path.isfile(f):
f = f.encode(sys.getfilesystemencoding())
list.append( f )
f=StringIO()
file = zipfile.ZipFile(f, "w")
for fname in list:
nfname=os.path.join(os.path.basename(path),fname[len(path)+1:])
file.write(fname, nfname , zipfile.ZIP_DEFLATED)
file.close()
f.seek(0)
return f
class ZipMaker(webapp.RequestHandler):
def get(self):
if users.is_current_user_admin():
folder = os.path.dirname(__file__)
self.response.headers['Content-Type'] = 'application/zip'
self.response.headers['Content-Disposition'] = \
'attachment; filename="%s.zip"' %os.path.basename(folder)
fid=createZip(folder)
while True:
buf=fid.read(2048)
if buf=="": break
self.response.out.write(buf)
fid.close()
else:
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write("<a href=\"%s\">You must be admin</a>." %
users.create_login_url("/zipme"))
def main():
application = webapp.WSGIApplication(
[('/zipme', ZipMaker)],
debug=False)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()