Class Zips

java.lang.Object
org.zeroturnaround.zip.Zips

public class Zips extends Object
Fluent api for zip handling.
  • Field Details

    • src

      private final File src
      Source archive.
    • dest

      private File dest
      Optional destination archive, if null, src will be overwritten
    • charset

      private Charset charset
      Charset to use for entry names
    • preserveTimestamps

      private boolean preserveTimestamps
      Flag to carry timestamps of entries on.
    • changedEntries

      private List<ZipEntrySource> changedEntries
      List
    • removedEntries

      private Set<String> removedEntries
      Set
    • transformers

      private List<ZipEntryTransformerEntry> transformers
      List
    • nameMapper

      private NameMapper nameMapper
    • unpackedResult

      private boolean unpackedResult
      Flag to show that we want the final result to be unpacked
  • Constructor Details

    • Zips

      private Zips(File src)
  • Method Details

    • get

      public static Zips get(File src)
      Static factory method to obtain an instance of Zips.
      Parameters:
      src - zip file to process
      Returns:
      instance of Zips
    • create

      public static Zips create()
      Static factory method to obtain an instance of Zips without source file. See get(File src).
      Returns:
      instance of Zips
    • addEntry

      public Zips addEntry(ZipEntrySource entry)
      Specifies an entry to add or change to the output when this Zips executes. Adding takes precedence over removal of entries.
      Parameters:
      entry - entry to add
      Returns:
      this Zips for fluent api
    • addEntries

      public Zips addEntries(ZipEntrySource[] entries)
      Specifies entries to add or change to the output when this Zips executes. Adding takes precedence over removal of entries.
      Parameters:
      entries - entries to add
      Returns:
      this Zips for fluent api
    • addFile

      public Zips addFile(File file)
      Adds a file entry. If given file is a dir, adds it and all subfiles recursively. Adding takes precedence over removal of entries.
      Parameters:
      file - file to add.
      Returns:
      this Zips for fluent api
    • addFile

      public Zips addFile(File file, boolean preserveRoot)
      Adds a file entry. If given file is a dir, adds it and all subfiles recursively. Adding takes precedence over removal of entries.
      Parameters:
      file - file to add.
      preserveRoot - if file is a directory, true indicates we want to preserve this dir in the zip. otherwise children of the file are added directly under root.
      Returns:
      this Zips for fluent api
    • addFile

      public Zips addFile(File file, FileFilter filter)
      Adds a file entry. If given file is a dir, adds it and all subfiles recursively. Adding takes precedence over removal of entries.
      Parameters:
      file - file to add.
      filter - a filter to accept files for adding, null means all files are accepted
      Returns:
      this Zips for fluent api
    • addFile

      public Zips addFile(File file, boolean preserveRoot, FileFilter filter)
      Adds a file entry. If given file is a dir, adds it and all subfiles recursively. Adding takes precedence over removal of entries.
      Parameters:
      file - file to add.
      preserveRoot - if file is a directory, true indicates we want to preserve this dir in the zip. otherwise children of the file are added directly under root.
      filter - a filter to accept files for adding, null means all files are accepted
      Returns:
      this Zips for fluent api
    • getRelativePath

      private String getRelativePath(File parent, File file)
    • removeEntry

      public Zips removeEntry(String entry)
      Specifies an entry to remove to the output when this Zips executes.
      Parameters:
      entry - path of the entry to remove
      Returns:
      this Zips for fluent api
    • removeEntries

      public Zips removeEntries(String[] entries)
      Specifies entries to remove to the output when this Zips executes.
      Parameters:
      entries - paths of the entry to remove
      Returns:
      this Zips for fluent api
    • preserveTimestamps

      public Zips preserveTimestamps()
      Enables timestamp preserving for this Zips execution
      Returns:
      this Zips for fluent api
    • setPreserveTimestamps

      public Zips setPreserveTimestamps(boolean preserve)
      Specifies timestamp preserving for this Zips execution
      Parameters:
      preserve - flag to preserve timestamps
      Returns:
      this Zips for fluent api
    • charset

      public Zips charset(Charset charset)
      Specifies charset for this Zips execution
      Parameters:
      charset - charset to use
      Returns:
      this Zips for fluent api
    • destination

      public Zips destination(File destination)
      Specifies destination file for this Zips execution, if destination is null (default value), then source file will be overwritten. Temporary file will be used as destination and then written over the source to create an illusion if inplace action.
      Parameters:
      destination - charset to use
      Returns:
      this Zips for fluent api
    • nameMapper

      public Zips nameMapper(NameMapper nameMapper)
      Parameters:
      nameMapper - to use when processing entries
      Returns:
      this Zips for fluent api
    • unpack

      public Zips unpack()
    • isInPlace

      private boolean isInPlace()
      Returns:
      true if destination is not specified.
    • isUnpack

      private boolean isUnpack()
      Returns:
      should the result of the processing be unpacked.
    • addTransformer

      public Zips addTransformer(String path, ZipEntryTransformer transformer)
      Registers a transformer for a given entry.
      Parameters:
      path - entry to transform
      transformer - transformer for the entry
      Returns:
      this Zips for fluent api
    • process

      public void process()
      Iterates through source Zip entries removing or changing them according to set parameters.
    • processAllEntries

      private void processAllEntries(ZipEntryOrInfoAdapter zipEntryAdapter)
    • getDestinationFile

      private File getDestinationFile() throws IOException
      Throws:
      IOException
    • iterate

      public void iterate(ZipEntryCallback zipEntryCallback)
      Reads the source ZIP file and executes the given callback for each entry.

      For each entry the corresponding input stream is also passed to the callback. If you want to stop the loop then throw a ZipBreakException. This method is charset aware and uses Zips.charset.

      Parameters:
      zipEntryCallback - callback to be called for each entry.
      See Also:
    • iterate

      public void iterate(ZipInfoCallback callback)
      Scans the source ZIP file and executes the given callback for each entry.

      Only the meta-data without the actual data is read. If you want to stop the loop then throw a ZipBreakException. This method is charset aware and uses Zips.charset.

      Parameters:
      callback - callback to be called for each entry.
      See Also:
    • getEntry

      public byte[] getEntry(String name)
      Alias to ZipUtil.getEntry()
      Parameters:
      name - name of the entry to fetch bytes from
      Returns:
      byte[] contents of the entry by given name
    • containsEntry

      public boolean containsEntry(String name)
      Alias to ZipUtil.containsEntry()
      Parameters:
      name - entry to check existence of
      Returns:
      true if zip archive we're processing contains entry by given name, false otherwise
    • iterateExistingExceptRemoved

      private void iterateExistingExceptRemoved(ZipEntryOrInfoAdapter zipEntryCallback)
      Iterate through source for not removed entries with a given callback
      Parameters:
      zipEntryCallback - callback to execute on entries or their info.
    • iterateChangedAndAdded

      private void iterateChangedAndAdded(ZipEntryOrInfoAdapter zipEntryCallback)
      Iterate through ZipEntrySources for added or changed entries with a given callback
      Parameters:
      zipEntryCallback - callback to execute on entries or their info
    • handleInPlaceActions

      private void handleInPlaceActions(File result) throws IOException
      if we are doing something in place, move result file into src.
      Parameters:
      result - destination zip file
      Throws:
      IOException
    • isEntryInDir

      private boolean isEntryInDir(Set<String> dirNames, String entryName)
      Checks if entry given by name resides inside of one of the dirs.
      Parameters:
      dirNames - dirs
      entryName - entryPath
    • getZipFile

      private ZipFile getZipFile() throws IOException
      Creates a ZipFile from src and charset of this object. If a constructor with charset is not available, throws an exception.
      Returns:
      ZipFile
      Throws:
      IOException - if ZipFile cannot be constructed
      IllegalArgumentException - if accessing constructor ZipFile(File, Charset)