create a zipfile from an input file


/ Published in: Python
Save to your folder(s)

This creates a zipfile with the same name as the input file in the same location as the input file. Only does a single file.


Copy this code and paste it in your HTML
  1. import zlib, zipfile, os
  2.  
  3. def zipme (inputFilename):
  4. fileName = os.path.basename(inputFilename)
  5. zipFileName = os.path.basename (os.path.splitext(fileName)[0]) + '.zip'
  6. zipFilePath = os.path.dirname (inputFilename)
  7. zFile = zipfile.ZipFile(zipFilePath + '/' + zipFileName ,'w')
  8. zFile.write (inputFilename, fileName, zipfile.ZIP_DEFLATED)
  9. zFile.close()
  10. if os.stat(zipFileName):
  11. print 'zipfile created : %s' % (zipFileName)
  12. return 1
  13. else :
  14. print 'Error creating zipfile : %s' % (zipFileName)
  15. return 0

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.