Connecting Tech Pros Worldwide Forums | Help | Site Map

Archiving files with zipfile.ZipFile

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,564
#1: Jan 19 '07
Quote:

Originally Posted by cuties

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 :-)

Newbie
 
Join Date: Jan 2007
Posts: 8
#2: Jan 22 '07

re: Archiving files with zipfile.ZipFile


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.....
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,564
#3: Jan 22 '07

re: Archiving files with zipfile.ZipFile


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.
Newbie
 
Join Date: Jan 2007
Posts: 8
#4: Jan 22 '07

re: Archiving files with zipfile.ZipFile


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.....
Reply