2006-11-02

 

ZipFile interface slightly shocked me

Yesterday I needed to read some files from ZIP archives in my program on Python. So I read the zipfile.ZipFile class documentation and found that the only way to read the archived file content is read() method which returns bytes of the file. I think "Hmm, how is it? I really need to try some examples":

$ ls -l test.txt
-rw-r--r-- 1 dima dima 63212690 Nov 1 17:24 test.txt
$ zip -9 test.zip test.txt
adding: test.txt (deflated 93%)
$ python
Python 2.5
>>> from zipfile import ZipFile
>>> z = ZipFile("test.zip", "r")
>>> z.namelist()
['test.txt']
>>> b = z.read("test.txt")
>>> type(b)
<type 'str'>
>>> len(b)
63212690

So the whole file was read into the b variable! But what if I only need to read some bytes from the file header or read it line by line for example? I was slightly shocked. Why ZipFile don't have a method which returns a file-like object?

This page is powered by Blogger. Isn't yours?