#for Mac OS using python 3
#! /usr/bin/env python3 

import zipfile, os

os.chdir('/Users/alinafe.matenda/Desktop/images') #move to the folder with images

imagesZip = zipfile.ZipFile('images.zip')
print(imagesZip.namelist())
# namelist() method returns a list of strings for all files and folders in the ZIP file.

#if you call your program zipfile
# you will see a AttributeError: module 'zipfile' has no attribute 'ZipFile'
# essentially its attempting to import itself, change the file name.

#Output
#['Screen Shot 2017-06-12 at 01.27.05.png', 'Screen Shot 2017-06-12 at 11.42.52.png', 'statement.jpeg']

#Strings from namelist() are passed into getinfo() to retun "FileInfo" object
#The object has other attributes such as file_size and compress_size(bytes).
FileInfo = imagesZip.getinfo('statement.jpeg')
print('File size ',FileInfo.file_size)
print('Compression information: ',FileInfo.compress_size)
print('Compressed file is %sx smaller!' % (round(FileInfo.file_size / FileInfo.compress_size, 2)))

#output
#114979
#105141
#Compressed file is 1.09x smaller!
#close
imagesZip.close()
Advertisements