python3 operate ZIP file

>>>import zipfile , oside


//infomation for zip filespa

>>>zipFileHandle = zipfile.ZipFile('example.zip')   //create zip file handleip

>>>zipFileHandle.namelist()      //return all files and directorys in the zip fileget

>>>spamInfo = zipFileHandle.getinfo('filename') //return a object of file in the zip fileit

>>>spamInfo.filename     //return filenameio

>>>spamInfo.file_size    //return file original sizeclass

>>>spamInfo.compress_size   // file compressed sizeimport

>>>round(spamInfo.compress_size / spamInfo/file_size , 2)    //compress ratesed


//unzipobject

>>>zipFileHandle.extractall()      //unzip all to current directory

>>>zipFileHandle.extract('filename')   //unzip 'filename' file to current directory


//close handle

>>>zipFileHandle.close()


//create a new zip file

>>>newZip = zipfile.ZipFile('new.zip','w')

>>>newZip.write('filename' , compress_type=zipfile.ZIP_DEFLATED)

>>>newZip.close()


//add files into a zip file

>>>zipFileHandle = zipfile.ZipFile('oldfile.zip' , 'a')

>>>zipFileHandle.write('filename' , compress_type=zipfile.ZIP_DEFLATED)

>>>>>>zipFileHandle.close()

相關文章
相關標籤/搜索