www.dextsolution.com
DEXTUPLOAD
JK
menu toggleReference > dextuploadjk > engine < CompressUtil

dextuploadjk.engine
Class CompressUtil

Minimum version
1.0.0
Minimum environment
Java 17, Jakarta EE 9+
Description

The CompressUtil class has the function of creating a file by compressing a file or directory.

List<File> files = new ArrayList<File>();
files.add(new File("/src/test/resources/compress/....txt"));
files.add(new File("/src/test/resources/compress/....pdf"));
files.add(new File("/src/test/resources/compress/subA/subB/....txt"));
files.add(new File("/src/test/resources/compress/....jpg"));
files.add(new File("/src/test/resources/compress/....docx"));
		
CompressUtil zipper = new CompressUtil();
File zip = zipper.zip(files, new File("/tmp"), "UTF-8", false, true);

A directory can be specified instead of a file list.

CompressUtil zipper = new CompressUtil();
File zip = zipper.zip(new File("/src/test/resources/compress/"), true, new File("/tmp"), "UTF-8", false, true);

The CompressUtil class does not use the java.util.zip package to compress in zip format, but uses the Apache Commons Compress™ library. Therefore, to use the zip compression function, you must refer to the Apache Commons Compress™ library (commons-compress-{version}.jar).

For more details, refer to https://commons.apache.org/proper/commons-compress/.

Constructor

CompressUtil

  • Creates an object of the CompressUtil class.

  • Signatures

    public CompressUtil()
Methods

zip

  • Compresses the given file or directory and returns it as a single zip file.

  • Signatures

    public File zip(List<File> targets, File tempZipDir, String zipCharsetName, boolean includeHiddenFile, boolean persist)
    public File zip(List<File> targets, List<String> names, List File tempZipDir, String zipCharsetName, boolean includeHiddenFile, boolean persist)
    public File zip(File targetDir, boolean includeTargetDirName, File tempZipDir, String zipCharsetName, boolean includeHiddenFile, boolean persist)
  • Parameters

    Name Type Description
    targets java.util.List<java.io.File> This is an object pointing to a list of files to be compressed.
    targetDir java.io.File This is an object pointing to the directory to be compressed.
    includeTargetDirName boolean

    Set whether to include the root directory when compressing directories.

    If the parameter value is true, compression is performed including the target directory, and if false, child files or child directories excluding the target are compressed.

    tempZipDir java.io.File

    This is an object pointing to the temporary directory where the zip file will be created.

    If null, the operating system determines the temporary directory location.

    zipCharsetName java.lang.String

    Set the character set name to be used in the process of encoding file names containing multiple languages.

    Generally, it is set to "UTF-8", and if null, the default character set is used.

    includeHiddenFile boolean

    When compressing, set whether to compress hidden files as well.

    persist boolean

    Set conditions for maintaining compressed files.

    If the parameter value is set to false, the created compressed file is deleted when the virtual machine is terminated. Conversely, if true, the file is maintained regardless of whether the machine is shut down or not.

    names java.util.List<java.lang.String>

    This is a list of names of files to compress, and must be the same as the size of the targets parameter.

  • Return

    Returns a java.io.File object pointing to the compressed file.