Class ZipUtil
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
compress
(ZipUtil.COMPRESSION_FORMAT format, String source, String destination, Boolean includeBaseFolder, Boolean overwrite, String prefix, Object filter, Boolean recurse, IBoxContext context) A compression method that compresses a file or folder into a zip file according to the specified formatstatic String
compressGzip
(String source, String destination, Boolean includeBaseFolder, Boolean overwrite) Gzip compression method that compresses a file or folder into a gzip file and returns the absolute path of the compressed file Note: Gzip does not support compressing directories, so we will compress the files within the directory to the gzip filestatic String
compressZip
(String source, String destination, Boolean includeBaseFolder, Boolean overwrite, String prefix, Object filter, Boolean recurse, IBoxContext context) Compression method that compresses a file or folder into a zip file and returns the absolute path of the compressed filestatic void
deleteEntries
(String source, Object filter, Array entryPaths, IBoxContext context) Delete entries from a zip file based on a filter which can be:static void
extract
(ZipUtil.COMPRESSION_FORMAT format, String source, String destination, Boolean overwrite, Boolean recurse, Object filter, Array entryPaths, IBoxContext context) Extracts a compressed file to a destination folderstatic void
extractGZip
(String source, String destination, Boolean overwrite) Extracts a gzip file to a destination folder.static void
extractZip
(String source, String destination, Boolean overwrite, Boolean recurse, Object filter, Array entryPaths, IBoxContext context) Extracts a zip file to a destination folder.static Boolean
Verifies if a file is a zip file or notstatic Array
listEntries
(String source, Object filter, Boolean recurse, IBoxContext context) List the entries in a zip file into an array of structures of information about the entries.static Array
listEntriesFlat
(String source, Object filter, Boolean recurse, IBoxContext context) List the entries into a flat array of paths in a zip filestatic byte[]
readBinaryEntry
(String source, String entryPath) This method reads an entry from a zip file and returns the content as a byte arraystatic String
This method reads an entry from a zip file and returns the content as a string using the default charsetstatic String
This method reads an entry from a zip file and returns the content as a string using the specified charset
-
Constructor Details
-
ZipUtil
public ZipUtil()
-
-
Method Details
-
compress
public static String compress(ZipUtil.COMPRESSION_FORMAT format, String source, String destination, Boolean includeBaseFolder, Boolean overwrite, String prefix, Object filter, Boolean recurse, IBoxContext context) A compression method that compresses a file or folder into a zip file according to the specified format- Parameters:
format
- The compression format to usesource
- The absolute file or folder to compressdestination
- The absolute destination of the compressed file, we will add the extension based on the formatincludeBaseFolder
- Whether to include the base folder in the compressed file, default is trueoverwrite
- Whether to overwrite the destination file if it already exists, default is falseprefix
- String added as a prefix to the final archive. The string is the name of a subdirectory in which the entries are added to exclusively. Only works for zip archiving.filter
- A regex or BoxLang function or Java Predicate to apply as a filter to the extractioncontext
- The BoxLang context
-
compressZip
public static String compressZip(String source, String destination, Boolean includeBaseFolder, Boolean overwrite, String prefix, Object filter, Boolean recurse, IBoxContext context) Compression method that compresses a file or folder into a zip file and returns the absolute path of the compressed file- Parameters:
source
- The absolute file or folder to compressdestination
- The absolute destination of the compressed file, we will add the extension based on the formatincludeBaseFolder
- Whether to include the base folder in the compressed fileoverwrite
- Whether to overwrite the destination file if it already exists, default is falseprefix
- String added as a prefix to the final archive. The string is the name of a subdirectory in which the entries are added to exclusively. Only works for zip archiving.filter
- A regex or BoxLang function or Java Predicate to apply as a filter to the extractionrecurse
- Whether to recurse into subdirectories, default is truecontext
- The BoxLang context- Returns:
- The absolute path of the compressed file
-
compressGzip
public static String compressGzip(String source, String destination, Boolean includeBaseFolder, Boolean overwrite) Gzip compression method that compresses a file or folder into a gzip file and returns the absolute path of the compressed file Note: Gzip does not support compressing directories, so we will compress the files within the directory to the gzip file- Parameters:
source
- The absolute file or folder to compressdestination
- The absolute destination of the compressed file, we will add the extension based on the formatincludeBaseFolder
- Whether to include the base folder in the compressed fileoverwrite
- Whether to overwrite the destination file if it already exists, default is false- Returns:
- The absolute path of the compressed file
-
extract
public static void extract(ZipUtil.COMPRESSION_FORMAT format, String source, String destination, Boolean overwrite, Boolean recurse, Object filter, Array entryPaths, IBoxContext context) Extracts a compressed file to a destination folder- Parameters:
format
- The compression format to use: zip, gzip, etc.source
- The absolute path of the compressed filedestination
- The absolute destination folder to extract the compressed fileoverwrite
- Whether to overwrite the destination file if it already exists, default is falserecurse
- Whether to recurse into subdirectories, default is truefilter
- A regex or BoxLang function or Java Predicate to apply as a filter to the extractionentryPaths
- The specific entry paths to extract from the zip filecontext
- The BoxLang context
-
extractZip
public static void extractZip(String source, String destination, Boolean overwrite, Boolean recurse, Object filter, Array entryPaths, IBoxContext context) Extracts a zip file to a destination folder.The
filter
argument is used to filter the files to extract. It can be:A regex string:
".*\\.txt"
A BoxLang function:
(path) => path.endsWith(".txt")
- The function should returntrue
to extract the entry andfalse
to skip it. - The function should take a single argument which is the entry path - A IBoxContext object is mandatory for BoxLang functionsA Java Predicate:
(entry) -> entry.getName().endsWith(".txt")
- The predicate should returntrue
to extract the entry andfalse
to skip it. - The predicate should take a single argument which is theZipEntry
objectThe
entryPaths
argument is used to extract specific entries from the zip file.The
recurse
argument is used to extract the files recursively. The default istrue
.- Parameters:
source
- The absolute path of the compressed filedestination
- The absolute destination folder to extract the compressed fileoverwrite
- Whether to overwrite the destination file if it already exists, default is falserecurse
- Whether to recurse into subdirectories, default is truefilter
- A regex or BoxLang function or Java Predicate to apply as a filter to the extractionentryPaths
- The specific entry paths to extract from the zip file- Throws:
BoxRuntimeException
- If an error occurs during extraction
-
extractGZip
Extracts a gzip file to a destination folder. Note: Gzip does not support compressing directories, so we will compress the files within the directory to the gzip file- Parameters:
source
- The absolute path of the compressed filedestination
- The absolute destination folder to extract the compressed fileoverwrite
- Whether to overwrite the destination file if it already exists, default is false
-
listEntries
List the entries in a zip file into an array of structures of information about the entries.The filter can be a regex string, BoxLang function or Java Predicate.
A regex string:
".*\\.txt"
A BoxLang function:
(path) => path.endsWith(".txt")
- The function should returntrue
to list the entry andfalse
to skip it. - The function should take a single argument which is the entry path - A IBoxContext object is mandatory for BoxLang functionsA Java Predicate:
(entry) -> entry.getName().endsWith(".txt")
- The predicate should returntrue
to list the entry andfalse
to skip it. - The predicate should take a single argument which is theZipEntry
objectThe structure should contain the following: - fullpath: The full path of the entry: e.g. "folder1/folder2/file.txt" - name: The file name of the entry: e.g. "file.txt" - directory: The directory containing the entry: e.g. "folder1/folder2" - size: The size of the entry in bytes - compressedSize: The compressed size of the entry in bytes - type: The type of the entry: file or directory - dateLastModified: The date the entry was last modified - crc: The CRC checksum of the entry - comment: The comment of the entry - isEncrypted: Whether the entry is encrypted - isCompressed: Whether the entry is compressed - isDirectory: Whether the entry is a directory
- Parameters:
source
- The absolute path of the zip filefilter
- A regex or BoxLang function or Java Predicate to apply as a filter to the extraction.recurse
- Whether to recurse into subdirectories, default is true.context
- The BoxLang context- Returns:
- An array of structures containing information about the entries in the zip file
-
listEntriesFlat
public static Array listEntriesFlat(String source, Object filter, Boolean recurse, IBoxContext context) List the entries into a flat array of paths in a zip file- Parameters:
source
- The absolute path of the zip filefilter
- A regex or BoxLang function or Java Predicate to apply as a filter to the extraction.recurse
- Whether to recurse into subdirectories, default is true.context
- The BoxLang context- Returns:
- An array of structures containing information about the entries in the zip file
-
deleteEntries
public static void deleteEntries(String source, Object filter, Array entryPaths, IBoxContext context) Delete entries from a zip file based on a filter which can be:A regex string:
".*\\.txt"
A BoxLang function:
(path) => path.endsWith(".txt")
- The function should returnfalse
to keep the entry andtrue
to delete it. - The function should take a single argument which is the entry path - A IBoxContext object is mandatory for BoxLang functionsA Java Predicate:
(entry) -> entry.getName().endsWith(".txt")
- The predicate should returnfalse
to keep the entry andtrue
to delete it. - The predicate should take a single argument which is theZipEntry
object// String regex filter ZipUtil.deleteEntries( "path/to/zipfile.zip", ".*\\.txt", null ); // BoxLang function filter ZipUtil.deleteEntries( "path/to/zipfile.zip", (path) => path.endsWith(".txt"), context ); // Java Predicate filter ZipUtil.deleteEntries( "path/to/zipfile.zip", (entry) -> entry.getName().endsWith(".txt"), null );
- Parameters:
source
- The absolute path of the zip filefilter
- The filter to apply to the entries: string regex, BoxLang function or Java Predicate to be deletedentryPaths
- The specific entry paths to delete from the zip filecontext
- The BoxLang context if using BoxLang functions
-
readEntry
This method reads an entry from a zip file and returns the content as a string using the specified charset- Parameters:
source
- The absolute path of the zip fileentryPath
- The path of the entry to readcharset
- The charset to use for reading the entry- Returns:
- The content of the entry as a string
- Throws:
BoxRuntimeException
- If the entry is not found in the zip file
-
readEntry
This method reads an entry from a zip file and returns the content as a string using the default charset- Parameters:
source
- The absolute path of the zip fileentryPath
- The path of the entry to read- Returns:
- The content of the entry as a string
-
readBinaryEntry
This method reads an entry from a zip file and returns the content as a byte array- Parameters:
source
- The absolute path of the zip fileentryPath
- The path of the entry to read- Returns:
- The byte array content of the entry
-
isZipFile
Verifies if a file is a zip file or not- Parameters:
filepath
- The file path to verify- Returns:
- True if the file is a zip file, false otherwise
-