zipfile

Using Python to add a list of files into a zip file

喜夏-厌秋 提交于 2019-12-04 01:15:30
I want to write a script to add all the ‘.py’ files into a zip file. Here is what I have: import zipfile import os working_folder = 'C:\\Python27\\' files = os.listdir(working_folder) files_py = [] for f in files: if f[-2:] == 'py': fff = working_folder + f files_py.append(fff) ZipFile = zipfile.ZipFile("zip testing.zip", "w" ) for a in files_py: ZipFile.write(a, zipfile.ZIP_DEFLATED) However it gives an error: Traceback (most recent call last): File "C:\Python27\working.py", line 19, in <module> ZipFile.write(str(a), zipfile.ZIP_DEFLATED) File "C:\Python27\lib\zipfile.py", line 1121, in write

Read ZIP files from S3 without downloading the entire file

被刻印的时光 ゝ 提交于 2019-12-04 01:09:40
问题 We have ZIP files that are 5-10GB in size. The typical ZIP file has 5-10 internal files, each 1-5 GB in size uncompressed. I have a nice set of Python tools for reading these files. Basically, I can open a filename and if there is a ZIP file, the tools search in the ZIP file and then open the compressed file. It's all rather transparent. I want to store these files in Amazon S3 as compressed files. I can fetch ranges of S3 files, so it should be possible to fetch the ZIP central directory (it

Confused about making a CSV file into a ZIP file in django

一世执手 提交于 2019-12-03 21:57:08
I have a view that takes data from my site and then makes it into a zip compressed csv file. Here is my working code sans zip: def backup_to_csv(request): response = HttpResponse(mimetype='text/csv') response['Content-Disposition'] = 'attachment; filename=backup.csv' writer = csv.writer(response, dialect='excel') #code for writing csv file go here... return response and it works great. Now I want that file to be compressed before it gets sent out. This is where I get stuck. def backup_to_csv(request): output = StringIO.StringIO() ## temp output file writer = csv.writer(output, dialect='excel')

How can I save an edited Word document with Python?

二次信任 提交于 2019-12-03 20:54:37
I am attempting to create a script which can extract the XML from a Word document, modify it, and finally save the new Word document, all using Python. Here's the code I used, which was effectively stolen from here : import zipfile import os import tempfile import shutil def getXml(docxFilename): zip = zipfile.ZipFile(open(docxFilename,"rb")) xmlString = str(zip.read("word/document.xml")) return xmlString def createNewDocx(originalDocx,xmlContent,newFilename): tmpDir = tempfile.mkdtemp() zip = zipfile.ZipFile(open(originalDocx,"rb")) zip.extractall(tmpDir) with open(os.path.join(tmpDir,"word

How to zip a very large file in python

浪子不回头ぞ 提交于 2019-12-03 16:14:02
I would like to zip a couple of files that may amount to about 99 GB using python. Please what is the most efficient way to do this using the zipfile library. This is a sample code I have with gcs.open(zip_file_name, 'w', content_type=b'application/zip') as f: with zipfile.ZipFile(f, 'w') as z: for file in files: is_owner = (is_page_allowed_to_visitor(page, visitor) or (file.owner_id == visitor.id) ) if is_owner: file.show = True elif file.available_from: if file.available_from > datetime.now(): file.show = False elif file.available_to: if file.available_to < datetime.now(): file.show = False

How to create compressed Zip archive using ZipOutputStream so that method getSize() of ZipEntry returns correct size?

馋奶兔 提交于 2019-12-03 15:50:44
Consider the code example that put a single file test_file.pdf into zip archive test.zip and then read this archive: import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class Main { public static void main(String[] args) { File infile = new File("test_file.pdf"); try ( FileInputStream fis = new FileInputStream(infile); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("test.zip")); ) { int bytesRead; byte[] buffer = new byte[1024]; ZipEntry entry = new ZipEntry("data"); entry.setSize(infile.length());

Python zip a sub folder and not the entire folder path

谁说我不能喝 提交于 2019-12-03 13:08:09
I have a program to zip all the contents in a folder. I did not write this code but I found it somewhere online and I am using it. I intend to zip a folder for example say, C:/folder1/folder2/folder3/ . I want to zip folder3 and all its contents in a file say folder3.zip. With the below code, once i zip it, the contents of folder3.zip wil be folder1/folder2/folder3/and files. I do not want the entire path to be zipped and i only want the subfolder im interested to zip (folder3 in this case). I tried some os.chdir etc, but no luck. def makeArchive(fileList, archive): """ 'fileList' is a list of

Python - Extracting files from a large (6GB+) zip file

懵懂的女人 提交于 2019-12-03 08:52:13
I have a Python script where I need to extract the contents of a ZIP file. However, the zip file is over 6GB in size. There is a lot of information about zlib and zipfile modules, however, I can't find a single approach that works in my case. I have the code: with zipfile.ZipFile(fname, "r") as z: try: log.info("Extracting %s " %fname) head, tail = os.path.split(fname) z.extractall(folder + "/" + tail) except zipfile.BadZipfile: log.error("Bad Zip file") except zipfile.LargeZipFile: log.error("Zip file requires ZIP64 functionality but that has not been enabled (i.e., too large)") except

Progress Bar with unzipping of file

你。 提交于 2019-12-03 08:51:18
I am trying to update a progress bar with unzipping of file in the sd card. My unzipping works fine but the progress bar does not appear. Here is my code in mainactivity: private ProgressBar bar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bar = (ProgressBar) findViewById(R.id.progress); String zipFilename = Environment.getExternalStorageDirectory() + "path to my zip file in sd card"; String unzipLocation = Environment.getExternalStorageDirectory() + "the output folder"; Decompress d = new Decompress(zipFilename

How to create ZipArchive from files in memory in C#?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 06:21:20
Is it somehow possible to create a ZipArchive from the file(s) in memory (and not actually on the disk). Following is the use case: Multiple files are received in an IEnumerable<HttpPostedFileBase> variable. I want to zip all these files together using ZipArchive . The problem is that ZipArchive only allows CreateEntryFromFile , which expects a path to the file, where as I just have the files in memory. Question: Is there a way to use a 'stream' to create the 'entry' in ZipArchive , so that I can directly put in the file's contents in the zip? I don't want to first save the files, create the