Class ZipHandler

  • Direct Known Subclasses:
    JarHandler

    public class ZipHandler
    extends java.lang.Object
    Implements a handler function for ZIP files (which is prepared for handling JAR files in a similar way).
    Author:
    Holger Eichelberger, kroeher
    • Constructor Summary

      Constructors 
      Constructor Description
      ZipHandler()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<java.io.File> add​(java.io.File base, java.util.List<java.io.File> files, java.io.File zip)
      Adds a set of files (relative to base) to the ZIP file zip.
      protected void additionalUnpacking​(java.util.zip.ZipInputStream zis, java.io.File target, java.util.List<java.io.File> files)
      Performs additional unpacking (called by unpack(File, File, String, List)).
      protected java.util.zip.ZipEntry createEntry​(java.lang.String name, java.io.File file)
      Creates a ZIP entry.
      protected java.util.zip.ZipInputStream createInputStream​(java.io.InputStream in)
      Creates a ZIP input stream for in.
      protected java.util.zip.ZipOutputStream createOutputStream​(java.io.OutputStream out)
      Creates a ZIP output stream for out.
      protected boolean include​(java.io.File file)
      Returns whether file shall be included in repackaging a JAR file.
      private java.lang.String makeRelative​(java.lang.String basePath, java.io.File file)
      Makes a file path relative to the given basePath.
      private static boolean matches​(java.util.zip.ZipEntry entry, java.lang.String pattern)
      Checks whether an entry matches a filter expression.
      private void packInto​(java.lang.String basePath, java.io.File source, java.util.zip.ZipOutputStream out, java.util.Set<java.lang.String> done)
      Packs the files in source into out.
      java.io.File unpack​(java.io.File zip, java.io.File target, java.lang.String pattern, java.util.List<java.io.File> files)
      Unpacks an ZIP file to target.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ZipHandler

        public ZipHandler()
    • Method Detail

      • add

        public java.util.List<java.io.File> add​(java.io.File base,
                                                java.util.List<java.io.File> files,
                                                java.io.File zip)
                                         throws java.io.IOException
        Adds a set of files (relative to base) to the ZIP file zip. If zip exists, files are added to zip, i.e., zip is overwritten with an updated version. If zip does not exist, a new zip archive will be created.
        Parameters:
        base - the base directory used for making files relative
        files - the files to be put into zip
        zip - the ZIP file
        Returns:
        the modified files (actually only ZIP)
        Throws:
        java.io.IOException - in case that serious I/O problems occurred
      • makeRelative

        private java.lang.String makeRelative​(java.lang.String basePath,
                                              java.io.File file)
        Makes a file path relative to the given basePath.
        Parameters:
        basePath - the base path
        file - the file to be made relative
        Returns:
        the relative path (if file can be made relative)
      • createInputStream

        protected java.util.zip.ZipInputStream createInputStream​(java.io.InputStream in)
                                                          throws java.io.IOException
        Creates a ZIP input stream for in. This method shall be overridden for specialized ZIP types, e.g., JAR.
        Parameters:
        in - the input stream to create the ZIP stream for.
        Returns:
        the ZIP input stream
        Throws:
        java.io.IOException - in case that creating the input stream fails
      • createOutputStream

        protected java.util.zip.ZipOutputStream createOutputStream​(java.io.OutputStream out)
                                                            throws java.io.IOException
        Creates a ZIP output stream for out. This method shall be overridden for specialized ZIP types, e.g., JAR.
        Parameters:
        out - the output stream to create the ZIP stream for.
        Returns:
        the ZIP output stream
        Throws:
        java.io.IOException - in case that creating the output stream fails
      • createEntry

        protected java.util.zip.ZipEntry createEntry​(java.lang.String name,
                                                     java.io.File file)
        Creates a ZIP entry. This method shall be overridden for specialized ZIP types, e.g., JAR.
        Parameters:
        name - the name of the entry
        file - the underlying file
        Returns:
        the ZIP entry
      • unpack

        public java.io.File unpack​(java.io.File zip,
                                   java.io.File target,
                                   java.lang.String pattern,
                                   java.util.List<java.io.File> files)
                            throws java.io.IOException
        Unpacks an ZIP file to target.
        Parameters:
        zip - the ZIP file to be unpacked (nothing happens if zip does not exist)
        target - the target directory where to unpack (a temporary directory is created if target is null)
        pattern - an optional ANT pattern used as a filter expression, typically a specific folder or package. May be null if no filter is required.
        files - the unpacked files (may contain directories, modified as a side effect, may be null if the unpacked files shall not be collected)
        Returns:
        the target directory, in particular a newly created temporary directory
        Throws:
        java.io.IOException - in case that unpacking fails for some reason
      • matches

        private static boolean matches​(java.util.zip.ZipEntry entry,
                                       java.lang.String pattern)
        Checks whether an entry matches a filter expression.
        Parameters:
        entry - the Zip entry
        pattern - an optional ANT pattern used as a filter expression, typically a specific folder or package. May be null if no filter is required.
        Returns:
        true if entry matches filter or filter is null, false otherwise
      • additionalUnpacking

        protected void additionalUnpacking​(java.util.zip.ZipInputStream zis,
                                           java.io.File target,
                                           java.util.List<java.io.File> files)
                                    throws java.io.IOException
        Performs additional unpacking (called by unpack(File, File, String, List)).
        Parameters:
        zis - the ZIP input stream
        target - the target directory where to unpack (a temporary directory is created if target is null)
        files - the unpacked files (may contain directories, modified as a side effect, may be null if the unpacked files shall not be collected)
        Throws:
        java.io.IOException - in case that unpacking fails for some reason
      • packInto

        private void packInto​(java.lang.String basePath,
                              java.io.File source,
                              java.util.zip.ZipOutputStream out,
                              java.util.Set<java.lang.String> done)
                       throws java.io.IOException
        Packs the files in source into out.
        Parameters:
        basePath - the base path (shall be the absolute path of source for the first call
        source - the source directory to take the files from
        out - the ZIP output stream to write the files into
        done - already processed ZIP paths
        Throws:
        java.io.IOException - in case that reading or creating files fails
      • include

        protected boolean include​(java.io.File file)
        Returns whether file shall be included in repackaging a JAR file.
        Parameters:
        file - the file to be considered
        Returns:
        true if file shall be packed, false else