Пример #1
0
    def generateChunks(path):
        stream = ZipFile(mode="w", compression=ZIP_DEFLATED)

        for item in path.glob('*'):
            if not item.exists() or item.is_dir():
                continue
            try:
                stream.write_iter((item.name.split('/')[-1]).lstrip("/"),
                                  generateFileChunks(item))
            except:
                continue

            for chunk in stream.next_file():
                yield chunk

        for chunk in stream:
            yield chunk
Пример #2
0
def zipball(request, token):
    zip_content = redis_client.get("zip:%s" % (token))
    if not(zip_content):
        return HttpResponseBadRequest()
    zip_content = json.loads(zip_content.decode('utf-8'))
    zip_filename = zip_content['zip_filename']
    z = ZipFile(mode='w', compression=ZIP_DEFLATED)
    for file_def in zip_content['files']:
        filepath = file_def.get("path", None)
        file_content = file_def.get("content", None)
        filename = file_def.get("filename", None)
        backend = file_def.get("backend", None)
        if filepath:
            storage = get_storage_class(backend)()
            f = storage.open(filepath, 'r')
            z.write_iter(filename, read_in_chunks(f))
        elif file_content:
            z.writestr(filename, file_content.encode("utf-8"))
    response = StreamingHttpResponse(z, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename={}'.format(zip_filename)
    return response