Package net.lingala.zip4j
Class ZipFile
java.lang.Object
net.lingala.zip4j.ZipFile
- All Implemented Interfaces:
Closeable
,AutoCloseable
Base class to handle zip files. Some of the operations supported
in this class are:
- Create Zip File
- Add files to zip file
- Add folder to zip file
- Extract files from zip files
- Remove files from zip file
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
private Charset
private ExecutorService
private HeaderWriter
private boolean
private List
<InputStream> private char[]
private ProgressMonitor
private boolean
private ThreadFactory
private boolean
private File
private ZipModel
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Zip File Object with the input file.Creates a new Zip File Object with the input file.Creates a new ZipFile instance with the zip file at the location specified in zipFile.Creates a new ZipFile instance with the zip file at the location specified in zipFile. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds input source file to the zip file with default zip parameters.void
addFile
(File fileToAdd, ZipParameters parameters) Adds input source file to the zip file.void
Adds input source file to the zip file with default zip parameters.void
addFile
(String fileToAdd, ZipParameters zipParameters) Adds input source file to the zip file with provided zip parameters.void
Adds the list of input files to the zip file with default zip parameters.void
addFiles
(List<File> filesToAdd, ZipParameters parameters) Adds the list of input files to the zip file.void
Adds the folder in the given file object to the zip file with default zip parameters.void
addFolder
(File folderToAdd, ZipParameters zipParameters) Adds the folder in the given file object to the zip file.private void
addFolder
(File folderToAdd, ZipParameters zipParameters, boolean checkSplitArchive) Internal method to add a folder to the zip file.void
addStream
(InputStream inputStream, ZipParameters parameters) Creates a new entry in the zip file and adds the content of the input stream to the zip file.private AsyncZipTask.AsyncTaskParameters
private Zip4jConfig
void
close()
Closes any open streams that were open by an instance of this class.private void
void
createSplitZipFile
(List<File> filesToAdd, ZipParameters parameters, boolean splitArchive, long splitLength) Creates a zip file and adds the list of source file(s) to the zip file.void
createSplitZipFileFromFolder
(File folderToAdd, ZipParameters parameters, boolean splitArchive, long splitLength) Creates a zip file and adds the files/folders from the specified folder to the zip file.void
extractAll
(String destinationPath) Extracts all the files in the given zip file to the input destination path.void
extractAll
(String destinationPath, UnzipParameters unzipParameters) Extracts all entries in the zip file to the destination path considering the options defined in UnzipParametersvoid
extractFile
(String fileName, String destinationPath) Extracts a specific file from the zip file to the destination path.void
extractFile
(String fileName, String destinationPath, String newFileName) Extracts a specific file from the zip file to the destination path.void
extractFile
(String fileName, String destinationPath, String newFileName, UnzipParameters unzipParameters) Extracts a specific file from the zip file to the destination path.void
extractFile
(String fileName, String destinationPath, UnzipParameters unzipParameters) Extracts a specific file from the zip file to the destination path.void
extractFile
(FileHeader fileHeader, String destinationPath) Extracts a specific file from the zip file to the destination path.void
extractFile
(FileHeader fileHeader, String destinationPath, String newFileName) Extracts a specific file from the zip file to the destination path.void
extractFile
(FileHeader fileHeader, String destinationPath, String newFileName, UnzipParameters unzipParameters) Extracts a specific file from the zip file to the destination path.void
extractFile
(FileHeader fileHeader, String destinationPath, UnzipParameters unzipParameters) Extracts a specific file from the zip file to the destination path.int
Returns the size of the buffer used to read streamsReturns user defined charset that was set by setCharset() method.Returns the comment set for the Zip filegetFile()
getFileHeader
(String fileName) Returns FileHeader if a file header with the given fileHeader string exists in the zip model: If not returns nullReturns the list of file headers in the zip file.getInputStream
(FileHeader fileHeader) Returns an input stream for reading the contents of the Zip file corresponding to the input FileHeader.Returns the full file path+names of all split zip files in an ArrayList.private RandomAccessFile
boolean
Checks to see if the zip file is encryptedboolean
boolean
Checks if the zip file is a split archiveboolean
boolean
Checks to see if the input zip file is a valid zip file.void
mergeSplitFiles
(File outputZipFile) Merges split zip files into a single zip file without the need to extract the files in the archiveprivate void
Reads the zip header information for this zip file.void
removeFile
(String fileName) Removes the file provided in the input parameters from the zip file.void
removeFile
(FileHeader fileHeader) Removes the file provided in the input file header from the zip file.void
removeFiles
(List<String> fileNames) Removes all files from the zip file that match the names in the input list.void
renameFile
(String fileNameToRename, String newFileName) Renames file name of the entry represented by input fileNameToRename.void
renameFile
(FileHeader fileHeader, String newFileName) Renames file name of the entry represented by file header.void
renameFiles
(Map<String, String> fileNamesMap) Renames all the entries in the zip file that match the keys in the map to their corresponding values in the map.void
setBufferSize
(int bufferSize) Sets the size of buffer that should be used when reading streams.void
setCharset
(Charset charset) Sets the charset to be used for encoding file names and commentsvoid
setComment
(String comment) Sets comment for the Zip filevoid
setPassword
(char[] password) Sets a password to be used for the zip file.void
setRunInThread
(boolean runInThread) void
setThreadFactory
(ThreadFactory threadFactory) void
setUseUtf8CharsetForPasswords
(boolean useUtf8CharsetForPasswords) toString()
private boolean
verifyAllSplitFilesOfZipExists
(List<File> allSplitFiles)
-
Field Details
-
zipFile
-
zipModel
-
isEncrypted
private boolean isEncrypted -
progressMonitor
-
runInThread
private boolean runInThread -
password
private char[] password -
headerWriter
-
charset
-
threadFactory
-
executorService
-
bufferSize
private int bufferSize -
openInputStreams
-
useUtf8CharsetForPasswords
private boolean useUtf8CharsetForPasswords
-
-
Constructor Details
-
ZipFile
Creates a new ZipFile instance with the zip file at the location specified in zipFile. This constructor does not yet create a zip file if it does not exist. Creation happens when adding files to this ZipFile instance- Parameters:
zipFile
-
-
ZipFile
Creates a new ZipFile instance with the zip file at the location specified in zipFile. Input password will be used for any zip operations like adding files or extracting files. This constructor does not yet create a zip file if it does not exist. Creation happens when adding files to this ZipFile instance- Parameters:
zipFile
-
-
ZipFile
Creates a new Zip File Object with the input file. If the zip file does not exist, it is not created at this point.- Parameters:
zipFile
- file reference to the zip file- Throws:
IllegalArgumentException
- when zip file parameter is null
-
ZipFile
Creates a new Zip File Object with the input file. If the zip file does not exist, it is not created at this point.- Parameters:
zipFile
- file reference to the zip filepassword
- password to use for the zip file- Throws:
IllegalArgumentException
- when zip file parameter is null
-
-
Method Details
-
createSplitZipFile
public void createSplitZipFile(List<File> filesToAdd, ZipParameters parameters, boolean splitArchive, long splitLength) throws ZipException Creates a zip file and adds the list of source file(s) to the zip file. If the zip file exists then this method throws an exception. Parameters such as compression type, etc can be set in the input parameters. While the method addFile/addFiles also creates the zip file if it does not exist, the main functionality of this method is to create a split zip file. To create a split zip file, set the splitArchive parameter to true with a valid splitLength. Split Length has to be more than 65536 bytes- Parameters:
filesToAdd
- - File to be added to the zip fileparameters
- - zip parameters for this file listsplitArchive
- - if archive has to be split or notsplitLength
- - if archive has to be split, then length in bytes at which it has to be split- Throws:
ZipException
-
createSplitZipFileFromFolder
public void createSplitZipFileFromFolder(File folderToAdd, ZipParameters parameters, boolean splitArchive, long splitLength) throws ZipException Creates a zip file and adds the files/folders from the specified folder to the zip file. This method does the same functionality as in addFolder method except that this method can also create split zip files when adding a folder. To create a split zip file, set the splitArchive parameter to true and specify the splitLength. Split length has to be more than or equal to 65536 bytes. Note that this method throws an exception if the zip file already exists.- Parameters:
folderToAdd
-parameters
-splitArchive
-splitLength
-- Throws:
ZipException
-
addFile
Adds input source file to the zip file with default zip parameters. If zip file does not exist, this method creates a new zip file.- Parameters:
fileToAdd
- - File with path to be added to the zip file- Throws:
ZipException
-
addFile
Adds input source file to the zip file with provided zip parameters. If zip file does not exist, this method creates a new zip file.- Parameters:
fileToAdd
- - File with path to be added to the zip filezipParameters
- - parameters for the entry to be added to zip- Throws:
ZipException
-
addFile
Adds input source file to the zip file with default zip parameters. If zip file does not exist, this method creates a new zip file.- Parameters:
fileToAdd
- - File to be added to the zip file- Throws:
ZipException
-
addFile
Adds input source file to the zip file. If zip file does not exist, this method creates a new zip file. Parameters such as compression type, etc can be set in the input parameters.- Parameters:
fileToAdd
- - File to be added to the zip fileparameters
- - zip parameters for this file- Throws:
ZipException
-
addFiles
Adds the list of input files to the zip file with default zip parameters. If zip file does not exist, this method creates a new zip file.- Parameters:
filesToAdd
-- Throws:
ZipException
-
addFiles
Adds the list of input files to the zip file. If zip file does not exist, then this method creates a new zip file. Parameters such as compression type, etc can be set in the input parameters.- Parameters:
filesToAdd
-parameters
-- Throws:
ZipException
-
addFolder
Adds the folder in the given file object to the zip file with default zip parameters. If zip file does not exist, then a new zip file is created. If input folder is invalid then an exception is thrown.- Parameters:
folderToAdd
-- Throws:
ZipException
-
addFolder
Adds the folder in the given file object to the zip file. If zip file does not exist, then a new zip file is created. If input folder is invalid then an exception is thrown. Zip parameters for the files in the folder to be added can be set in the input parameters- Parameters:
folderToAdd
-zipParameters
-- Throws:
ZipException
-
addFolder
private void addFolder(File folderToAdd, ZipParameters zipParameters, boolean checkSplitArchive) throws ZipException Internal method to add a folder to the zip file.- Parameters:
folderToAdd
-zipParameters
-checkSplitArchive
-- Throws:
ZipException
-
addStream
Creates a new entry in the zip file and adds the content of the input stream to the zip file. ZipParameters.isSourceExternalStream and ZipParameters.fileNameInZip have to be set before in the input parameters. If the file name ends with / or \, this method treats the content as a directory. Setting the flag ProgressMonitor.setRunInThread to true will have no effect for this method and hence this method cannot be used to add content to zip in thread mode- Parameters:
inputStream
-parameters
-- Throws:
ZipException
-
extractAll
Extracts all the files in the given zip file to the input destination path. If zip file does not exist or destination path is invalid then an exception is thrown.- Parameters:
destinationPath
- path to which the entries of the zip are to be extracted- Throws:
ZipException
- when an issue occurs during extraction
-
extractAll
Extracts all entries in the zip file to the destination path considering the options defined in UnzipParameters- Parameters:
destinationPath
- path to which the entries of the zip are to be extractedunzipParameters
- parameters to be considered during extraction- Throws:
ZipException
- when an issue occurs during extraction
-
extractFile
Extracts a specific file from the zip file to the destination path. If destination path is invalid, then this method throws an exception.
If fileHeader is a directory, this method extracts all files under this directory- Parameters:
fileHeader
- file header corresponding to the entry which has to be extracteddestinationPath
- path to which the entries of the zip are to be extracted- Throws:
ZipException
- when an issue occurs during extraction
-
extractFile
public void extractFile(FileHeader fileHeader, String destinationPath, UnzipParameters unzipParameters) throws ZipException Extracts a specific file from the zip file to the destination path. If destination path is invalid, then this method throws an exception.
If fileHeader is a directory, this method extracts all files under this directory- Parameters:
fileHeader
- file header corresponding to the entry which has to be extracteddestinationPath
- path to which the entries of the zip are to be extractedunzipParameters
- any parameters that have to be considered during extraction- Throws:
ZipException
- when an issue occurs during extraction
-
extractFile
public void extractFile(FileHeader fileHeader, String destinationPath, String newFileName, UnzipParameters unzipParameters) throws ZipException Extracts a specific file from the zip file to the destination path. If destination path is invalid, then this method throws an exception.
If newFileName is not null or empty, newly created file name will be replaced by the value in newFileName. If this value is null, then the file name will be the value in FileHeader.getFileName. If file being extract is a directory, the directory name will be replaced with the newFileName
If fileHeader is a directory, this method extracts all files under this directory.
Any parameters that have to be considered during extraction can be passed in through unzipParameters- Parameters:
fileHeader
- file header corresponding to the entry which has to be extracteddestinationPath
- path to which the entries of the zip are to be extractednewFileName
- if not null, this will be the name given to the file upon extractionunzipParameters
- any parameters that have to be considered during extraction- Throws:
ZipException
- when an issue occurs during extraction
-
extractFile
Extracts a specific file from the zip file to the destination path. This method first finds the necessary file header from the input file name.
File name is relative file name in the zip file. For example if a zip file contains a file "a.txt", then to extract this file, input file name has to be "a.txt". Another example is if there is a file "b.txt" in a folder "abc" in the zip file, then the input file name has to be abc/b.txt
If fileHeader is a directory, this method extracts all files under this directory.
Throws an exception of typeZipException.Type.FILE_NOT_FOUND
if file header could not be found for the given file name. Throws an exception if the destination path is invalid.- Parameters:
fileName
- name of the entry which has to be extracteddestinationPath
- path to which the entries of the zip are to be extracted- Throws:
ZipException
- when an issue occurs during extraction
-
extractFile
public void extractFile(String fileName, String destinationPath, UnzipParameters unzipParameters) throws ZipException Extracts a specific file from the zip file to the destination path. This method first finds the necessary file header from the input file name.
File name is relative file name in the zip file. For example if a zip file contains a file "a.txt", then to extract this file, input file name has to be "a.txt". Another example is if there is a file "b.txt" in a folder "abc" in the zip file, then the input file name has to be abc/b.txt
If fileHeader is a directory, this method extracts all files under this directory.
Any parameters that have to be considered during extraction can be passed in through unzipParameters
Throws an exception of typeZipException.Type.FILE_NOT_FOUND
if file header could not be found for the given file name. Throws an exception if the destination path is invalid.- Parameters:
fileName
- name of the entry which has to be extracteddestinationPath
- path to which the entries of the zip are to be extractedunzipParameters
- any parameters that have to be considered during extraction- Throws:
ZipException
- when an issue occurs during extraction
-
extractFile
public void extractFile(String fileName, String destinationPath, String newFileName) throws ZipException Extracts a specific file from the zip file to the destination path. This method first finds the necessary file header from the input file name.
File name is relative file name in the zip file. For example if a zip file contains a file "a.txt", then to extract this file, input file name has to be "a.txt". Another example is if there is a file "b.txt" in a folder "abc" in the zip file, then the input file name has to be abc/b.txt
If newFileName is not null or empty, newly created file name will be replaced by the value in newFileName. If this value is null, then the file name will be the value in FileHeader.getFileName. If file being extract is a directory, the directory name will be replaced with the newFileName
If fileHeader is a directory, this method extracts all files under this directory.
Throws an exception of typeZipException.Type.FILE_NOT_FOUND
if file header could not be found for the given file name. Throws an exception if the destination path is invalid.- Parameters:
fileName
- name of the entry which has to be extracteddestinationPath
- path to which the entries of the zip are to be extractednewFileName
- if not null, this will be the name given to the file upon extraction- Throws:
ZipException
- when an issue occurs during extraction
-
extractFile
public void extractFile(FileHeader fileHeader, String destinationPath, String newFileName) throws ZipException Extracts a specific file from the zip file to the destination path. If destination path is invalid, then this method throws an exception.
If newFileName is not null or empty, newly created file name will be replaced by the value in newFileName. If this value is null, then the file name will be the value in FileHeader.getFileName. If file being extract is a directory, the directory name will be replaced with the newFileName
If fileHeader is a directory, this method extracts all files under this directory.- Parameters:
fileHeader
- file header corresponding to the entry which has to be extracteddestinationPath
- path to which the entries of the zip are to be extractednewFileName
- if not null, this will be the name given to the file upon extraction- Throws:
ZipException
- when an issue occurs during extraction
-
extractFile
public void extractFile(String fileName, String destinationPath, String newFileName, UnzipParameters unzipParameters) throws ZipException Extracts a specific file from the zip file to the destination path. This method first finds the necessary file header from the input file name.
File name is relative file name in the zip file. For example if a zip file contains a file "a.txt", then to extract this file, input file name has to be "a.txt". Another example is if there is a file "b.txt" in a folder "abc" in the zip file, then the input file name has to be abc/b.txt
If newFileName is not null or empty, newly created file name will be replaced by the value in newFileName. If this value is null, then the file name will be the value in FileHeader.getFileName. If file being extract is a directory, the directory name will be replaced with the newFileName
If fileHeader is a directory, this method extracts all files under this directory.
Any parameters that have to be considered during extraction can be passed in through unzipParameters
Throws an exception of typeZipException.Type.FILE_NOT_FOUND
if file header could not be found for the given file name. Throws an exception if the destination path is invalid.- Parameters:
fileName
- name of the entry which has to be extracteddestinationPath
- path to which the entries of the zip are to be extractednewFileName
- if not null, this will be the name given to the file upon extractionunzipParameters
- any parameters that have to be considered during extraction- Throws:
ZipException
- when an issue occurs during extraction
-
getFileHeaders
Returns the list of file headers in the zip file. Returns an empty list if the zip file does not exist.- Returns:
- list of file headers
- Throws:
ZipException
-
getFileHeader
Returns FileHeader if a file header with the given fileHeader string exists in the zip model: If not returns null- Parameters:
fileName
-- Returns:
- FileHeader
- Throws:
ZipException
-
isEncrypted
Checks to see if the zip file is encrypted- Returns:
- true if encrypted, false if not
- Throws:
ZipException
-
isSplitArchive
Checks if the zip file is a split archive- Returns:
- true if split archive, false if not
- Throws:
ZipException
-
removeFile
Removes the file provided in the input file header from the zip file. If zip file is a split zip file, then this method throws an exception as zip specification does not allow for updating split zip archives. If this file header is a directory, all files and directories under this directory will be removed as well.- Parameters:
fileHeader
-- Throws:
ZipException
-
removeFile
Removes the file provided in the input parameters from the zip file. This method first finds the file header and then removes the file. If file does not exist, then this method throws an exception. If zip file is a split zip file, then this method throws an exception as zip specification does not allow for updating split zip archives. If the entry representing this file name is a directory, all files and directories under this directory will be removed as well.- Parameters:
fileName
-- Throws:
ZipException
-
removeFiles
Removes all files from the zip file that match the names in the input list. If any of the file is a directory, all the files and directories under this directory will be removed as well If zip file is a split zip file, then this method throws an exception as zip specification does not allow for updating split zip archives.- Parameters:
fileNames
-- Throws:
ZipException
-
renameFile
Renames file name of the entry represented by file header. If the file name in the input file header does not match any entry in the zip file, the zip file will not be modified. If the file header is a folder in the zip file, all sub-files and sub-folders in the zip file will also be renamed. Zip file format does not allow modifying a split zip file. Therefore if the zip file being dealt with is a split zip file, this method throws an exception- Parameters:
fileHeader
- file header to be changednewFileName
- the file name that has to be changed to- Throws:
ZipException
- if fileHeader is null or newFileName is null or empty or if the zip file is a split file
-
renameFile
Renames file name of the entry represented by input fileNameToRename. If there is no entry in the zip file matching the file name as in fileNameToRename, the zip file will not be modified. If the entry with fileNameToRename is a folder in the zip file, all sub-files and sub-folders in the zip file will also be renamed. For a folder, the fileNameToRename has to end with zip file separator "/". For example, if a folder name "some-folder-name" has to be modified to "new-folder-name", then value of fileNameToRename should be "some-folder-name/". If newFileName does not end with a separator, zip4j will add a separator. Zip file format does not allow modifying a split zip file. Therefore if the zip file being dealt with is a split zip file, this method throws an exception- Parameters:
fileNameToRename
- file name in the zip that has to be renamednewFileName
- the file name that has to be changed to- Throws:
ZipException
- if fileNameToRename is empty or newFileName is empty or if the zip file is a split file
-
renameFiles
Renames all the entries in the zip file that match the keys in the map to their corresponding values in the map. If there are no entries matching any of the keys from the map, the zip file is not modified. If any of the entry in the map represents a folder, all files and folders will be renamed so that their parent represents the renamed folder. Zip file format does not allow modifying a split zip file. Therefore if the zip file being dealt with is a split zip file, this method throws an exception- Parameters:
fileNamesMap
- map of file names that have to be changed with values in the map being the name to be changed to- Throws:
ZipException
- if map is null or if the zip file is a split file
-
mergeSplitFiles
Merges split zip files into a single zip file without the need to extract the files in the archive- Parameters:
outputZipFile
-- Throws:
ZipException
-
setComment
Sets comment for the Zip file- Parameters:
comment
-- Throws:
ZipException
-
getComment
Returns the comment set for the Zip file- Returns:
- String
- Throws:
ZipException
-
getInputStream
Returns an input stream for reading the contents of the Zip file corresponding to the input FileHeader. Throws an exception if the FileHeader does not exist in the ZipFile- Parameters:
fileHeader
-- Returns:
- ZipInputStream
- Throws:
ZipException
IOException
-
isValidZipFile
public boolean isValidZipFile()Checks to see if the input zip file is a valid zip file. This method will try to read zip headers. If headers are read successfully, this method returns true else false. Since v2.7.0: if the zip file is a split zip file, this method also checks to see if all the split files of the zip exists.- Returns:
- boolean - true if a valid zip file, i.e, zip4j is able to read the zip headers, and in case of a split zip file, all split files of the zip exists; false otherwise
- Since:
- 1.2.3
-
getSplitZipFiles
Returns the full file path+names of all split zip files in an ArrayList. For example: If a split zip file(abc.zip) has a 10 split parts this method returns an array list with path + "abc.z01", path + "abc.z02", etc. Returns null if the zip file does not exist- Returns:
- List of Split zip Files
- Throws:
ZipException
-
close
Closes any open streams that were open by an instance of this class.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- when the underlying input stream throws an exception when trying to close it
-
setPassword
public void setPassword(char[] password) Sets a password to be used for the zip file. Will override if a password supplied via ZipFile constructor- Parameters:
password
- - char array of the password to be used
-
getBufferSize
public int getBufferSize()Returns the size of the buffer used to read streams- Returns:
- size of the buffer used to read streams
-
setBufferSize
public void setBufferSize(int bufferSize) Sets the size of buffer that should be used when reading streams. This size cannot be less than the value defined in InternalZipConstants.MIN_BUFF_SIZE- Parameters:
bufferSize
- size of the buffer that should be used when reading streams- Throws:
IllegalArgumentException
- if bufferSize is less than value configured in InternalZipConstants.MIN_BUFF_SIZE
-
readZipInfo
Reads the zip header information for this zip file. If the zip file does not exist, it creates an empty zip model.
Note: This method does not read local file header information- Throws:
ZipException
-
createNewZipModel
private void createNewZipModel() -
initializeRandomAccessFileForHeaderReading
- Throws:
IOException
-
buildAsyncParameters
-
verifyAllSplitFilesOfZipExists
-
getProgressMonitor
-
isRunInThread
public boolean isRunInThread() -
setRunInThread
public void setRunInThread(boolean runInThread) -
getFile
-
getCharset
Returns user defined charset that was set by setCharset() method. If no charset was explicitly defined (by calling setCharset()), this method returns the default charset which zip4j uses, which is utf-8.- Returns:
- user-defined charset or utf-8 if no charset explicitly set
-
setCharset
Sets the charset to be used for encoding file names and comments- Parameters:
charset
- charset to use to encode file names and comments- Throws:
IllegalArgumentException
- if charset is null
-
setThreadFactory
-
getExecutorService
-
toString
-
buildConfig
-
isUseUtf8CharsetForPasswords
public boolean isUseUtf8CharsetForPasswords() -
setUseUtf8CharsetForPasswords
public void setUseUtf8CharsetForPasswords(boolean useUtf8CharsetForPasswords)
-