Kristya

Replace a file inside a ZIP file

Discussion created by Kristya on Oct 26, 2011
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()

Outcomes