Skip to content

Instantly share code, notes, and snippets.

@mdornseif
Created May 1, 2011 20:44
Show Gist options
  • Save mdornseif/950860 to your computer and use it in GitHub Desktop.
Save mdornseif/950860 to your computer and use it in GitHub Desktop.
This is shows how to generate ZIP files in the blobstore.
def store_pdfs_in_zip():
docs = Dokument.all().order('updated_at')
file_name = files.blobstore.create(mime_type='application/zip',
_blobinfo_uploaded_filename='test.zip')
with files.open(file_name, 'w') as f:
z = blobstoreZipFile(f)
for doc in docs.fetch(75):
pdf = doc.data
fname = "%s-%s.pdf" % (doc.designator, doc.updated_at)
fname = fname.encode('ascii', 'replace')
z.writestr(fname, pdf, date_time=doc.updated_at.timetuple()[:6])
# Finalize ZIP file and write directory
z.flush()
# Finalize the file in the blobstore
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
@Abe404
Copy link

Abe404 commented Jul 27, 2011

Hello,
Thanks for contributing this.
When I try to use it I get an Error. (both with local sdk and on app engine)

with files.open(file_name, 'w') as f:
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/files/file.py", line 523, in open
exclusive_lock=exclusive_lock)
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/files/file.py", line 326, in init
self._open()
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/files/file.py", line 479, in _open
raise UnsupportedOpenModeError('Unsupported open mode: %s', self._mode)
UnsupportedOpenModeError: ('Unsupported open mode: %s', 'w')

@mdornseif
Copy link
Author

Do you have enabled Billing?

@carlopires
Copy link

Thanks for sharing this. The files generated are seen like corrupted. Did you get this:

carlo@carlo-laptop:/tmp$ unzip -l /tmp/pages.zip 
Archive:  /tmp/pages.zip
warning [/tmp/pages.zip]:  60 extra bytes at beginning or within zipfile
  (attempting to process anyway)
error [/tmp/pages.zip]:  reported length of central directory is
  -60 bytes too long (Atari STZip zipfile?  J.H.Holm ZIPSPLIT 1.1
  zipfile?).  Compensating...
  Length      Date    Time    Name
---------  ---------- -----   ----
     2537  2011-09-27 16:13   000001.pickled
---------                     -------
     2537                     1 file

Despite the errors, I can extract zipped files:

carlo@carlo-laptop:/tmp$ unzip -x pages.zip 
Archive:  pages.zip
warning [pages.zip]:  60 extra bytes at beginning or within zipfile
  (attempting to process anyway)
error [pages.zip]:  reported length of central directory is
  -60 bytes too long (Atari STZip zipfile?  J.H.Holm ZIPSPLIT 1.1
  zipfile?).  Compensating...
  inflating: 000001.pickled

@mdornseif
Copy link
Author

@carlopires I tested it and then moved to tar.bz2. Did you check the actual contents of the file?

hexdump -C /tmp/pages.zip | head -n 20

@carlopires
Copy link

Do you mean the file generated is bzip2 compressed?

@mdornseif
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment