Tuesday, May 22, 2007

Creating a KMZ from KML in Java using Zip

This is a very quick post just because I didn't find something when searching that did exactly what I wanted.

Scenario: I have an XML doc (that I have converted to a String) which represents the KML file and I want to return a Zipped version of that file to save on bandwidth and the like. First of all you need to have somewhere to get the KML file from (in this case counter.getKML().


response.setContentType("application/vnd.google-earth.kmz");
ZipEntry entry = new ZipEntry("geoblog.kml");
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
zipOutputStream.setLevel(9);
zipOutputStream.putNextEntry(entry);
zipOutputStream.write(counter.getKML().getBytes("UTF-8"));
zipOutputStream.closeEntry();
zipOutputStream.close();


And that is it. Mind bogglingly simple.

Technorati Tags: ,

1 comment:

Anonymous said...

Awesome!
Thanks this really helped me a lot.