zipfile

Sending multiple .CSV files to .ZIP without storing to disk in Python

安稳与你 提交于 2019-11-30 13:37:37
I'm working on a reporting application for my Django powered website. I want to run several reports and have each report generate a .csv file in memory that can be downloaded in batch as a .zip. I would like to do this without storing any files to disk. So far, to generate a single .csv file, I am following the common operation: mem_file = StringIO.StringIO() writer = csv.writer(mem_file) writer.writerow(["My content", my_value]) mem_file.seek(0) response = HttpResponse(mem_file, content_type='text/csv') response['Content-Disposition'] = 'attachment; filename=my_file.csv' This works fine, but

Python: Getting files into an archive without the directory?

非 Y 不嫁゛ 提交于 2019-11-30 12:36:21
I've been learning python for about 3 weeks now, and I'm currently trying to write a little script for sorting files (about 10.000) by keywords and date appearing in the filename. Files before a given date should be added to an archive. The sorting works fine, but not the archiving It creates an archive - the name is fine - but in the archive is the complete path to the files. If i open it, it looks like: folder1 -> folder2 -> folder3 -> files . How can I change it such that the archive only contains the files and not the whole structure? Below is a snippet with my zip function, node is the

unzipping file results in “BadZipFile: File is not a zip file”

删除回忆录丶 提交于 2019-11-30 10:46:31
I have two zip files, both of them open well with Windows Explorer and 7-zip. However when i open them with Python's zipfile module [ zipfile.ZipFile("filex.zip") ], one of them gets opened but the other one gives error " BadZipfile: File is not a zip file ". I've made sure that the latter one is a valid Zip File by opening it with 7-Zip and looking at its properties (says 7Zip.ZIP). When I open the file with a text editor, the first two characters are "PK", showing that it is indeed a zip file. I'm using Python 2.5 and really don't have any clue how to go about for this. I've tried it both

How do I create a zip file of a file path using Python, including empty directories?

烈酒焚心 提交于 2019-11-30 07:32:14
I've been trying to use the zipfile and shutil.make_archive modules to recursively create a zip file of a directory. Both modules work great--except empty directories do not get added to the archive. Empty directories containing other empty directories are also silently skipped. I can use 7Zip to create an archive of the same path and empty directories are preserved. Therefore I know this is possible within the file format itself. I just don't know how to do it within Python. Any ideas? Thanks! There is a example using zipfile: import os, zipfile from os.path import join def zipfolder

How to zip a folder and download it using php?

一个人想着一个人 提交于 2019-11-30 07:02:46
问题 I have folder named "data". This "data" folder contains a file "filecontent.txt" and another folder named "Files". The "Files" folder contains a "info.txt" file. So it is a folder inside folder structure. I have to zip this folder "data"(using php) along with the file and folder inside it, and download the zipped file. I have tried the examples available at http://www.php.net/manual/en/zip.examples.php These examples did not work. My PHP version is 5.2.10 Please help. I have written this code

Safely extract zip or tar using Python

混江龙づ霸主 提交于 2019-11-30 05:58:32
I'm trying to extract user-submitted zip and tar files to a directory. The documentation for zipfile's extractall method (similarly with tarfile's extractall ) states that it's possible for paths to be absolute or contain .. paths that go outside the destination path. Instead, I could use extract myself, like this: some_path = '/destination/path' some_zip = '/some/file.zip' zipf = zipfile.ZipFile(some_zip, mode='r') for subfile in zipf.namelist(): zipf.extract(subfile, some_path) Is this safe? Is it possible for a file in the archive to wind up outside of some_path in this case? If so, what

Php create zip file (from post attachments wordpress)

回眸只為那壹抹淺笑 提交于 2019-11-30 05:30:04
问题 I am trying to create a zip file from post attachments in wordpress . I have tried both methods below - but it results in nothing (No error messages , no file created) - What am I doing wrong (again .. ) I do not think that The fact that those are post attachments in wordpress has anything to do with it - because those methods failed also with normal files . why ? --> See the answer . $files_to_zip = array();// create files array //run a query $post_id = get_the_id(); $args = array( 'post

Convert a VERY LARGE binary file into a Base64String incrementally

二次信任 提交于 2019-11-30 03:48:27
问题 I need help converting a VERY LARGE binary file (ZIP file) to a Base64String and back again. The files are too large to be loaded into memory all at once (they throw OutOfMemoryExceptions) otherwise this would be a simple task. I do not want to process the contents of the ZIP file individually, I want to process the entire ZIP file. The problem: I can convert the entire ZIP file (test sizes vary from 1 MB to 800 MB at present) to Base64String, but when I convert it back, it is corrupted. The

zipfile cant handle some type of zip data?

回眸只為那壹抹淺笑 提交于 2019-11-30 03:35:48
问题 I came up over this problem while trying to decompress a zip file. -- zipfile.is_zipfile(my_file) always returns False, even though the UNIX command unzip handles it just fine. Also, when trying to do zipfile.ZipFile(path/file_handle_to_path) I get the same error -- the file command returns Zip archive data, at least v2.0 to extract and using less on the file it shows: PKZIP for iSeries by PKWARE Length Method Size Cmpr Date Time CRC-32 Name 2113482674 Defl:S 204502989 90% 2010-11-01 08:39

Zipping dynamic files in App Engine (Python)

ⅰ亾dé卋堺 提交于 2019-11-29 20:05:33
问题 Is there anyway I can zip dynamically generated content, such as a freshly rendered html template, into a zip file using zipfile? There seem to be some examples around for zipping static content, but none for zipping dynamic ones. Or, is it not possible at all? One more question: Is it possible to create a zip file with a bunch of sub-folders inside it? Thanks. 回答1: You can add whatever you want to a zip file using ZipFile.writestr(): my_data = "<html><body><p>Hello, world!</p></body></html>"