Posts

Showing posts with the label zip

Python flask on downloading zip file gets corrupted

Python flask on downloading zip file gets corrupted I have written following python flask code to download zip file. @ns.route('/zip_file_download', strict_slashes=False) class ZipDownload(Resource): def get(self): output_path = os.path.abspath(os.path.join(os.getcwd(), './out')) out_file_name = os.path.join(output_path, 'sample.zip') r = send_file(out_file_name, mimetype='application/octet-stream', attachment_filename='sample.zip', as_attachment=True) return r This downloads the zip file but on extracting the zip file it gives an error - Error - 21 - is not a directory - Unable to expand probably because it gets corrupted. I also tried with send_from_directory but the same error crept there too. I am developing this on Mac machine but the downloaded files are also corrupted when opened on windows or an other OS. Any suggestions? PS: Moreover, this issue is only happening when running the API from a Mac machine. Things ...

Python code to recursively create a zip file within a zip file for 'n' iterations

Python code to recursively create a zip file within a zip file for 'n' iterations I'm trying (and failing) to create a Python function that essentially zips up some text, then zips the archive up again and again (the number based on a parameter). So we'd end up with file1.zip containing file2.zip which in turn contains file3.zip. i.e (file1.zip(file2.zip(file3.zip))). Hope that's articulated well enough. import io import zipfile import gzip hexdata = "5a69702054657374" def zip_it(filename, depth): zip_buffer = io.BytesIO() for i in range(0, depth): with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file: zip_file.writestr(filename + str(i), bytes.fromhex(hexdata).decode("utf-8")) with open(filename + ".zip", "wb") as f: f.write(zip_buffer.getvalue()) if __name__ == '__main__': zip_it("testfile", 3) The above code is what I have so fa...