Package org.zeroturnaround.zip
Class Zips
- java.lang.Object
-
- org.zeroturnaround.zip.Zips
-
public class Zips extends java.lang.ObjectFluent api for zip handling.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classZips.CopyingCallbackprivate static classZips.UnpackingCallback
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<ZipEntrySource>changedEntriesListprivate java.nio.charset.CharsetcharsetCharset to use for entry namesprivate java.io.FiledestOptional destination archive, if null, src will be overwrittenprivate NameMappernameMapperprivate booleanpreserveTimestampsFlag to carry timestamps of entries on.private java.util.Set<java.lang.String>removedEntriesSetprivate java.io.FilesrcSource archive.private java.util.List<ZipEntryTransformerEntry>transformersListprivate booleanunpackedResultFlag to show that we want the final result to be unpacked
-
Constructor Summary
Constructors Modifier Constructor Description privateZips(java.io.File src)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ZipsaddEntries(ZipEntrySource[] entries)Specifies entries to add or change to the output when this Zips executes.ZipsaddEntry(ZipEntrySource entry)Specifies an entry to add or change to the output when this Zips executes.ZipsaddFile(java.io.File file)Adds a file entry.ZipsaddFile(java.io.File file, boolean preserveRoot)Adds a file entry.ZipsaddFile(java.io.File file, boolean preserveRoot, java.io.FileFilter filter)Adds a file entry.ZipsaddFile(java.io.File file, java.io.FileFilter filter)Adds a file entry.ZipsaddTransformer(java.lang.String path, ZipEntryTransformer transformer)Registers a transformer for a given entry.Zipscharset(java.nio.charset.Charset charset)Specifies charset for this Zips executionbooleancontainsEntry(java.lang.String name)Alias to ZipUtil.containsEntry()static Zipscreate()Static factory method to obtain an instance of Zips without source file.Zipsdestination(java.io.File destination)Specifies destination file for this Zips execution, if destination is null (default value), then source file will be overwritten.static Zipsget(java.io.File src)Static factory method to obtain an instance of Zips.private java.io.FilegetDestinationFile()byte[]getEntry(java.lang.String name)Alias to ZipUtil.getEntry()private java.lang.StringgetRelativePath(java.io.File parent, java.io.File file)private java.util.zip.ZipFilegetZipFile()Creates a ZipFile from src and charset of this object.private voidhandleInPlaceActions(java.io.File result)if we are doing something in place, move result file into src.private booleanisEntryInDir(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 booleanisInPlace()private booleanisUnpack()voiditerate(ZipEntryCallback zipEntryCallback)Reads the source ZIP file and executes the given callback for each entry.voiditerate(ZipInfoCallback callback)Scans the source ZIP file and executes the given callback for each entry.private voiditerateChangedAndAdded(ZipEntryOrInfoAdapter zipEntryCallback)Iterate through ZipEntrySources for added or changed entries with a given callbackprivate voiditerateExistingExceptRemoved(ZipEntryOrInfoAdapter zipEntryCallback)Iterate through source for not removed entries with a given callbackZipsnameMapper(NameMapper nameMapper)ZipspreserveTimestamps()Enables timestamp preserving for this Zips executionvoidprocess()Iterates through source Zip entries removing or changing them according to set parameters.private voidprocessAllEntries(ZipEntryOrInfoAdapter zipEntryAdapter)ZipsremoveEntries(java.lang.String[] entries)Specifies entries to remove to the output when this Zips executes.ZipsremoveEntry(java.lang.String entry)Specifies an entry to remove to the output when this Zips executes.ZipssetPreserveTimestamps(boolean preserve)Specifies timestamp preserving for this Zips executionZipsunpack()
-
-
-
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.IOExceptionif 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.IOExceptionCreates 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)
-
-