Originally posted by cuties
Code:
"""
Function makeArchive is a wrapper for the Python class zipfile.ZipFile
'fileList' is a list of file names - full path each name
'archive' is the file name for the archive with a full path
"""
import zipfile, os
def makeArchive(fileList, archive):
try:
# ZipFile will accept a file name or file object
a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
for f in fileList:
print "archiving file %s" % (f)
a.write(f, os.path.basename(f))
a.close()
return True
except: return False
if __name__== '__main__':
fileList = ['H:/TEMP/temsys/Anchor Rod Plans.txt',
'H:/TEMP/temsys/abc1.txt',
'H:/TEMP/temsys/abc2.txt',
'H:/TEMP/temsys/abc2.txt',
'H:/TEMP/temsys/abc3.txt',
'H:/TEMP/temsys/abc4.txt'
]
arcfile_name = 'H:/TEMP/temsys/zipped_files.zip'
if makeArchive(fileList, arcfile_name):
print arcfile_name, "was created.\n"
# check the new archive file
f = zipfile.ZipFile(arcfile_name, 'r')
for info in f.infolist():
print info.filename, info.date_time, info.file_size, info.compress_size
f.close()
else:
print "There was an error"
"""Sample Output
>>> archiving file H:/TEMP/temsys/Anchor Rod Plans.txt
archiving file H:/TEMP/temsys/abc1.txt
archiving file H:/TEMP/temsys/abc2.txt
archiving file H:/TEMP/temsys/abc2.txt
archiving file H:/TEMP/temsys/abc3.txt
archiving file H:/TEMP/temsys/abc4.txt
H:/TEMP/temsys/zipped_files.zip was created.
Anchor Rod Plans.txt (2005, 10, 19, 19, 17, 16) 1657 828
abc1.txt (2007, 1, 5, 11, 16, 46) 61 46
abc2.txt (2007, 1, 5, 11, 1, 2) 43 41
abc2.txt (2007, 1, 5, 11, 1, 2) 43 41
abc3.txt (2007, 1, 5, 11, 0, 46) 43 39
abc4.txt (2007, 1, 5, 11, 0, 30) 25 25
>>> """
Comment