Java API for KML (JAK) embedding images in kmz files

后端 未结 2 822
难免孤独
难免孤独 2020-12-18 08:48

Is there a way to just add an image file into a kmz file using Java API for KML (JAK)? I can create a kml file with no problem, but I\'m trying to just embed a resources (su

2条回答
  •  悲哀的现实
    2020-12-18 09:33

    Java comes with a default zip utility, all you need to do is to pass the kml file you need to zip along with the list of image files. These two functions should do the trick.

       private void exportAsKmz(Kml kmlFile,File[] files) throws IOException {
          ZipOutputStream out = new ZipOutputStream(new FileOutputStream("example.kmz"));
          addKmzFile(kmlFile,out,files);
          out.close();
       }
    
       private void addKmzFile(Kml kmzFile, ZipOutputStream out,File[] files) throws IOException {
          String fileName = "";
          fileName = kmzFile.getFeature().getName();
          if (!fileName.endsWith(".kml")) {
             fileName += ".kml";
          }
          for(File file : files){
             out.putNextEntry(new ZipEntry(file.getName()));
             Files.copy(file.toPath(), out);
          }
          out.putNextEntry(new ZipEntry(URLEncoder.encode(fileName, "UTF-8")));
          kmzFile.marshal(out);
          out.closeEntry();
       }
    

提交回复
热议问题