Archiving files with zipfile.ZipFile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    Archiving files with zipfile.ZipFile

    Originally posted by cuties
    Code:
    the_records = []
    
    for record in tick:
        query = 'select location from download_file where d_id = %s' %record
        print "<br>", query
        curs.execute(query)
        a= curs.fetchone()
        print "<br>", a[0]
        the_records.append(a[0])
    
    print "<br>To be zipped: ", the_records
    
    def zipdir():
        for b in the_records:
            print "<br>In record: ", b
            try:
                zip.write(the_records + b)
            except IOError:
                None
    if __name__=='__main__':
    
        zip = zipfile.ZipFile("C:\\zipfile.zip", 'w')
        zipdir('')
        zip.close
    Following is an example that may provide a solution to you:
    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
    >>> """
    HTH :-)
  • cuties
    New Member
    • Jan 2007
    • 8

    #2
    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......

    Code:
    c = len(str(tick[0])) 
    
    if c > 1:
    
        print "<p>The d_id that is checked is:" , tick, "</p>"
    
        the_records = []
        
        for record in tick:
    
    	query = 'select location from download_file where d_id = %s' %record
    	print "<br>", query 
    	curs.execute(query)
    	a= curs.fetchone()
    	print "<br>", a[0]
    	the_records.append(a[0])
    
        print "<p>To be zipped: ", the_records, "</p>"
    
        zip = zipfile.ZipFile("C:\\zipfile.zip", 'w')
        
        for each in the_records:
    
    	print "<p> Each: " , each, "</p>" 
    	try:
    	    zip.write(each)
    	except IOError:
    	    None
    
    
        zip.close()
    
    elif c == 1:
    
        the_records = []
    
        print "<p>The d_id that is checked is:" , tick, "</p>"
    
        query = 'select location from download_file where d_id = %s' %tick
    
        print "<p>", query, "</p>"
    
        curs.execute(query)
        a= curs.fetchone()
        print "<br>", a[0]
        the_records.append(a[0])
    
        print "<p>To be zipped: ", the_records, "</p>"
    
        zip = zipfile.ZipFile("C:\\Temp\\zipfile.zip", 'w')
    
        for each in the_records:
    
    	print "<p> Each: " , each, "</p>"
    	try:
    	    zip.write(each)
    	except IOError:
    	    None
    
        zip.close()
    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.....

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      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.

      Comment

      • cuties
        New Member
        • Jan 2007
        • 8

        #4
        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......than ks once again for really helpin me out.....

        Comment

        Working...