Package org.zeroturnaround.zip
Class Zips
java.lang.Object
org.zeroturnaround.zip.Zips
Fluent api for zip handling.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
private static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate List
<ZipEntrySource> Listprivate Charset
Charset to use for entry namesprivate File
Optional destination archive, if null, src will be overwrittenprivate NameMapper
private boolean
Flag to carry timestamps of entries on.Setprivate final File
Source archive.private List
<ZipEntryTransformerEntry> Listprivate boolean
Flag to show that we want the final result to be unpacked -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddEntries
(ZipEntrySource[] entries) Specifies entries to add or change to the output when this Zips executes.addEntry
(ZipEntrySource entry) Specifies an entry to add or change to the output when this Zips executes.Adds a file entry.Adds a file entry.addFile
(File file, boolean preserveRoot, FileFilter filter) Adds a file entry.addFile
(File file, FileFilter filter) Adds a file entry.addTransformer
(String path, ZipEntryTransformer transformer) Registers a transformer for a given entry.Specifies charset for this Zips executionboolean
containsEntry
(String name) Alias to ZipUtil.containsEntry()static Zips
create()
Static factory method to obtain an instance of Zips without source file.destination
(File destination) Specifies destination file for this Zips execution, if destination is null (default value), then source file will be overwritten.static Zips
Static factory method to obtain an instance of Zips.private File
byte[]
Alias to ZipUtil.getEntry()private String
getRelativePath
(File parent, File file) private ZipFile
Creates a ZipFile from src and charset of this object.private void
handleInPlaceActions
(File result) if we are doing something in place, move result file into src.private boolean
isEntryInDir
(Set<String> dirNames, String entryName) Checks if entry given by name resides inside of one of the dirs.private boolean
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 callbacknameMapper
(NameMapper nameMapper) 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) removeEntries
(String[] entries) Specifies entries to remove to the output when this Zips executes.removeEntry
(String entry) Specifies an entry to remove to the output when this Zips executes.setPreserveTimestamps
(boolean preserve) Specifies timestamp preserving for this Zips executionunpack()
-
Field Details
-
src
Source archive. -
dest
Optional destination archive, if null, src will be overwritten -
charset
Charset to use for entry names -
preserveTimestamps
private boolean preserveTimestampsFlag to carry timestamps of entries on. -
changedEntries
List -
removedEntries
Set -
transformers
List -
nameMapper
-
unpackedResult
private boolean unpackedResultFlag to show that we want the final result to be unpacked
-
-
Constructor Details
-
Zips
-
-
Method Details
-
get
Static factory method to obtain an instance of Zips.- Parameters:
src
- zip file to process- Returns:
- instance of Zips
-
create
Static factory method to obtain an instance of Zips without source file. Seeget(File src)
.- Returns:
- instance of Zips
-
addEntry
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
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
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
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
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
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
-
removeEntry
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
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
Enables timestamp preserving for this Zips execution- Returns:
- this Zips for fluent api
-
setPreserveTimestamps
Specifies timestamp preserving for this Zips execution- Parameters:
preserve
- flag to preserve timestamps- Returns:
- this Zips for fluent api
-
charset
Specifies charset for this Zips execution- Parameters:
charset
- charset to use- Returns:
- this Zips for fluent api
-
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
- Parameters:
nameMapper
- to use when processing entries- Returns:
- this Zips for fluent api
-
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
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
-
getDestinationFile
- Throws:
IOException
-
iterate
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
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
Alias to ZipUtil.getEntry()- Parameters:
name
- name of the entry to fetch bytes from- Returns:
- byte[] contents of the entry by given name
-
containsEntry
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
Iterate through source for not removed entries with a given callback- Parameters:
zipEntryCallback
- callback to execute on entries or their info.
-
iterateChangedAndAdded
Iterate through ZipEntrySources for added or changed entries with a given callback- Parameters:
zipEntryCallback
- callback to execute on entries or their info
-
handleInPlaceActions
if we are doing something in place, move result file into src.- Parameters:
result
- destination zip file- Throws:
IOException
-
isEntryInDir
Checks if entry given by name resides inside of one of the dirs.- Parameters:
dirNames
- dirsentryName
- entryPath
-
getZipFile
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 constructedIllegalArgumentException
- if accessing constructor ZipFile(File, Charset)
-