Zip a set of blobs from appengine datastore
I need to zip a set of blobs available in data store. These can of different types like some html/images/swf/ etc. where all these are available in datastore as blob.
I tried to implement this solution: Zipping dynamic files in App Engine (Python)?
Tried with some static texts it worked great, I am also able to create a zip with a set of files with respective content but I could not trace out some issue when making zip from query.
z.writestr(fil.Template_name, my_data.encode('UTF-8'))
File "C:\Python25\lib\zipfile.py", line 626, in writestr
self.fp.write(zinfo.FileHeader())
File "C:\Python25\lib\zipfile.py", line 260, in FileHeader
return header + self开发者_如何学C.filename + extra
UnicodeDecodeError: 'ascii' codec can't decode byte 0xde in position 12: ordinal not in range(128)
This is the error for this part of code
class filesDB(db.model)
Template_file = db.BlobProperty()
Template_name= db.StringProperty()
output = StringIO.StringIO()
z = zipfile.ZipFile(output,'w')
files = filesDB.all().filter("fCreatedBy","sandeep")
for fil in files:
my_data = fil.Template_file
z.writestr(fil.Template_name, my_data)
z.close()
Per the zipfile documentation:
There is no official file name encoding for ZIP files.
If you have unicode file names, you must convert them to byte strings in your desired encoding before passing them to write().
Try to encode your filename in UTF-8 for example with:
class filesDB(db.model)
Template_file = db.BlobProperty()
Template_name= db.StringProperty()
output = StringIO.StringIO()
z = zipfile.ZipFile(output,'w')
files = filesDB.all().filter("fCreatedBy","sandeep")
for fil in files:
my_data = fil.Template_file
z.writestr(fil.Template_name.encode('utf-8'), my_data)
z.close()
精彩评论