Replace a file inside a ZIP file

1127
0
10-26-2011 11:11 AM
KristinaGrace
New Contributor II
Hello,

I have a Python script that creates a .KMZ file, renames it to .ZIP, extracts the files from the .ZIP into a folder, replaces text inside the doc.kml (adds screen overlay). The next step I thought would be to put the .kml file back in the .ZIP and rename the .ZIP back to .KMZ, but I cannot get Python to do that for me.

Here's the piece I think I need to change (the entire Python script is attached):

    # open the zipped file for writing
    zfile = zipfile.ZipFile( basecamp + newZIP, 'w' )
    arcpy.AddMessage('opened the zip file for writing')
    print 'opened the zip file for writing'
    KMLData = open(basecamp + KMLFile, 'r')
    for info2 in zfile.infolist():
        fname2 = info2.filename
        if fname2 == basecamp + newZIP + KMLFile:
            KMLWrite = open(fname2, 'w')
            KMLWrite.write(KMLData)
            KMLWrite.close()
            arcpy.AddMessage( 'KML replaced successfully')
            print 'KML replaced successfully'
    KMLData.close()
    zfile.close()
Tags (2)
0 Kudos
0 Replies