Search code examples
javagroovykmlgoogle-earthkmz

Can't get images to display properly in KMZ using zipentry in Java/Groovy


Basically, I've been trying to get images to display under the attribute of a KMZ file. My current setup creates a unzipped directory with the following structure:

Main Folder/ doc.kml files/ img1.jpeg img2.jpeg img3.jpeg ... and then I go through and zip everything using the following code into a KMZ

    void zipFile(File directory, File file, ZipOutputStream zip) {
        if (!file.isDirectory()){
            def input = new FileInputStream(file)
            String name = file.getPath()
            name = name.replace(kmzDir.path+"\\", "")
            zip.putNextEntry(new ZipEntry(name))
            IOUtils.copy(input, zip)
            zip.closeEntry()
        }

    }

    void zipKMZDirectory() {

        BufferedOutputStream fileWriter = new BufferedOutputStream(new FileOutputStream(new File(kmzDir.path + ".kmz")))
        ZipOutputStream zip = new ZipOutputStream(fileWriter)
        resourceDir.listFiles().each { file ->
            zipFile(resourceDir, file, zip)
        }
        kmzDir.listFiles().each { file ->
            zipFile(kmzDir, file, zip)
        }
        zip.close()
        fileWriter.close()
    }

Opening the doc.kml file outside of the KMZ works perfectly fine (loads the images in google earth), but opening the the KMZ file and trying to see any images just shows a blue question mark in place of the image.

I tried viewing the network data when it attempts to load an image (right click -> inspect -> network) and it says it successfully found the image under Main Folder/files/image.jpeg. Also, I tried removing all the images stored in the kmz archive and just manually adding all the images back into the zip through 7zip which does show the images. Which tells me there is something going wrong during the zip compression stage (?) that is not allowing google to open the image (though I did just try to view the images myself and they aren't corrupted or anything).

Edit: I am using Google Earth Pro on the desktop


Solution

  • I'm not sure what the issue was in the end, I'm assuming it was in the way in which I was making the zip file that was corrupting something. I took a different route to fix it thought, instead of making the directory and then compressing it, I essentially just zipped the files as they were being made (following this: https://stackoverflow.com/a/12762850/5304918 as an outline)