How To Simulate Zipfile.open In Python 2.5?
Solution 1:
Haven't tested this bit, but I use something extremely similar in Python 2.4
import zipfile
def extract_from_zip(name, dest_path, zip_file):
dest_file = open(dest_path, 'wb')
dest_file.write(zip_file.read(name))
dest_file.close()
extract_from_zip('path/to/file/in/archive.dat',
'output.txt',
zipfile.ZipFile('test.zip', 'r'))
Solution 2:
I know I am a bit late to the party for this question, but was having the exact same problem.
The solution I used was to copy the python 2.6.6 version of zipfile and put in a folder (I called it python_fix) and import that instead:
python_fix/zipfile.py
Then in code:
import python_fix.zipfileas zipfile
From there I was able to use the 2.6.6 version of zipfile with the python 2.5.1 interpreter (the 2.7.X versions fail on the "with" with this version")
Hope this helps someone else using ancient technology.
Solution 3:
Given my constraints, it looks like the answer was given in my question: parse the ZipFile structure yourself and use zlib.decompressobj
to unzip the bytes once you've found them.
If you don't have (/suffer from) my constraints, you can find better answers here:
- If you can, just upgrade Python 2.5 to 2.6 (or later!), as suggested in a comment by Daenyth.
- If you only have small files in the zip which can be 100% loaded in memory, use ChrisAdams' answer
- If you can introduce a dependency on an external utility, make an appropriate system call to
/usr/bin/unzip
or similar, as suggested in Vlad's answer
Post a Comment for "How To Simulate Zipfile.open In Python 2.5?"