By default, the ZipOutputStream class in Java will create a compatible KMZ file that Google Earth can read.
In ZipEntry, you can specify either the STORED compression or the DEFLATED method, both of which are compatible with Google Earth.
- Pay attention to any ZIP library or API that you use, you should definitely specify ZIP 2.0 or "obsolete" compression methods (ie STORED and DEFLATE methods) if they are not standard methods. The DEFLATE method is called SuperFast and STORED is called None or "No Compression" in the WinZip Documentation .
- Google Earth also supports the maximum or advanced deflation method, which is often displayed with the short name " Def: X ".
- More advanced compression methods (e.g. bzip2, LZMA, etc.) are NOT compatible with Google Earth, and such KMZ files will be silently ignored when opened.
Here is a simple piece of code to create a KMZ file in Java.
FileOutputStream fos = new FileOutputStream("example.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("<Document>"); ps.println("<Placemark>"); // ... ps.println("</Placemark>"); ps.println("</Document>"); ps.println("</kml>"); ps.flush(); zoS.closeEntry(); // close KML entry // include and write other files (Eg icons, overlays, other KML files, etc.) zoS.close();
source share