zipfile

How to download multiple files from S3 as Zip format in Rails 5.x.x app?

百般思念 提交于 2019-12-05 12:30:28
I am implementing feature which allows user to either download single file or multiple files from S3. Single file downloading is working properly, but for multiple files I am receiving error on Heroku, Errno::ENOENT (No such file or directory @ rb_file_s_lstat ) Controller code snippet for downloading files as zip format is as below, def method_name zipfile_name = "#{Rails.root}/public/archive.zip" Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile | @transfer.transfer_attachments.each do |attachment | zipfile.add(attachment.avatar.file.filename, attachment.avatar.url) end end send

Strange “BadZipfile: Bad CRC-32” problem

谁都会走 提交于 2019-12-05 09:48:57
This code is simplification of code in a Django app that receives an uploaded zip file via HTTP multi-part POST and does read-only processing of the data inside: #!/usr/bin/env python import csv, sys, StringIO, traceback, zipfile try: import io except ImportError: sys.stderr.write('Could not import the `io` module.\n') def get_zip_file(filename, method): if method == 'direct': return zipfile.ZipFile(filename) elif method == 'StringIO': data = file(filename).read() return zipfile.ZipFile(StringIO.StringIO(data)) elif method == 'BytesIO': data = file(filename).read() return zipfile.ZipFile(io

Create zip file from all files in folder

一曲冷凌霜 提交于 2019-12-05 07:57:05
I'm trying to create a zip file from all files in a folder, but can't find any related snippet online. I'm trying to do something like this: DirectoryInfo dir = new DirectoryInfo("somedir path"); ZipFile zip = new ZipFile(); zip.AddFiles(dir.getfiles()); zip.SaveTo("some other path"); Any help is very much appreciated. edit: I only want to zip the files from a folder, not it's subfolders. Shannon Holsinger Referencing System.IO.Compression and System.IO.Compression.FileSystem in your Project using System.IO.Compression; string startPath = @"c:\example\start";//folder to add string zipPath = @

load a pickle file from a zipfile

纵然是瞬间 提交于 2019-12-05 06:18:41
For some reason I cannot get cPickle.load to work on the file-type object returned by ZipFile.open(). If I call read() on the file-type object returned by ZipFile.open() I can use cPickle.loads though. Example .... import zipfile import cPickle # the data we want to store some_data = {1: 'one', 2: 'two', 3: 'three'} # # create a zipped pickle file # zf = zipfile.ZipFile('zipped_pickle.zip', 'w', zipfile.ZIP_DEFLATED) zf.writestr('data.pkl', cPickle.dumps(some_data)) zf.close() # # cPickle.loads works # zf = zipfile.ZipFile('zipped_pickle.zip', 'r') sd1 = cPickle.loads(zf.open('data.pkl').read(

How to compress csv file into zip archive directly?

纵然是瞬间 提交于 2019-12-05 02:09:01
I am generating a number of csv files dynamically, using the following code: import csv fieldnames = ['foo1', 'foo2', 'foo3', 'foo4'] with open(csvfilepath, 'wb') as csvfile: csvwrite = csv.DictWriter(csvfile, delimiter=',', fieldnames=fieldnames) csvwrite.writeheader() for row in data: csvwrite.writerow(row) To save space, I want to compress them. Using the gzip module is quite easy: with gzip.open("foo.gz", "w") as csvfile : csvwrite = csv.DictWriter(csvfile, delimiter=',', fieldnames=fieldnames) csvwrite.writeheader() for row in data: csvwrite.writerow(row) But I want the file in 'zip'

How to check if a zip file is encrypted using python's standard library zipfile?

二次信任 提交于 2019-12-05 01:47:23
I am using python's standard library, zipfile, to test an archive: zf = zipfile.ZipFile(archive_name) if zf.testzip()==None: checksum_OK=True And I am getting this Runtime exception: File "./packaging.py", line 36, in test_wgt if zf.testzip()==None: checksum_OK=True File "/usr/lib/python2.7/zipfile.py", line 844, in testzip f = self.open(zinfo.filename, "r") File "/usr/lib/python2.7/zipfile.py", line 915, in open "password required for extraction" % name RuntimeError: File xxxxx/xxxxxxxx.xxx is encrypted, password required for extraction How can I test, before I run testzip(), if the zip is

Adding file to existing zipfile

限于喜欢 提交于 2019-12-05 01:00:18
I'm using python's zipfile module. Having a zip file located in a path of: /home/user/a/b/c/test.zip And having another file created under /home/user/a/b/c/1.txt I want to add this file to existing zip, I did: zip = zipfile.ZipFile('/home/user/a/b/c/test.zip','a') zip.write('/home/user/a/b/c/1.txt') zip.close()` And got all the subfolders appears in path when unzipping the file, how do I just enter the zip file without path's subfolders? I tried also : zip.write(os.path.basename('/home/user/a/b/c/1.txt')) And got an error that file doesn't exist, although it does. You got very close: zip.write

Python - how to convert unicode filename to CP437?

穿精又带淫゛_ 提交于 2019-12-04 23:17:25
I have a file that has a Unicode name, say 'קובץ.txt' . I want to pack him, and I'm using python's zipfile . I can zip the files and open them later on with a problem except that file names are messed up when using windows 7 file explorer to view the files (7zip works great). According to the docs, this is a common problem, and there are instructions on how to deal with that: From ZipFile.write Note There is no official file name encoding for ZIP files. If you have unicode file names, you must convert them to byte strings in your desired encoding before passing them to write(). WinZip

Web2Py - Upload a file and read the content as Zip file

妖精的绣舞 提交于 2019-12-04 21:54:08
I am trying to upload a zip file from Web2Py form and then read the contents: form = FORM(TABLE( TR(TD('Upload File:', INPUT(_type='file', _name='myfile', id='myfile', requires=IS_NOT_EMPTY()))), TR(TD(INPUT(_type='submit',_value='Submit'))) )) if form.accepts(request.vars): data=StringIO.StringIO(request.vars.myfile) import zipfile zfile=zipfile.Zipfile(data) For some reason this code does work and complains of file not being a zip file although the uploaded file is a zip file. I am new to Web2Py . How can the data be represented as zip-file? web2py form field uploads already are cgi

getInputStream for a ZipEntry from ZipInputStream (without using the ZipFile class)

前提是你 提交于 2019-12-04 15:25:55
问题 How can I get an InputStream for a ZipEntry from a ZipInputStream without using the ZipFile class? 回答1: it works this way static InputStream getInputStream(File zip, String entry) throws IOException { ZipInputStream zin = new ZipInputStream(new FileInputStream(zip)); for (ZipEntry e; (e = zin.getNextEntry()) != null;) { if (e.getName().equals(entry)) { return zin; } } throw new EOFException("Cannot find " + entry); } public static void main(String[] args) throws Exception { InputStream in =