def _downloadShare(self, path): if not os.path.exists(path): return WebFunc.jsonResult({'errCode': 464}) if os.path.isdir(path): folderPath = ProfileFunc.slashFormat(path) filename = os.path.basename(folderPath) filename = filename + '.zip' request = cherrypy.serving.request filename = UtilFunc.StringUrlEncode(filename) response = cherrypy.response response.headers['Content-Type'] = 'application/zip' response.headers['Last-Modified'] = time.time() User_Agent = request.headers.get('User-Agent') if 'Firefox' in User_Agent: response.headers[ 'Content-Disposition'] = 'attachment;filename*="%s"' % filename else: response.headers[ 'Content-Disposition'] = 'attachment;filename="%s"' % filename zipobj = ZipStream(folderPath) return zipobj.__iter__() else: filename = os.path.basename(path) filename = UtilFunc.StringUrlEncode(filename) return PostStatic.serve_download(path, filename)
def stream_as_zip(request): streamed_data_filename = "my_streamed_zip_file.zip" files = [] files.append({'file': '/tmp/some_image.jpg'}) files.append({'file': '/tmp/foo'}) # this file will have different name than original files.append({'file': '/tmp/my_mp3_file.mp3', 'name': 'my.mp3'}) # large chunk size will improve speed, but increase memory usage stream = ZipStream(files, chunksize=32768) # streamed response response = StreamingHttpResponse( stream.stream(), content_type="application/zip") response['Content-Disposition'] = 'attachment; filename="%s"' % streamed_data_filename return response
def download_media(): id = request.values["id"] try: uid = get_entity_id(Track, id) except GenericError: uid = None try: fid = get_entity_id(Folder, id) except GenericError: fid = None if uid is None and fid is None: raise GenericError("Invalid ID") if uid is not None: try: rv = Track[uid] return send_file(rv.path, mimetype=rv.mimetype, conditional=True) except ObjectNotFound: try: # Album -> stream zipped tracks rv = Album[uid] except ObjectNotFound: raise NotFound("Track or Album") else: try: # Folder -> stream zipped tracks, non recursive rv = Folder[fid] except ObjectNotFound: raise NotFound("Folder") # Stream a zip of multiple files to the client z = ZipStream(sized=True) if isinstance(rv, Folder): # Add the entire folder tree to the zip z.add_path(rv.path, recurse=True) else: # Add tracks + cover art to the zip for track in rv.tracks: z.add_path(track.path) cover_path = _cover_from_collection(rv, extract=False) if cover_path: z.add_path(cover_path) if not z: raise GenericError("Nothing to download") resp = Response(z, mimetype="application/zip") resp.headers["Content-Disposition"] = "attachment; filename={}.zip".format( rv.name) resp.headers["Content-Length"] = len(z) return resp
def GET(self, name, _path): try: _path, path, uploadable = handlePath(name, _path) except RuntimeError as err: return web.notfound() if not os.path.isdir(path): return web.notfound() web.header('Content-type', 'application/zip') web.header('Content-disposition', 'attachment; filename="%s.zip"' % name) return ZipStream(path).__iter__()
def DownloadFolderZip(folderPath): if not os.path.isdir(folderPath): raise cherrypy.HTTPError(460, 'Bad Parameter') folderPath = ProfileFunc.slashFormat(folderPath) filename = os.path.basename(folderPath) filename = filename + '.zip' request = cherrypy.serving.request filename = UtilFunc.StringUrlEncode(filename) response = cherrypy.response response.headers['Content-Type'] = 'application/zip' response.headers['Last-Modified'] = time.time() User_Agent = request.headers.get('User-Agent') if 'Firefox' in User_Agent: response.headers[ 'Content-Disposition'] = 'attachment;filename*="%s"' % filename else: response.headers[ 'Content-Disposition'] = 'attachment;filename="%s"' % filename zipobj = ZipStream(folderPath) return zipobj.__iter__()
#!/usr/bin/env python3 from zipstream import ZipStream files = [ { 'stream': [ b'qwoeui youasd\n', b'yfoasudyf oiausdy foiuasdy\n', b'foiuasdy foiuasdy foa\n' ], 'name': 'a.txt', 'compression': 'deflate' }, { 'file': '/tmp/car.jpeg' }, { 'file': '/tmp/aaa.mp3', 'name': 'music.mp3' }, ] zs = ZipStream(files) with open("example.zip", "wb") as fout: for data in zs.stream(): fout.write(data)
#!/usr/bin/env python3 import os import random from zipstream import ZipStream def bin_generator(lines): blah = ['foo', 'baz', 'bar', 'xyz', 'aaa', 'bbb'] for a in range(lines): ln = [] for b in range(random.randint(5, 20)): ln.append(random.choice(blah)) yield bytes(' '.join(ln), 'ascii') yield b'\n' def files_to_stream(dirname): for f in os.listdir(dirname): fp = os.path.join(dirname, f) if os.path.isfile(fp): name = "foo_" + os.path.basename(fp) yield {'file': fp, 'name': name} yield {'stream': bin_generator(10), 'name': 'foo.txt'} zs = ZipStream(files_to_stream("/tmp/files/to/stream")) with open("example.zip", "wb") as fout: for data in zs.stream(): fout.write(data)