Class ZipUtil

java.lang.Object
ortus.boxlang.runtime.util.ZipUtil

public class ZipUtil extends Object
This class provides zip utilities for the BoxLang runtime
  • 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 use
      source - The absolute file or folder to compress
      destination - The absolute destination of the compressed file, we will add the extension based on the format
      includeBaseFolder - Whether to include the base folder in the compressed file, default is true
      overwrite - Whether to overwrite the destination file if it already exists, default is false
      prefix - 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 extraction
      context - 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 compress
      destination - The absolute destination of the compressed file, we will add the extension based on the format
      includeBaseFolder - Whether to include the base folder in the compressed file
      overwrite - Whether to overwrite the destination file if it already exists, default is false
      prefix - 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 extraction
      recurse - Whether to recurse into subdirectories, default is true
      context - 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 compress
      destination - The absolute destination of the compressed file, we will add the extension based on the format
      includeBaseFolder - Whether to include the base folder in the compressed file
      overwrite - 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 file
      destination - The absolute destination folder to extract the compressed file
      overwrite - Whether to overwrite the destination file if it already exists, default is false
      recurse - Whether to recurse into subdirectories, default is true
      filter - A regex or BoxLang function or Java Predicate to apply as a filter to the extraction
      entryPaths - The specific entry paths to extract from the zip file
      context - 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 return true to extract the entry and false to skip it. - The function should take a single argument which is the entry path - A IBoxContext object is mandatory for BoxLang functions

      A Java Predicate: (entry) -> entry.getName().endsWith(".txt") - The predicate should return true to extract the entry and false to skip it. - The predicate should take a single argument which is the ZipEntry object

      The entryPaths argument is used to extract specific entries from the zip file.

      The recurse argument is used to extract the files recursively. The default is true.

      Parameters:
      source - The absolute path of the compressed file
      destination - The absolute destination folder to extract the compressed file
      overwrite - Whether to overwrite the destination file if it already exists, default is false
      recurse - Whether to recurse into subdirectories, default is true
      filter - A regex or BoxLang function or Java Predicate to apply as a filter to the extraction
      entryPaths - The specific entry paths to extract from the zip file
      Throws:
      BoxRuntimeException - If an error occurs during extraction
    • extractGZip

      public static void extractGZip(String source, String destination, Boolean overwrite)
      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 file
      destination - The absolute destination folder to extract the compressed file
      overwrite - Whether to overwrite the destination file if it already exists, default is false
    • listEntries

      public static 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.

      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 return true to list the entry and false to skip it. - The function should take a single argument which is the entry path - A IBoxContext object is mandatory for BoxLang functions

      A Java Predicate: (entry) -> entry.getName().endsWith(".txt") - The predicate should return true to list the entry and false to skip it. - The predicate should take a single argument which is the ZipEntry object

      The 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 file
      filter - 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 file
      filter - 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 return false to keep the entry and true to delete it. - The function should take a single argument which is the entry path - A IBoxContext object is mandatory for BoxLang functions

      A Java Predicate: (entry) -> entry.getName().endsWith(".txt") - The predicate should return false to keep the entry and true to delete it. - The predicate should take a single argument which is the ZipEntry 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 file
      filter - The filter to apply to the entries: string regex, BoxLang function or Java Predicate to be deleted
      entryPaths - The specific entry paths to delete from the zip file
      context - The BoxLang context if using BoxLang functions
    • readEntry

      public static String readEntry(String source, String entryPath, String charset)
      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 file
      entryPath - The path of the entry to read
      charset - 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

      public static String readEntry(String source, String entryPath)
      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 file
      entryPath - The path of the entry to read
      Returns:
      The content of the entry as a string
    • readBinaryEntry

      public static byte[] readBinaryEntry(String source, String entryPath)
      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 file
      entryPath - The path of the entry to read
      Returns:
      The byte array content of the entry
    • isZipFile

      public static Boolean isZipFile(String filepath)
      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