Python: Open file in zip without temporarily extracting it
Vincent Povirk's answer won't work completely;
import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgfile = archive.open('img_01.png')
...
You have to change it in:
import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...
For details read the ZipFile
docs here.
Open file from zip without extracting it in Python?
Try this:
import pandas as pd
import requests
from io import BytesIO, StringIO
from zipfile import ZipFile
response = requests.get(url)
zip_file = ZipFile(BytesIO(response.content))
files = zip_file.namelist()
with zip_file.open(files[0]) as csvfile:
print(pd.read_csv(csvfile, encoding='utf8', sep=","))
Downloading and unzipping a .zip file without writing to disk
My suggestion would be to use a StringIO
object. They emulate files, but reside in memory. So you could do something like this:
# get_zip_data() gets a zip archive containing 'foo.txt', reading 'hey, foo'
import zipfile
from StringIO import StringIO
zipdata = StringIO()
zipdata.write(get_zip_data())
myzipfile = zipfile.ZipFile(zipdata)
foofile = myzipfile.open('foo.txt')
print foofile.read()
# output: "hey, foo"
Or more simply (apologies to Vishal):
myzipfile = zipfile.ZipFile(StringIO(get_zip_data()))
for name in myzipfile.namelist():
[ ... ]
In Python 3 use BytesIO instead of StringIO:
import zipfile
from io import BytesIO
filebytes = BytesIO(get_zip_data())
myzipfile = zipfile.ZipFile(filebytes)
for name in myzipfile.namelist():
[ ... ]
unzip file without creating temporary files
To only extract certain files, you can pass a list to extractall
:
with zipfile.ZipFile('/tmp/DataPump_10000838.zip', 'r') as zip_ref:
lstNEW = list(filter(lambda x: not x.startswith("__MACOSX/"), zip_ref.namelist()))
zip_ref.extractall('/tmp/', members=lstNEW)
The files are not temporary files, but rather macOS's way of representing resource forks in zip files that don't normally support this.
How to unzip without writing to disk?
Using ZipFile.open
on the already opened archive, we can do just that:
import zipfile
import pandas as pd
with zipfile.ZipFile("archive.zip") as archive:
with archive.open("testing.txt") as csv:
df = pd.read_csv(csv)
print(df)
Unzipping files in Python
import zipfile
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
zip_ref.extractall(directory_to_extract_to)
That's pretty much it!
How to extract file from zip without maintaining directory structure in Python?
You can use zipfile.ZipFile.open:
import shutil
import zipfile
with zipfile.ZipFile('/path/to/my_file.apk') as z:
with z.open('/res/drawable/icon.png') as zf, open('temp/icon.png', 'wb') as f:
shutil.copyfileobj(zf, f)
Or use zipfile.ZipFile.read:
import zipfile
with zipfile.ZipFile('/path/to/my_file.apk') as z:
with open('temp/icon.png', 'wb') as f:
f.write(z.read('/res/drawable/icon.png'))
Related Topics
Python Ftp Implicit Tls Connection Issue
How to Call Function That Takes an Argument in a Django Template
Is There Any Built-In Way to Get the Length of an Iterable in Python
Use Groupby in Pandas to Count Things in One Column in Comparison to Another
Operation on Every Pair of Element in a List
Pip Ignores Dependency_Links in Setup.Py
Checking File Permissions in Linux with Python
Does a Slicing Operation Give Me a Deep or Shallow Copy
Pythonic Way to Create Union of All Values Contained in Multiple Lists
Python (And Python C API): _New_ Versus _Init_
Python Requests.Exceptions.Sslerror: Eof Occurred in Violation of Protocol
Hitting Maximum Recursion Depth Using Pickle/Cpickle
Cancel an Already Executing Task with Celery
How to Get Tkinter Canvas to Dynamically Resize to Window Width
Solving Embarassingly Parallel Problems Using Python Multiprocessing
How to Rotate Xticklabels in Matplotlib So That the Spacing Between Each Xticklabel Is Equal