By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Archiving files with zipfile.ZipFile

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. the_records = []
  2.  
  3. for record in tick:
  4.     query = 'select location from download_file where d_id = %s' %record
  5.     print "<br>", query
  6.     curs.execute(query)
  7.     a= curs.fetchone()
  8.     print "<br>", a[0]
  9.     the_records.append(a[0])
  10.  
  11. print "<br>To be zipped: ", the_records
  12.  
  13. def zipdir():
  14.     for b in the_records:
  15.         print "<br>In record: ", b
  16.         try:
  17.             zip.write(the_records + b)
  18.         except IOError:
  19.             None
  20. if __name__=='__main__':
  21.  
  22.     zip = zipfile.ZipFile("C:\\zipfile.zip", 'w')
  23.     zipdir('')
  24.     zip.close
Following is an example that may provide a solution to you:
Expand|Select|Wrap|Line Numbers
  1. """
  2. Function makeArchive is a wrapper for the Python class zipfile.ZipFile
  3. 'fileList' is a list of file names - full path each name
  4. 'archive' is the file name for the archive with a full path
  5. """
  6.  
  7. import zipfile, os
  8.  
  9. def makeArchive(fileList, archive):
  10.     try:
  11.         # ZipFile will accept a file name or file object
  12.         a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
  13.         for f in fileList:
  14.             print "archiving file %s" % (f)
  15.             a.write(f, os.path.basename(f))
  16.         a.close()
  17.         return True
  18.     except: return False
  19.  
  20. if __name__== '__main__':
  21.  
  22.     fileList = ['H:/TEMP/temsys/Anchor Rod Plans.txt',
  23.                 'H:/TEMP/temsys/abc1.txt',
  24.                 'H:/TEMP/temsys/abc2.txt',
  25.                 'H:/TEMP/temsys/abc2.txt',
  26.                 'H:/TEMP/temsys/abc3.txt',
  27.                 'H:/TEMP/temsys/abc4.txt'
  28.                 ]
  29.  
  30.     arcfile_name = 'H:/TEMP/temsys/zipped_files.zip'
  31.  
  32.     if makeArchive(fileList, arcfile_name):
  33.         print arcfile_name, "was created.\n"
  34.         # check the new archive file
  35.         f = zipfile.ZipFile(arcfile_name, 'r')
  36.         for info in f.infolist():
  37.             print info.filename, info.date_time, info.file_size, info.compress_size
  38.         f.close()
  39.     else:
  40.         print "There was an error"
  41.  
  42. """Sample Output
  43. >>> archiving file H:/TEMP/temsys/Anchor Rod Plans.txt
  44. archiving file H:/TEMP/temsys/abc1.txt
  45. archiving file H:/TEMP/temsys/abc2.txt
  46. archiving file H:/TEMP/temsys/abc2.txt
  47. archiving file H:/TEMP/temsys/abc3.txt
  48. archiving file H:/TEMP/temsys/abc4.txt
  49. H:/TEMP/temsys/zipped_files.zip was created.
  50.  
  51. Anchor Rod Plans.txt (2005, 10, 19, 19, 17, 16) 1657 828
  52. abc1.txt (2007, 1, 5, 11, 16, 46) 61 46
  53. abc2.txt (2007, 1, 5, 11, 1, 2) 43 41
  54. abc2.txt (2007, 1, 5, 11, 1, 2) 43 41
  55. abc3.txt (2007, 1, 5, 11, 0, 46) 43 39
  56. abc4.txt (2007, 1, 5, 11, 0, 30) 25 25
  57. >>> """
HTH :-)
Jan 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: 8
thanks..... i'll try this example..... here is another code that i did.....i hope you could comment on it..... as i told you before there is checkbox something like our email and wen a button is pushed after the checkbox is ticked....it'll download the files from its location and created a zip file....so here is the codes if more that 1 box is checked or if only 1 box is checked......

Expand|Select|Wrap|Line Numbers
  1. c = len(str(tick[0])) 
  2.  
  3. if c > 1:
  4.  
  5.     print "<p>The d_id that is checked is:" , tick, "</p>"
  6.  
  7.     the_records = []
  8.  
  9.     for record in tick:
  10.  
  11.     query = 'select location from download_file where d_id = %s' %record
  12.     print "<br>", query 
  13.     curs.execute(query)
  14.     a= curs.fetchone()
  15.     print "<br>", a[0]
  16.     the_records.append(a[0])
  17.  
  18.     print "<p>To be zipped: ", the_records, "</p>"
  19.  
  20.     zip = zipfile.ZipFile("C:\\zipfile.zip", 'w')
  21.  
  22.     for each in the_records:
  23.  
  24.     print "<p> Each: " , each, "</p>" 
  25.     try:
  26.         zip.write(each)
  27.     except IOError:
  28.         None
  29.  
  30.  
  31.     zip.close()
  32.  
  33. elif c == 1:
  34.  
  35.     the_records = []
  36.  
  37.     print "<p>The d_id that is checked is:" , tick, "</p>"
  38.  
  39.     query = 'select location from download_file where d_id = %s' %tick
  40.  
  41.     print "<p>", query, "</p>"
  42.  
  43.     curs.execute(query)
  44.     a= curs.fetchone()
  45.     print "<br>", a[0]
  46.     the_records.append(a[0])
  47.  
  48.     print "<p>To be zipped: ", the_records, "</p>"
  49.  
  50.     zip = zipfile.ZipFile("C:\\Temp\\zipfile.zip", 'w')
  51.  
  52.     for each in the_records:
  53.  
  54.     print "<p> Each: " , each, "</p>"
  55.     try:
  56.         zip.write(each)
  57.     except IOError:
  58.         None
  59.  
  60.     zip.close()
  61.  
but now my problem is that each time da button is pushed...it overwrites the old zip file...so i don't know how could i avoid this....pls help with this.....and don't forget to comment on this new codes....thanks for helping so much.....
Jan 22 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Three things come to mind -
Ask the user for a zip file name.
Check to see if the zip file exists (os.path.isfile(path_file)). If it does, change the filename to something else like zipfile1.zip or zipfile2.zip.
Instantiate zipfile.ZipFile in append mode ('a' instead of 'w'). All files will be added to the same file.

Sorry I cannot be more help with CGI/HTML issues.
Jan 22 '07 #3

P: 8
Thanks..... will give it a try but i don't want the user to give an input as this will hv 2 b in another cgi then which i would like to avoid......thanks once again for really helpin me out.....
Jan 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.