Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:22:11+00:00 2026-06-12T17:22:11+00:00

So, I have made some sample KML/KMZ files in the past, largely by hand,

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:22:12+00:00Added an answer on June 12, 2026 at 5:22 pm

    Here’s Java code to create a sample KMZ file using ZipOutputStream with root KML file and an image file entry. If you don’t properly close the KML entry before adding the image entries then the KMZ file can become corrupt.

    IMPORTANT: You must make sure the zip file entries match exactly to the URL references within the KML. The zip file entries should NOT start with ‘/’ or ‘../’ or ‘C:/’. Likewise, the URL/href references to the KMZ entries in the KML should be relative (e.g. icons/b-lv.png) to the particular KML file.

    To reduce the lines of code in the example below, the Apache commons IOUtils library is used to copy the input file to the KMZ output stream and to close the stream.

    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    import org.apache.commons.io.IOUtils;
    import java.io.*;
    
    public class TestKmz {
    
        public static void main(String[] args) throws IOException {     
            createKMZ();
            System.out.println("file out.kmz created");
        }
    
        public static void createKMZ()  throws IOException  {
            FileOutputStream fos = new FileOutputStream("out.kmz");
            ZipOutputStream zoS = new ZipOutputStream(fos);     
            ZipEntry ze = new ZipEntry("doc.kml");
            zoS.putNextEntry(ze);
            PrintStream ps = new PrintStream(zoS);          
            ps.println("<?xml version='1.0' encoding='UTF-8'?>");
            ps.println("<kml xmlns='http://www.opengis.net/kml/2.2'>");     
            // write out contents of KML file ...
            ps.println("<Placemark>");
            // add reference to image via inline style
            ps.println("  <Style><IconStyle>");
            ps.println("    <Icon><href>image.png</href></Icon>");
            ps.println("  </IconStyle></Style>");
            ps.println("</Placemark>");
            ps.println("</kml>");
            ps.flush();                 
            zoS.closeEntry(); // close KML entry
    
            // now add image file entry to KMZ
            FileInputStream is = null;
            try {                   
                is = new FileInputStream("image.png");
                ZipEntry zEnt = new ZipEntry("image.png");
                zoS.putNextEntry(zEnt);
                // copy image input to KMZ output
                // write contents to entry within compressed KMZ file
                IOUtils.copy(is, zoS);
            } finally {
                IOUtils.closeQuietly(is);
            }
            zoS.closeEntry();
            zoS.close();
        }   
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have downloaded some sample source codes, modified them and made a audio-video player.
Just wondering about some practice about this; I have made a simple visual C#
I have made some changes in one perforce client, but haven't submitted them. I
I have made some progress with the DataList and UserControl this morning but I
I have made some screenshots of my website, and in internet explorer 6 my
I have made some screenshots of my website, and in internet explorer 6 my
I have a datagrid in C#.net I have made some columns in grid editable.
I posted a thread about this earlier and have made some progress but now
I'm using Pex and Moles 0.94.51023.0 with VS2010 SP1. I have successfully made some
i have made columns in some of the tables encrypted in sql server 2008.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.