Package jahuwaldt.io

Class ZipUtils


  • public final class ZipUtils
    extends java.lang.Object
    This is a utility class of static methods for working with ZIP archive files.

    Modified by: Joseph A. Huwaldt

    Version:
    September 16, 2016
    Author:
    Behrouz Fallahi, Date: April 28, 2000
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char[] ILLEGAL_CHARACTERS
      A list of characters that are illegal on some of the file systems supported by this program.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String cleanFileName​(java.lang.String name)
      Replace any potentially illegal characters from a file name with '_'.
      static void extractZip​(java.io.File zipFile, java.io.File outDir)
      Extracts a ZIP archive file to the specified directory.
      static void extractZip​(java.io.InputStream input, java.io.File outDir)
      Extracts a ZIP archive file to the specified directory.
      static boolean hasIllegalChars​(java.lang.String name)
      Returns true if the supplied file name contains characters that are illegal on some file systems.
      static void writeZip​(java.io.File fileOrDir, java.io.File zipFile)
      Create a ZIP archive file made up of all the contents of the specified file or directory.
      static void writeZip​(java.io.File fileOrDir, java.io.File zipFile, int zipLevel)
      Create a ZIP archive file made up of all the contents of the specified file or directory, using the specified compression method.
      static void writeZip​(java.io.File fileOrDir, java.io.OutputStream out, int zipLevel)
      Write a ZIP archive to the specified output stream made up of all the contents of the specified file or directory, using the specified compression method.
      • Methods inherited from class java.lang.Object

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

      • ILLEGAL_CHARACTERS

        public static final char[] ILLEGAL_CHARACTERS
        A list of characters that are illegal on some of the file systems supported by this program. The characters in this list include: \n,\r,\t,\0,\f,',?,*,<,>,|,",:,~,@,!,#,[,],=,+,;, and ','.
    • Method Detail

      • cleanFileName

        public static java.lang.String cleanFileName​(java.lang.String name)
        Replace any potentially illegal characters from a file name with '_'.
        Parameters:
        name - The file name to be cleaned of potentially illegal characters. May not be null.
        Returns:
        The input file name with potentially illegal characters replaced with "_".
      • hasIllegalChars

        public static boolean hasIllegalChars​(java.lang.String name)
        Returns true if the supplied file name contains characters that are illegal on some file systems.
        Parameters:
        name - The file name to be checked for potentially illegal characters. May not be null.
        Returns:
        true if the file name contains potentially illegal characters, false if it is safe.
      • writeZip

        public static void writeZip​(java.io.File fileOrDir,
                                    java.io.OutputStream out,
                                    int zipLevel)
                             throws java.io.IOException
        Write a ZIP archive to the specified output stream made up of all the contents of the specified file or directory, using the specified compression method. If any of the files in the specified directory contain characters that are illegal on some file systems, then those files are skipped and not included in the archive. The output stream is closed by this method.
        Parameters:
        fileOrDir - The file or directory to be compressed into a ZIP archive. May not be null.
        out - The output stream to write the ZIP archive data to. May not be null.
        zipLevel - The compression level (0-9) or java.util.zip.Deflater.DEFAULT_COMPRESSION.
        Throws:
        java.io.IOException - If there is any problem writing out the ZIP stream.
        See Also:
        java.util.zip.Deflater.DEFAULT_COMPRESSION
      • writeZip

        public static void writeZip​(java.io.File fileOrDir,
                                    java.io.File zipFile,
                                    int zipLevel)
                             throws java.io.IOException
        Create a ZIP archive file made up of all the contents of the specified file or directory, using the specified compression method. If any of the files in the specified directory contain characters that are illegal on some file systems, then those files are skipped and not included in the archive.
        Parameters:
        fileOrDir - The file or directory to be compressed into a ZIP archive. May not be null.
        zipFile - The ZIP file to be written out. May not be null.
        zipLevel - The compression level (0-9) or java.util.zip.ZipOutputStream.DEFAULT_COMPRESSION.
        Throws:
        java.io.IOException - If there is any problem writing out the ZIP file.
        See Also:
        java.util.zip.ZipOutputStream.DEFAULT_COMPRESSION
      • writeZip

        public static void writeZip​(java.io.File fileOrDir,
                                    java.io.File zipFile)
                             throws java.io.IOException
        Create a ZIP archive file made up of all the contents of the specified file or directory. If any of the files in the specified directory contain characters that are illegal on some file systems, then those files are skipped and not included in the archive.
        Parameters:
        fileOrDir - The file or directory to be compressed into a ZIP archive. May not be null.
        zipFile - The ZIP file to be written out. May not be null.
        Throws:
        java.io.IOException - If there is any problem writing out the ZIP file.
      • extractZip

        public static void extractZip​(java.io.InputStream input,
                                      java.io.File outDir)
                               throws java.io.IOException
        Extracts a ZIP archive file to the specified directory. If the ZIP archive contains files with characters that might be illegal on some file systems, those characters are replaced with underline characters, '_'.
        Parameters:
        input - An InputStream from a ZIP archive. May not be null.
        outDir - The directory to extract the ZIP file into. May not be null.
        Throws:
        java.io.IOException - If there is any problem extracting from the ZIP stream.
      • extractZip

        public static void extractZip​(java.io.File zipFile,
                                      java.io.File outDir)
                               throws java.io.IOException
        Extracts a ZIP archive file to the specified directory. If the ZIP archive contains files with characters that might be illegal on some file systems, those characters are replaced with underline characters, '_'.
        Parameters:
        zipFile - The ZIP file to be extracted. May not be null.
        outDir - The directory to extract the ZIP file into. May not be null.
        Throws:
        java.io.IOException - If there is any problem extracting the ZIP file.