Package net.lingala.zip4j.core
Class ZipFile
- java.lang.Object
-
- net.lingala.zip4j.core.ZipFile
-
public class ZipFile extends Object
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
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFile(File sourceFile, ZipParameters parameters)Adds input source file to the zip file.voidaddFiles(ArrayList sourceFileList, ZipParameters parameters)Adds the list of input files to the zip file.voidaddFolder(File path, ZipParameters parameters)Adds the folder in the given file object to the zip file.voidaddFolder(String path, ZipParameters parameters)Adds the folder in the given path to the zip file.voidaddStream(InputStream inputStream, ZipParameters parameters)Creates a new entry in the zip file and adds the content of the inputstream to the zip file.voidcreateZipFile(File sourceFile, ZipParameters parameters)Creates a zip file and adds the source file to the zip file.voidcreateZipFile(File sourceFile, ZipParameters parameters, boolean splitArchive, long splitLength)Creates a zip file and adds the source file to the zip file.voidcreateZipFile(ArrayList sourceFileList, ZipParameters parameters)Creates a zip file and adds the list of source file(s) to the zip file.voidcreateZipFile(ArrayList sourceFileList, ZipParameters parameters, boolean splitArchive, long splitLength)Creates a zip file and adds the list of source file(s) to the zip file.voidcreateZipFileFromFolder(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.voidcreateZipFileFromFolder(String folderToAdd, ZipParameters parameters, boolean splitArchive, long splitLength)Creates a zip file and adds the files/folders from the specified folder to the zip file.voidextractAll(String destPath)Extracts all the files in the given zip file to the input destination path.voidextractAll(String destPath, UnzipParameters unzipParameters)Extracts all the files in the given zip file to the input destination path.voidextractFile(String fileName, String destPath)Extracts a specific file from the zip file to the destination path.voidextractFile(String fileName, String destPath, UnzipParameters unzipParameters)Extracts a specific file from the zip file to the destination path.voidextractFile(String fileName, String destPath, UnzipParameters unzipParameters, String newFileName)Extracts a specific file from the zip file to the destination path.voidextractFile(FileHeader fileHeader, String destPath)Extracts a specific file from the zip file to the destination path.voidextractFile(FileHeader fileHeader, String destPath, UnzipParameters unzipParameters)Extracts a specific file from the zip file to the destination path.voidextractFile(FileHeader fileHeader, String destPath, UnzipParameters unzipParameters, String newFileName)Extracts a specific file from the zip file to the destination path.StringgetComment()Returns the comment set for the Zip fileStringgetComment(String encoding)Returns the comment set for the Zip file in the input encodingFilegetFile()Returns the File object of the zip fileFileHeadergetFileHeader(String fileName)Returns FileHeader if a file header with the given fileHeader string exists in the zip model: If not returns nullListgetFileHeaders()Returns the list of file headers in the zip file.ZipInputStreamgetInputStream(FileHeader fileHeader)Returns an input stream for reading the contents of the Zip file corresponding to the input FileHeader.ProgressMonitorgetProgressMonitor()ArrayListgetSplitZipFiles()Returns the full file path+names of all split zip files in an ArrayList.booleanisEncrypted()Checks to see if the zip file is encryptedbooleanisRunInThread()booleanisSplitArchive()Checks if the zip file is a split archivebooleanisValidZipFile()Checks to see if the input zip file is a valid zip file.voidmergeSplitFiles(File outputZipFile)Merges split zip files into a single zip file without the need to extract the files in the archivevoidremoveFile(String fileName)Removes the file provided in the input paramters from the zip file.voidremoveFile(FileHeader fileHeader)Removes the file provided in the input file header from the zip file.voidsetComment(String comment)Sets comment for the Zip filevoidsetFileNameCharset(String charsetName)Zip4j will encode all the file names with the input charset.voidsetPassword(char[] password)Sets the password for the zip filevoidsetPassword(String password)Sets the password for the zip file.
Note: For security reasons, usage of this method is discouraged.voidsetRunInThread(boolean runInThread)
-
-
-
Constructor Detail
-
ZipFile
public ZipFile(String zipFile) throws ZipException
Creates a new Zip File Object with the given zip file path. If the zip file does not exist, it is not created at this point.- Parameters:
zipFile-- Throws:
ZipException
-
ZipFile
public ZipFile(File zipFile) throws ZipException
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-- Throws:
ZipException
-
-
Method Detail
-
createZipFile
public void createZipFile(File sourceFile, ZipParameters parameters) throws ZipException
Creates a zip file and adds the source file 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- Parameters:
sourceFile- - File to be added to the zip fileparameters- - parameters to create the zip file- Throws:
ZipException
-
createZipFile
public void createZipFile(File sourceFile, ZipParameters parameters, boolean splitArchive, long splitLength) throws ZipException
Creates a zip file and adds the source file 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:
sourceFile- - File to be added to the zip fileparameters- - parameters to create the zip filesplitArchive- - 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
-
createZipFile
public void createZipFile(ArrayList sourceFileList, ZipParameters parameters) 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- Parameters:
sourceFileList- - File to be added to the zip fileparameters- - parameters to create the zip file- Throws:
ZipException
-
createZipFile
public void createZipFile(ArrayList sourceFileList, 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:
sourceFileList- - 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
-
createZipFileFromFolder
public void createZipFileFromFolder(String 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
-
createZipFileFromFolder
public void createZipFileFromFolder(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
public void addFile(File sourceFile, ZipParameters parameters) throws ZipException
Adds input source file 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:
sourceFile- - File to tbe added to the zip fileparameters- - zip parameters for this file- Throws:
ZipException
-
addFiles
public void addFiles(ArrayList sourceFileList, ZipParameters parameters) throws ZipException
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:
sourceFileList-parameters-- Throws:
ZipException
-
addFolder
public void addFolder(String path, ZipParameters parameters) throws ZipException
Adds the folder in the given path to the zip file. If zip file does not exist, then a new zip file is created. If input folder path 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:
path-parameters-- Throws:
ZipException
-
addFolder
public void addFolder(File path, ZipParameters parameters) throws ZipException
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:
path-parameters-- Throws:
ZipException
-
addStream
public void addStream(InputStream inputStream, ZipParameters parameters) throws ZipException
Creates a new entry in the zip file and adds the content of the inputstream 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
public void extractAll(String destPath) throws ZipException
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:
destPath-- Throws:
ZipException
-
extractAll
public void extractAll(String destPath, UnzipParameters unzipParameters) throws ZipException
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:
destPath-unzipParameters-- Throws:
ZipException
-
extractFile
public void extractFile(FileHeader fileHeader, String destPath) 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.- Parameters:
fileHeader-destPath-- Throws:
ZipException
-
extractFile
public void extractFile(FileHeader fileHeader, String destPath, 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- Parameters:
fileHeader-destPath-unzipParameters-- Throws:
ZipException
-
extractFile
public void extractFile(FileHeader fileHeader, String destPath, UnzipParameters unzipParameters, 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.- Parameters:
fileHeader-destPath-unzipParameters-newFileName-- Throws:
ZipException
-
extractFile
public void extractFile(String fileName, String destPath) 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
Throws an exception if file header could not be found for the given file name or if the destination path is invalid- Parameters:
fileName-destPath-- Throws:
ZipException
-
extractFile
public void extractFile(String fileName, String destPath, 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
Throws an exception if file header could not be found for the given file name or if the destination path is invalid- Parameters:
fileName-destPath-unzipParameters-- Throws:
ZipException
-
extractFile
public void extractFile(String fileName, String destPath, UnzipParameters unzipParameters, 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
Throws an exception if file header could not be found for the given file name or if the destination path is invalid- Parameters:
fileName-destPath-unzipParameters-newFileName-- Throws:
ZipException
-
setPassword
public void setPassword(String password) throws ZipException
Sets the password for the zip file.
Note: For security reasons, usage of this method is discouraged. Use setPassword(char[]) instead. As strings are immutable, they cannot be wiped out from memory explicitly after usage. Therefore, usage of Strings to store passwords is discouraged. More info here: http://docs.oracle.com/javase/1.5.0/docs/guide/security/jce/JCERefGuide.html#PBEEx- Parameters:
password-- Throws:
ZipException
-
setPassword
public void setPassword(char[] password) throws ZipExceptionSets the password for the zip file- Parameters:
password-- Throws:
ZipException
-
getFileHeaders
public List getFileHeaders() throws ZipException
Returns the list of file headers in the zip file. Throws an exception if the zip file does not exist- Returns:
- list of file headers
- Throws:
ZipException
-
getFileHeader
public FileHeader getFileHeader(String fileName) throws ZipException
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
public boolean isEncrypted() throws ZipExceptionChecks to see if the zip file is encrypted- Returns:
- true if encrypted, false if not
- Throws:
ZipException
-
isSplitArchive
public boolean isSplitArchive() throws ZipExceptionChecks if the zip file is a split archive- Returns:
- true if split archive, false if not
- Throws:
ZipException
-
removeFile
public void removeFile(String fileName) throws ZipException
Removes the file provided in the input paramters 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.- Parameters:
fileName-- Throws:
ZipException
-
removeFile
public void removeFile(FileHeader fileHeader) throws ZipException
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.- Parameters:
fileHeader-- Throws:
ZipException
-
mergeSplitFiles
public void mergeSplitFiles(File outputZipFile) throws ZipException
Merges split zip files into a single zip file without the need to extract the files in the archive- Parameters:
outputZipFile-- Throws:
ZipException
-
setComment
public void setComment(String comment) throws ZipException
Sets comment for the Zip file- Parameters:
comment-- Throws:
ZipException
-
getComment
public String getComment() throws ZipException
Returns the comment set for the Zip file- Returns:
- String
- Throws:
ZipException
-
getComment
public String getComment(String encoding) throws ZipException
Returns the comment set for the Zip file in the input encoding- Parameters:
encoding-- Returns:
- String
- Throws:
ZipException
-
setFileNameCharset
public void setFileNameCharset(String charsetName) throws ZipException
Zip4j will encode all the file names with the input charset. This method throws an exception if the Charset is not supported- Parameters:
charsetName-- Throws:
ZipException
-
getInputStream
public ZipInputStream getInputStream(FileHeader fileHeader) throws ZipException
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
-
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- Returns:
- boolean
- Since:
- 1.2.3
-
getSplitZipFiles
public ArrayList getSplitZipFiles() throws ZipException
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:
- ArrayList of Strings
- Throws:
ZipException
-
getProgressMonitor
public ProgressMonitor getProgressMonitor()
-
isRunInThread
public boolean isRunInThread()
-
setRunInThread
public void setRunInThread(boolean runInThread)
-
getFile
public File getFile()
Returns the File object of the zip file- Returns:
- File
-
-