Class ZipFile

java.lang.Object
net.lingala.zip4j.ZipFile
All Implemented Interfaces:
Closeable, AutoCloseable

public class ZipFile extends Object implements Closeable
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 Details

    • zipFile

      private File zipFile
    • zipModel

      private ZipModel zipModel
    • isEncrypted

      private boolean isEncrypted
    • progressMonitor

      private ProgressMonitor progressMonitor
    • runInThread

      private boolean runInThread
    • password

      private char[] password
    • headerWriter

      private HeaderWriter headerWriter
    • charset

      private Charset charset
    • threadFactory

      private ThreadFactory threadFactory
    • executorService

      private ExecutorService executorService
    • bufferSize

      private int bufferSize
    • openInputStreams

      private List<InputStream> openInputStreams
    • useUtf8CharsetForPasswords

      private boolean useUtf8CharsetForPasswords
  • Constructor Details

    • ZipFile

      public ZipFile(String 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

      public ZipFile(String zipFile, char[] password)
      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

      public ZipFile(File 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

      public ZipFile(File zipFile, char[] password)
      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
      password - 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 file
      parameters - - zip parameters for this file list
      splitArchive - - if archive has to be split or not
      splitLength - - 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

      public void addFile(String fileToAdd) throws ZipException
      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

      public void addFile(String fileToAdd, ZipParameters zipParameters) throws ZipException
      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 file
      zipParameters - - parameters for the entry to be added to zip
      Throws:
      ZipException
    • addFile

      public void addFile(File fileToAdd) throws ZipException
      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

      public void addFile(File fileToAdd, ZipParameters parameters) throws ZipException
      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 file
      parameters - - zip parameters for this file
      Throws:
      ZipException
    • addFiles

      public void addFiles(List<File> filesToAdd) throws ZipException
      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

      public void addFiles(List<File> filesToAdd, 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:
      filesToAdd -
      parameters -
      Throws:
      ZipException
    • addFolder

      public void addFolder(File folderToAdd) throws ZipException
      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

      public void addFolder(File folderToAdd, ZipParameters zipParameters) 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:
      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

      public void addStream(InputStream inputStream, ZipParameters parameters) throws ZipException
      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

      public void extractAll(String destinationPath) 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:
      destinationPath - path to which the entries of the zip are to be extracted
      Throws:
      ZipException - when an issue occurs during extraction
    • extractAll

      public void extractAll(String destinationPath, UnzipParameters unzipParameters) throws ZipException
      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 extracted
      unzipParameters - parameters to be considered during extraction
      Throws:
      ZipException - when an issue occurs during extraction
    • extractFile

      public void extractFile(FileHeader fileHeader, String destinationPath) 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 extracted
      destinationPath - 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 extracted
      destinationPath - path to which the entries of the zip are to be extracted
      unzipParameters - 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 extracted
      destinationPath - path to which the entries of the zip are to be extracted
      newFileName - if not null, this will be the name given to the file upon extraction
      unzipParameters - 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) 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.

      Throws an exception of type ZipException.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 extracted
      destinationPath - 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 type ZipException.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 extracted
      destinationPath - path to which the entries of the zip are to be extracted
      unzipParameters - 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 type ZipException.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 extracted
      destinationPath - path to which the entries of the zip are to be extracted
      newFileName - 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 extracted
      destinationPath - path to which the entries of the zip are to be extracted
      newFileName - 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 type ZipException.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 extracted
      destinationPath - path to which the entries of the zip are to be extracted
      newFileName - if not null, this will be the name given to the file upon extraction
      unzipParameters - any parameters that have to be considered during extraction
      Throws:
      ZipException - when an issue occurs during extraction
    • getFileHeaders

      public List<FileHeader> getFileHeaders() throws ZipException
      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

      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 ZipException
      Checks to see if the zip file is encrypted
      Returns:
      true if encrypted, false if not
      Throws:
      ZipException
    • isSplitArchive

      public boolean isSplitArchive() throws ZipException
      Checks if the zip file is a split archive
      Returns:
      true if split archive, false if not
      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. If this file header is a directory, all files and directories under this directory will be removed as well.
      Parameters:
      fileHeader -
      Throws:
      ZipException
    • removeFile

      public void removeFile(String fileName) throws ZipException
      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

      public void removeFiles(List<String> fileNames) throws ZipException
      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

      public void renameFile(FileHeader fileHeader, String newFileName) throws ZipException
      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 changed
      newFileName - 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

      public void renameFile(String fileNameToRename, String newFileName) throws ZipException
      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 renamed
      newFileName - 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

      public void renameFiles(Map<String,String> fileNamesMap) throws ZipException
      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

      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
    • getInputStream

      public ZipInputStream getInputStream(FileHeader fileHeader) throws IOException
      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

      public List<File> 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:
      List of Split zip Files
      Throws:
      ZipException
    • close

      public void close() throws IOException
      Closes any open streams that were open by an instance of this class.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      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

      private void readZipInfo() throws ZipException
      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

      private RandomAccessFile initializeRandomAccessFileForHeaderReading() throws IOException
      Throws:
      IOException
    • buildAsyncParameters

      private AsyncZipTask.AsyncTaskParameters buildAsyncParameters()
    • verifyAllSplitFilesOfZipExists

      private boolean verifyAllSplitFilesOfZipExists(List<File> allSplitFiles)
    • getProgressMonitor

      public ProgressMonitor getProgressMonitor()
    • isRunInThread

      public boolean isRunInThread()
    • setRunInThread

      public void setRunInThread(boolean runInThread)
    • getFile

      public File getFile()
    • getCharset

      public Charset 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

      public void setCharset(Charset charset) throws IllegalArgumentException
      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

      public void setThreadFactory(ThreadFactory threadFactory)
    • getExecutorService

      public ExecutorService getExecutorService()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • buildConfig

      private Zip4jConfig buildConfig()
    • isUseUtf8CharsetForPasswords

      public boolean isUseUtf8CharsetForPasswords()
    • setUseUtf8CharsetForPasswords

      public void setUseUtf8CharsetForPasswords(boolean useUtf8CharsetForPasswords)