import zipfile
def data_to_zip_direct(z, data, name):
import time
zinfo = zipfile.ZipInfo(name, time.localtime()[:6])
z.writestr(zinfo, data)
def data_to_zip_indirect(z, data, name):
import os
flob = open(name, 'wb')
flob.write(data)
flob.close()
z.write(name)
os.unlink(name)
zz = zipfile.ZipFile('z.zip', 'w', zipfile.ZIP_DEFLATED)
data = 'four score\nand seven\nyears ago\n'
data_to_zip_direct(zz, data, 'direct.txt')
data_to_zip_indirect(zz, data, 'indirect.txt')
zz.close()
|