So, I have made some sample KML/KMZ files in the past, largely by hand, and have discovered a few inconsistencies with Google Earth as a result of this work. For the most part, I thought I had a good handle on what I needed to do for specific versions of GE. Recently, I attempted to add some automation to my KML/KMZ sample files to customize them for certain clients based on a given CSV of points and other metadata.
I want to reach out to those with experience creating KMZ files from Java with custom icons or content. Here's the wrinkle I'm stuck on:
I created, by hand, a bunch of styles and custom icons to better display client data. Recently, I created a Java app that allows me to save a great deal of time by automating the KML/KMZ production. These styles were copied directly from the examples into my Java code (with escapes in their rightful places). The Java code initially built the KML, then I packaged the KML with the icons folder from my examples, zipped and saved as KMZ. All worked fine.
I then added a code block largely based on this example and added all my icons as resources to the jar. I then built the file as a .zip and verified that the KML and Icon files (in appropriate folders) were in the zip. They were. All was happy with the world.
I then changed the output filename to .kmz instead of .zip and tried to run the output in Google. None of my custom icons are loaded. The KML works fine, points and polygons are there with the proper style colors, but there are boxes with X's in the middle as if it can't access the icons.
If I rename the output KMZ to zip, unpack and run the KML inside it, everything works as expected. If I rezip and rename to KMZ, it's broken again.
Here's the real fun though. If I take the KML out of the KMZ, repack with the icons folder from the resources from my workspace, save as KMZ and load into google earth, it works OK.
I feel like that is telling me that my java.util.zip code is somehow corrupting the images to a point where GE doesn't know what to do with them. But I'm completely confused as to why they work fine when unpackaged, but then again broke when repackaged from the same location.
Anyone have any ideas? Apologies in advance for not posting code. I will post what I can if we can narrow down the problem space a little.
Here's as much code as I can transcribe at the moment:
//Create new file output based on file-name of previously made KML file (fileOut)
//nameToken exists to pop KML extension off the back end of fileOut.getName()
File fileOut2 = new File(fileOut.getParent(), nameToken2[0] + ".kmz");
FileOutputStream foutstream = new FileOutputStream(fileOut2);
ZipOutputStream zout = new ZipOutputStream(foutstream);
byte[] buffer = new byte[1024];
String[] resourceFiles = {null,"/icons/b-lv.png",...}; //many files listed here
for(int i = 0; i < resourceFiles.length; i++){
//Previously wrote kml file, time to read it in and add to zip
if (i == 0){
FileInputStream fin = new FileInoutStream(fileOut);
zout.putNextEntry(new ZipEntry(fileOut.getName());
int length;
while ((length = fin.read(buffer)) > 0){
zout.write(buffer,0,length);
}
zout.closeEntry();
fin.close();
}
//Read in resource icon files and add to zip
else{
InputStream inStream = this.getClass().getResourceAsStream(resourceFiles[i]);
zout.putNextEntry(new ZipEntry(resourceFiles[i]));
int length;
while((length = inStream.read(buffer)) > 0){
zout.write(buffer,0,length);
}
zout.closeEntry();
inStream.close();
}
}
zout.flush();
zout.close();
foutstream.close();
fileOut.delete(); //Deletes previously made KML file