Python standard library ZipFile
What is a zip file? Why zip-packaged files on the network are more common. Rather than RAR?
Zip is a compressed archive of files, zip open source.
Python's ZipFile module, with two major classes
ZipFile ()
function to determine if it is a zip type file
ZipFile. Is_zipfile (filename)
Returns True If filename is a valid ZIP file based to its magic number,otherwise Returns False. FileName May is a file or File-like object too
Read the contents of a file directly from a compressed file
ZipFile. Read (name [, PWD]) # actually called Self.open (). Read ()
Return the bytes of the file name in the archive. The name is the name of Thefile in the archive
ZipFile. Open (name [, mode [, PWD]])
Extract a member from the archive as a File-like object (zipextfile)
ZipFile. Close ()
Close the archive file. You are must call Close () before exiting your Programor essential the won't be records. Zipinfo ()
# compress to specified file
def file_zip (SRC, DST):
zip_obj = ZipFile (DST, mode= ' W ')
zip_obj.write (src)
zip_obj.close ( )
# decompression
def unzip (SRC):
zip_obj = ZipFile (src, mode= ' r ') for
info in zip_obj.filelist:
zip_ Obj.extract (Member=info)
zip_obj.close ()
ZipFile command-line interface
Usage:
zipfile.py-l zipfile.zip # Show listing of a zipfile zipfile.py-t # Test if a zip The file is valid
zipfile.py-e zipfile.zip target # Extract ZipFile to target dir
zipfile.py-c zipfile.zip src. .. # Create ZipFile from sources
-C Create
-e Extract (extract)
-L List