rivenz / pyfilesystem

Automatically exported from code.google.com/p/pyfilesystem
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Empty ZIP files generated - why? #183

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The following code is supposed to generate a ZIP file with one file foo.txt 
inside. However the resulting out.zip file is empty. Why?
-----------

[ajung@dev1 onkopedia_buildout]$ cat x.py 
import fs.zipfs

z = fs.zipfs.ZipFS('out.zip', 'w')
z.open('foo.txt', 'wb').write('nmj')
z.close()

[ajung@dev1 onkopedia_buildout]$ bin/zopepy x.py 
[ajung@dev1 onkopedia_buildout]$ !un
unzip out.zip 
Archive:  out.zip
warning [out.zip]:  zipfile is empty

Original issue reported on code.google.com by zopyxfilter on 31 Jul 2014 at 6:17

GoogleCodeExporter commented 8 years ago
This is because you aren't closing the file you opened. Files are added to the 
zip when they are closed, in your code the file is closed *after* the zipfs is 
closed (when the file goes out of scope.

Try the following:

    import fs.zipfs

    z = fs.zipfs.ZipFS('out.zip', 'w')
    with z.open('foo.txt', 'wb' as f:
        f.write('nmj')
    z.close()

or you could use:

    z.setcontents('goo.txt', 'nmj')

Original comment by willmcgugan on 31 Jul 2014 at 8:05