I have been using JAK for over a year in the project. I use it to create KML and then create it as simple KML (not KMZ). I created a separate utility class that uses the Java SE 'Zip' classes to manually create KMZ. It works great. KMZ is nothing more than a .zip archive containing exactly one .kml file and 0 or more resource files (for example, images, etc.). The only difference is that you specify the file as .kmz instead of .zip when you output it. For KML <Style> document definitions, refer to resource files with paths relative to the KML document itself. The KML file is considered to be located in the root of the KMZ archive. If your resource files are also in the root directory of KMZ (.zip), you don't need a path, just a file name.
EDIT: I really forgot that I removed the intermediate step of sorting the JAK Kml into a file before I zipped it up. My utility method below will marshal the Kml object directly to ZipOutputStream .
Here is a utility class that I created to do what I described. I am posting it here in the hope that someone else will post an alternative solution that uses only JAK so that I can resign from this code in the future. So far this will do your job.
NOTE. If you are not using slf4j, Apache Commons Lang, or Commons I / O, just make a few changes to the code to remove or replace these bits with your own code. Obviously, this code requires the JAK library.
package com.jimtough; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Collections; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import de.micromata.opengis.kml.v_2_2_0.Kml; public final class KMZPackager { private final static Logger logger = LoggerFactory.getLogger(KMZPackager.class); public static abstract class DataSource { protected String archivedFileName; public abstract void writeToStream(ZipOutputStream zipOutputStream) throws IOException; } public static final class FileDataSource extends DataSource { private File sourceFile; public FileDataSource( File sourceFile, String archivedFileName) throws IllegalArgumentException { Validate.notNull(sourceFile); Validate.notEmpty(archivedFileName); this.sourceFile = sourceFile; this.archivedFileName = archivedFileName; } @Override public void writeToStream(ZipOutputStream zipOutputStream) throws IOException { Validate.notNull(zipOutputStream);
source share