Package org.zeroturnaround.zip
Class Zips
- java.lang.Object
-
- org.zeroturnaround.zip.Zips
-
public class Zips extends java.lang.Object
Fluent api for zip handling.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Zips.CopyingCallback
private static class
Zips.UnpackingCallback
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<ZipEntrySource>
changedEntries
Listprivate java.nio.charset.Charset
charset
Charset to use for entry namesprivate java.io.File
dest
Optional destination archive, if null, src will be overwrittenprivate NameMapper
nameMapper
private boolean
preserveTimestamps
Flag to carry timestamps of entries on.private java.util.Set<java.lang.String>
removedEntries
Setprivate java.io.File
src
Source archive.private java.util.List<ZipEntryTransformerEntry>
transformers
Listprivate boolean
unpackedResult
Flag to show that we want the final result to be unpacked
-
Constructor Summary
Constructors Modifier Constructor Description private
Zips(java.io.File src)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Zips
addEntries(ZipEntrySource[] entries)
Specifies entries to add or change to the output when this Zips executes.Zips
addEntry(ZipEntrySource entry)
Specifies an entry to add or change to the output when this Zips executes.Zips
addFile(java.io.File file)
Adds a file entry.Zips
addFile(java.io.File file, boolean preserveRoot)
Adds a file entry.Zips
addFile(java.io.File file, boolean preserveRoot, java.io.FileFilter filter)
Adds a file entry.Zips
addFile(java.io.File file, java.io.FileFilter filter)
Adds a file entry.Zips
addTransformer(java.lang.String path, ZipEntryTransformer transformer)
Registers a transformer for a given entry.Zips
charset(java.nio.charset.Charset charset)
Specifies charset for this Zips executionboolean
containsEntry(java.lang.String name)
Alias to ZipUtil.containsEntry()static Zips
create()
Static factory method to obtain an instance of Zips without source file.Zips
destination(java.io.File destination)
Specifies destination file for this Zips execution, if destination is null (default value), then source file will be overwritten.static Zips
get(java.io.File src)
Static factory method to obtain an instance of Zips.private java.io.File
getDestinationFile()
byte[]
getEntry(java.lang.String name)
Alias to ZipUtil.getEntry()private java.lang.String
getRelativePath(java.io.File parent, java.io.File file)
private java.util.zip.ZipFile
getZipFile()
Creates a ZipFile from src and charset of this object.private void
handleInPlaceActions(java.io.File result)
if we are doing something in place, move result file into src.private boolean
isEntryInDir(java.util.Set<java.lang.String> dirNames, java.lang.String entryName)
Checks if entry given by name resides inside of one of the dirs.private boolean
isInPlace()
private boolean
isUnpack()
void
iterate(ZipEntryCallback zipEntryCallback)
Reads the source ZIP file and executes the given callback for each entry.void
iterate(ZipInfoCallback callback)
Scans the source ZIP file and executes the given callback for each entry.private void
iterateChangedAndAdded(ZipEntryOrInfoAdapter zipEntryCallback)
Iterate through ZipEntrySources for added or changed entries with a given callbackprivate void
iterateExistingExceptRemoved(ZipEntryOrInfoAdapter zipEntryCallback)
Iterate through source for not removed entries with a given callbackZips
nameMapper(NameMapper nameMapper)
Zips
preserveTimestamps()
Enables timestamp preserving for this Zips executionvoid
process()
Iterates through source Zip entries removing or changing them according to set parameters.private void
processAllEntries(ZipEntryOrInfoAdapter zipEntryAdapter)
Zips
removeEntries(java.lang.String[] entries)
Specifies entries to remove to the output when this Zips executes.Zips
removeEntry(java.lang.String entry)
Specifies an entry to remove to the output when this Zips executes.Zips
setPreserveTimestamps(boolean preserve)
Specifies timestamp preserving for this Zips executionZips
unpack()
-
-
-
Field Detail
-
src
private final java.io.File src
Source archive.
-
dest
private java.io.File dest
Optional destination archive, if null, src will be overwritten
-
charset
private java.nio.charset.Charset charset
Charset to use for entry names
-
preserveTimestamps
private boolean preserveTimestamps
Flag to carry timestamps of entries on.
-
changedEntries
private java.util.List<ZipEntrySource> changedEntries
List
-
removedEntries
private java.util.Set<java.lang.String> removedEntries
Set
-
transformers
private java.util.List<ZipEntryTransformerEntry> transformers
List
-
nameMapper
private NameMapper nameMapper
-
unpackedResult
private boolean unpackedResult
Flag to show that we want the final result to be unpacked
-
-
Method Detail
-
get
public static Zips get(java.io.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. Seeget(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(java.io.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(java.io.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(java.io.File file, java.io.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(java.io.File file, boolean preserveRoot, java.io.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 java.lang.String getRelativePath(java.io.File parent, java.io.File file)
-
removeEntry
public Zips removeEntry(java.lang.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(java.lang.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(java.nio.charset.Charset charset)
Specifies charset for this Zips execution- Parameters:
charset
- charset to use- Returns:
- this Zips for fluent api
-
destination
public Zips destination(java.io.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(java.lang.String path, ZipEntryTransformer transformer)
Registers a transformer for a given entry.- Parameters:
path
- entry to transformtransformer
- 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 java.io.File getDestinationFile() throws java.io.IOException
- Throws:
java.io.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:
ZipEntryCallback
-
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:
ZipInfoCallback
,iterate(ZipEntryCallback)
-
getEntry
public byte[] getEntry(java.lang.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(java.lang.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(java.io.File result) throws java.io.IOException
if we are doing something in place, move result file into src.- Parameters:
result
- destination zip file- Throws:
java.io.IOException
-
isEntryInDir
private boolean isEntryInDir(java.util.Set<java.lang.String> dirNames, java.lang.String entryName)
Checks if entry given by name resides inside of one of the dirs.- Parameters:
dirNames
- dirsentryName
- entryPath
-
getZipFile
private java.util.zip.ZipFile getZipFile() throws java.io.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:
java.io.IOException
- if ZipFile cannot be constructedjava.lang.IllegalArgumentException
- if accessing constructor ZipFile(File, Charset)
-
-