Class Zipper

java.lang.Object
com.mediaworx.ziputils.Zipper

public class Zipper extends Object
Convenience class providing an easy way to create zip files and add files or folders to it. Uses Apache commons-compress under the hood.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Zipper(String zipFilename, String zipTargetFolderPath)
    Creates a new zip file.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addDirectory(String zipRelativePath)
    Adds a directory entry with the given relative path to the zip.
    void
    addFile(String zipRelativePath, File file)
    Adds the given file to the zip archive using the given relative path.
    void
    addStringAsFile(String zipRelativePath, String plaintextContent)
    Adds the given plaintextContent as a file to the zip archive using the given relative path using the default encoding (UTF-8).
    void
    addStringAsFile(String zipRelativePath, String plaintextContent, String encoding)
    Adds the given plaintextContent as a file to the zip archive using the given relative path using the given encoding.
    void
    Finalizes the zip file and writes it to the disk.

    Methods inherited from class java.lang.Object

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

    • Zipper

      public Zipper(String zipFilename, String zipTargetFolderPath) throws IOException
      Creates a new zip file.
      Parameters:
      zipFilename - filename for the zip
      zipTargetFolderPath - path of the target folder where the zip should be stored (it will be created if it doesn't exist)
      Throws:
      IOException - Exceptions from the underlying package framework are bubbled up
  • Method Details

    • addFile

      public void addFile(String zipRelativePath, File file) throws IOException
      Adds the given file to the zip archive using the given relative path.
      Parameters:
      zipRelativePath - the path of the file relative to the zip root
      file - the file to be added
      Throws:
      IOException - Exceptions from the underlying package framework are bubbled up
    • addDirectory

      public void addDirectory(String zipRelativePath) throws IOException
      Adds a directory entry with the given relative path to the zip.
      Parameters:
      zipRelativePath - the path of the directory relative to the zip root
      Throws:
      IOException - Exceptions from the underlying package framework are bubbled up
    • addStringAsFile

      public void addStringAsFile(String zipRelativePath, String plaintextContent) throws IOException
      Adds the given plaintextContent as a file to the zip archive using the given relative path using the default encoding (UTF-8).
      Parameters:
      zipRelativePath - the path of the file relative to the zip root
      plaintextContent - text content to be added as a file
      Throws:
      IOException - Exceptions from the underlying package framework are bubbled up
    • addStringAsFile

      public void addStringAsFile(String zipRelativePath, String plaintextContent, String encoding) throws IOException
      Adds the given plaintextContent as a file to the zip archive using the given relative path using the given encoding.
      Parameters:
      zipRelativePath - the path of the file relative to the zip root
      plaintextContent - text content to be added as a file
      encoding - the encoding to be used
      Throws:
      IOException - Exceptions from the underlying package framework are bubbled up
    • finish

      public void finish()
      Finalizes the zip file and writes it to the disk. It may be wise to call this method in a finally block to avoid dangling file streams.