+@cache.cache_control(must_revalidate=True, max_age=1800)
+def download_shelf(request, slug):
+ """"
+ Create a ZIP archive on disk and transmit it in chunks of 8KB,
+ without loading the whole file into memory. A similar approach can
+ be used for large dynamic PDF files.
+ """
+ shelf = get_object_or_404(models.Tag, slug=slug, category='set')
+
+ # Create a ZIP archive
+ temp = tempfile.TemporaryFile()
+ archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
+ for book in models.Book.tagged.with_all(shelf):
+ filename = book.html_file.path
+ archive.write(filename, str('%s.html' % book.slug))
+ archive.close()
+
+ wrapper = FileWrapper(temp)
+ response = HttpResponse(wrapper, content_type='application/zip')
+ response['Content-Disposition'] = 'attachment; filename=%s.zip' % shelf.slug
+ response['Content-Length'] = temp.tell()
+ temp.seek(0)
+ return response
+
+