Ejemplo n.º 1
0
def response_to_zip(response, filename=None):
    """Create zip-like file object from `requests` response and set its real
    filename.
    """
    if not filename:
        filename = re.search(r'filename=(.+)',
                             response.headers['Content-Disposition']).group(1)
    _zip = ZipFile(BytesIO(response.content))
    _zip.filename = filename
    return _zip
Ejemplo n.º 2
0
def ntfs_zip():
    pwd = os.path.dirname(os.path.dirname(__file__))

    ntfs_path = os.path.join(pwd, 'fixtures/ntfs/*.txt')

    ntfs_zip = ZipFile(BytesIO(), 'a', ZIP_DEFLATED, False)
    ntfs_zip.filename = 'ntfs.zip'
    for filename in glob(ntfs_path):
        with open(filename) as file:
            ntfs_zip.writestr(os.path.basename(filename), file.read())
    return ntfs_zip
Ejemplo n.º 3
0
def ntfs_zip():
    pwd = os.path.dirname(os.path.dirname(__file__))

    ntfs_path = os.path.join(pwd, 'fixtures/ntfs/*.txt')

    ntfs_zip = ZipFile(BytesIO(), 'a', ZIP_DEFLATED, False)
    ntfs_zip.filename = 'ntfs.zip'
    for filename in glob(ntfs_path):
        with open(filename) as file:
            ntfs_zip.writestr(os.path.basename(filename), file.read())
    return ntfs_zip
Ejemplo n.º 4
0
def genManyPdf(pdf_files):
    s = StringIO()
    zf = ZipFile(s, mode='w')
    zf.filename = "teampdf.zip"

    for pdf in pdf_files:
        zf.write(path.join(DIR_DEST, pdf), arcname="teamPDF/" + pdf)
    zf.close()

    # Grab ZIP file from in-memory, make response with correct MIME-type
    response = HttpResponse(s.getvalue(),
                            mimetype="application/x-zip-compressed")
    # ..and correct content-disposition
    response['Content-Disposition'] = 'attachment; filename=%s' % zf.filename

    return response