analitics

Pages

Sunday, July 14, 2019

Python 3.7.3 : Simple tests with zipfile python module.

You can read about this python module here.
The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.

This module does not currently handle multi-disk ZIP files. It can handle ZIP files that use the ZIP64 extensions (that is ZIP files that are more than 4 GiB in size). It supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file. Decryption is extremely slow as it is implemented in native Python rather than C.

C:\Python373> python -m zipfile -c test.zip test.html  textalongpath.pdf
C:\Python373> python -m zipfile -c test_folder.zip test.html  temp
C:\Python373>python -m zipfile -l  test.zip
File Name                                             Modified             Size
test.html                                      2019-07-11 10:46:58         6115
textalongpath.pdf                              2019-06-08 22:55:50           84

C:\Python373>python -m zipfile -l  test_folder.zip
File Name                                             Modified             Size
test.html                                      2019-07-11 10:46:58         6115
temp/                                          2019-07-07 21:36:42            0
temp/wlop/                                     2019-07-07 21:36:42            0
This lines of code will create two archives named test.zip and test_folder.zip with the files shown on each command.
For extraction, is need to use the -e argument:
C:\Python373>python -m zipfile -e test.zip zipfiles/

C:\Python373>python -m zipfile -e test_folder.zip zipfiles/

C:\Python373>cd zipfiles

C:\Python373\zipfiles>dir
...
Let's using this python module inside python:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import zipfile
>>> import datetime
>>> my_zip = zipfile.ZipFile('test.zip','r')
>>> print (my_zip.namelist())
['test.html', 'textalongpath.pdf']
>>> def print_info(archive_name):
...     my_zip = zipfile.ZipFile(archive_name)
...     for info in my_zip.infolist():
...             print (info.filename)
...             print ('Comment: ', info.comment)
...             print ('Modified: ', datetime.datetime(*info.date_time))
...             print ('System: ', info.create_system, '(0 = Windows, 3 = Unix)'
)
...             print ('ZIP version: ', info.create_version)
...             print ('Compressed: ', info.compress_size, 'bytes')
...             print ('Uncompressed: ', info.file_size, 'bytes')
...
>>> print_info('test_folder.zip')
test.html
Comment:  b''
Modified:  2019-07-11 10:46:58
System:  0 (0 = Windows, 3 = Unix)
ZIP version:  20
Compressed:  1679 bytes
Uncompressed:  6115 bytes
temp/
Comment:  b''
Modified:  2019-07-07 21:36:42
...
Extract all files from an archive:
>>> from zipfile import ZipFile
>>> with ZipFile('test.zip','r') as zipObj:
...     zipObj.extractall()
Extract files by extension:
>>> with ZipFile('test.zip', 'r') as zipObj:
...    listOfFileNames = zipObj.namelist()
...    for fileName in listOfFileNames:
...        if fileName.endswith('.html'):
...            zipObj.extract(fileName, 'new.html')
...
'new.html\\test.html'
Create a new arhive named The_new.zip and add the new.html file on it.
>>> zipObj = ZipFile('The_new.zip','w')
>>> zipObj.write('new.html')
>>> zipObj.close()
>>> print_info('The_new.zip')
new.html/
Comment:  b''
Modified:  2019-07-14 22:15:58
System:  0 (0 = Windows, 3 = Unix)
ZIP version:  20
Compressed:  0 bytes
Uncompressed:  0 bytes