Class KMZFile

  • All Implemented Interfaces:
    KMLDoc

    public class KMZFile
    extends java.lang.Object
    implements KMLDoc
    Implements the KMLDoc interface for KMZ files located within a computer's file system.

    Note: This class does not yet resolve references to files in other KMZ archives. For example, it does not resolve references like this: ../other.kmz/file.png.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<java.lang.String,​java.io.File> files
      A mapping of the files in the KMZ file to their location in the temporary directory.
      protected java.io.File tempDir
      The directory to hold files copied from the KMZ file.
      protected java.util.zip.ZipFile zipFile
      The ZipFile reference specified to the constructor.
    • Constructor Summary

      Constructors 
      Constructor Description
      KMZFile​(java.io.File file)
      Construct a KMZFile instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.String copyEntryToTempDir​(java.util.zip.ZipEntry entry)
      Copies a zip entry to a temporary file.
      java.io.InputStream getKMLStream()
      Returns an InputStream to the first KML file in the KMZ file.
      java.lang.String getSupportFilePath​(java.lang.String path)
      Returns an absolute path to a specified file within the KMZ file.
      java.io.InputStream getSupportFileStream​(java.lang.String path)
      Returns an InputStream to a specified file within the KMZ file.
      java.util.zip.ZipFile getZipFile()
      Returns the file file specified to the constructor as a ZipFile.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • zipFile

        protected java.util.zip.ZipFile zipFile
        The ZipFile reference specified to the constructor.
      • files

        protected java.util.Map<java.lang.String,​java.io.File> files
        A mapping of the files in the KMZ file to their location in the temporary directory.
      • tempDir

        protected java.io.File tempDir
        The directory to hold files copied from the KMZ file. The directory and the files copied there are temporary.
    • Constructor Detail

      • KMZFile

        public KMZFile​(java.io.File file)
                throws java.io.IOException
        Construct a KMZFile instance.
        Parameters:
        file - path to the KMZ file.
        Throws:
        java.io.IOException - if an error occurs while attempting to query or open the file.
        java.lang.IllegalArgumentException - if the specified file is null.
        java.util.zip.ZipException - if a Zip error occurs.
    • Method Detail

      • getZipFile

        public java.util.zip.ZipFile getZipFile()
        Returns the file file specified to the constructor as a ZipFile.
        Returns:
        the file specified to the constructor, as a ZipFile.
      • getKMLStream

        public java.io.InputStream getKMLStream()
                                         throws java.io.IOException
        Returns an InputStream to the first KML file in the KMZ file.
        Specified by:
        getKMLStream in interface KMLDoc
        Returns:
        an input stream positioned to the first KML file in the KMZ file, or null if the KMZ file does not contain a KML file.
        Throws:
        java.io.IOException - if an error occurs while attempting to create or open the input stream.
      • getSupportFileStream

        public java.io.InputStream getSupportFileStream​(java.lang.String path)
                                                 throws java.io.IOException
        Returns an InputStream to a specified file within the KMZ file. The file's path is resolved relative to the internal root of the KMZ file.

        Note: This class does not yet resolve references to files in other KMZ archives. For example, it does not resolve references like this: ../other.kmz/file.png.

        Specified by:
        getSupportFileStream in interface KMLDoc
        Parameters:
        path - the path of the requested file.
        Returns:
        an input stream positioned to the start of the requested file, or null if the file does not exist or the specified path is absolute.
        Throws:
        java.lang.IllegalArgumentException - if the path is null.
        java.io.IOException - if an error occurs while attempting to create or open the input stream.
      • getSupportFilePath

        public java.lang.String getSupportFilePath​(java.lang.String path)
                                            throws java.io.IOException
        Returns an absolute path to a specified file within the KMZ file. The file's path is resolved relative to the internal root of the KMZ file.

        Note: This class does not yet resolve references to files in other KMZ archives. For example, it does not resolve references like this: ../other.kmz/file.png. // TODO

        Specified by:
        getSupportFilePath in interface KMLDoc
        Parameters:
        path - the path of the requested file.
        Returns:
        an absolute path to the requested file, or null if the file does not exist or the specified path is absolute.
        Throws:
        java.lang.IllegalArgumentException - if the path is null.
        java.io.IOException - if an error occurs while attempting to create a temporary file.
      • copyEntryToTempDir

        protected java.lang.String copyEntryToTempDir​(java.util.zip.ZipEntry entry)
                                               throws java.io.IOException
        Copies a zip entry to a temporary file. This method should only be called by a synchronized public method.
        Parameters:
        entry - the entry to copy.
        Returns:
        the path to the file, or null if the entry is a directory or the temporary directory cannot be created.
        Throws:
        java.io.IOException - if an error occurs during the copy.