Zip Filesystem¶
A filesystem implementation for .zip files.
-
class
fs.zipfs.
ReadZipFS
(file, encoding=u'utf-8')¶ A readable zip file.
-
class
fs.zipfs.
WriteZipFS
(file, compression=8, encoding=u'utf-8', temp_fs=u'temp://__ziptemp__')¶ A writable zip file.
-
write_zip
(file=None, compression=None, encoding=None)¶ Write zip to a file.
Note
This is called automatically when the ZipFS is closed.
Parameters: - file (str or file-like) – Destination file, may be a file name or an open file object.
- compression – Compression to use (one of the constants defined in the zipfile module in the stdlib).
-
-
class
fs.zipfs.
ZipFS
(wrap_fs)¶ Read and write zip files.
There are two ways to open a ZipFS for the use cases of reading a zip file, and creating a new one.
If you open the ZipFS with
write
set toFalse
(the default), then the filesystem will be a read only filesystem which maps to the files and directories within the zip file. Files are decompressed on the fly when you open them.Here’s how you might extract and print a readme from a zip file:
with ZipFS('foo.zip') as zip_fs: readme = zip_fs.gettext('readme.txt')
If you open the ZipFS with
write
set toTrue
, then the ZipFS will be a empty temporary filesystem. Any files / directories you create in the ZipFS will be written in to a zip file when the ZipFS is closed.Here’s how you might write a new zip file containing a readme.txt file:
with ZipFS('foo.zip', write=True) as new_zip: new_zip.settext( 'readme.txt', 'This zip file was written by PyFilesystem' )
Parameters: - file (str or file) – An OS filename, or a open file object.
- write (bool) – Set to
True
to write a new zip file, orFalse
to read an existing zip file. - compression (int) – Compression to use (one of the constants defined in the zipfile module in the stdlib).
- temp_fs (str) – An opener string for the temporary filesystem used to store data prior to zipping.