Creating ZIP files
GIS often requires the use of large files that will be compressed into a .zip
format for ease of sharing. Python includes a module that you can use to decompress and compress files in this format.
Getting ready
ZIP is a common compression and archive format and is implemented in Python through the zipfile
module. The ZipFile
class can be used to create, read, and write .zip
files. To create a new .zip
file, simply provide the filename along with a mode as w
, which indicates that you want to write data to the file. In the following code example, we are creating a .zip
file called datafile.zip
. The second parameter, w
, indicates that a new file will be created. A new file will be created or an existing file with the same name will be truncated in the write mode. An optional compression parameter can also be used when creating the file. This value can be set to either ZIP_STORED
or ZIP_DEFLATED
:
zipfile.ZipFile('dataFile.zip', 'w',zipfile.ZIP_STORED)
In this...