zipfile

Extract Google Drive zip from Google colab notebook

风流意气都作罢 提交于 2019-11-29 19:09:36
问题 I already have a zip of (2K images) dataset on a google drive. I have to use it in a ML training algorithm. Below Code extracts the content in a string format: from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from google.colab import auth from oauth2client.client import GoogleCredentials import io import zipfile # Authenticate and create the PyDrive client. # This only needs to be done once per notebook. auth.authenticate_user() gauth = GoogleAuth() gauth.credentials

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

六眼飞鱼酱① 提交于 2019-11-29 19:08:19
问题 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

Set permissions on a compressed file in python

浪子不回头ぞ 提交于 2019-11-29 14:06:24
I have a file test.txt that is inside a zip archive test.zip . The permissions on test.txt are out of my control when it's compressed, but now I want them to be group-writeable. I am extracting the file with Python, and don't want to escape out to the shell. EDIT: Here's what I've got so far: import zipfile z = zipfile.ZipFile('test.zip', 'w') zi = zipfile.ZipInfo('test.txt') zi.external_attr = 0777 << 16L z.writestr(zi, 'FOO') z.close() z = zipfile.ZipFile('test.zip', 'r') for name in z.namelist(): newFile = open(name, "wb") newFile.write(z.read(name)) newFile.close() z.close() This works

Correctly decoding zip entry file names — CP437, UTF-8 or?

≡放荡痞女 提交于 2019-11-29 13:47:14
I recently wrote a zip file I/O library called zipzap , but I'm struggling with correctly decoding zip entry file names from arbitrary zip files. Now, the PKWARE spec states: D.1 The ZIP format has historically supported only the original IBM PC character encoding set, commonly referred to as IBM Code Page 437... D.2 If general purpose bit 11 is unset, the file name and comment should conform to the original ZIP character encoding. If general purpose bit 11 is set, the filename and comment must support The Unicode Standard, Version 4.1.0 or greater using the character encoding form defined by

Read directly a file within a Zip file - Java

依然范特西╮ 提交于 2019-11-29 10:56:26
My situation is that I have a zip file that contains some files (txt, png, ...) and I want to read it directly by their names, I have tested the following code but no result (NullPointerExcepion): InputStream in = Main.class.getResourceAsStream("/resouces/zipfile/test.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); resources is a package and zipfile is a zip file. If you can be sure that your zip file will never be packed inside another jar, you can use something like: URL zipUrl = Main.class.getResource("/resources/zipfile.zip"); URL entryUrl = new URL("jar:

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

时光怂恿深爱的人放手 提交于 2019-11-29 09:53:39
问题 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?

How to read content of the Zipped file without extracting in java

浪子不回头ぞ 提交于 2019-11-29 07:28:54
I have file with names like ex.zip . In this example, the Zip file contains only one file with the same name(ie. `ex.txt'), which is quite large. I don't want to extract the zip file every time.Hence I need to read the content of the file(ex.txt) without extracting the zip file. I tried some code like below But i can only read the name of the file in the variable. How do I read the content of the file and stores it in the variable? Thank you in Advance fis=new FileInputStream("C:/Documents and Settings/satheesh/Desktop/ex.zip"); ZipInputStream zis = new ZipInputStream(new BufferedInputStream

Safely extract zip or tar using Python

Deadly 提交于 2019-11-29 05:15:05
问题 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?

seek() a file within a zip file in Python without passing it to memory

早过忘川 提交于 2019-11-29 04:13:35
is there anyway to make a file inside a zip file seekable in Python without reading it to memory? I tried the obvious procedure but I get an error since the file is not seekable: In [74]: inputZipFile = zipfile.ZipFile("linear_g_LAN2A_F_3keV_1MeV_30_small.zip", 'r') In [76]: inputCSVFile = inputZipFile.open(inputZipFile.namelist()[0], 'r') In [77]: inputCSVFile Out[77]: <zipfile.ZipExtFile at 0x102f5fad0> In [78]: inputCSVFile.se inputCSVFile.seek inputCSVFile.seekable In [78]: inputCSVFile.seek(0) --------------------------------------------------------------------------- UnsupportedOperation

Find and replace text in .docx file - Python

拜拜、爱过 提交于 2019-11-29 03:47:31
问题 I've been doing a lot of searching for a method to find and replace text in a docx file with little luck. I've tried the docx module and could not get that to work. Eventually I worked out the method described below using the zipfile module and replacing the document.xml file in the docx archive. For this to work you need a template document (docx) with the text you want to replace as unique strings that could not possibly match any other existing or future text in the document (eg. "The