Class ZipKit

java.lang.Object
org.aoju.bus.core.toolkit.ZipKit

public class ZipKit extends Object
压缩工具类
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • ZipKit

      public ZipKit()
  • Method Details

    • zip

      public static File zip(String srcPath) throws InstrumentException
      打包到当前目录,使用默认编码UTF-8
      Parameters:
      srcPath - 源文件路径
      Returns:
      打包好的压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(String srcPath, Charset charset) throws InstrumentException
      打包到当前目录
      Parameters:
      srcPath - 源文件路径
      charset - 编码
      Returns:
      打包好的压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File srcFile) throws InstrumentException
      打包到当前目录,使用默认编码UTF-8
      Parameters:
      srcFile - 源文件或目录
      Returns:
      打包好的压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File srcFile, Charset charset) throws InstrumentException
      打包到当前目录
      Parameters:
      srcFile - 源文件或目录
      charset - 编码
      Returns:
      打包好的压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(String srcPath, String zipPath) throws InstrumentException
      对文件或文件目录进行压缩 不包含被打包目录
      Parameters:
      srcPath - 要压缩的源文件路径 如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
      zipPath - 压缩文件保存的路径,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      Returns:
      压缩好的Zip文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(String srcPath, String zipPath, boolean withSrcDir) throws InstrumentException
      对文件或文件目录进行压缩
      Parameters:
      srcPath - 要压缩的源文件路径 如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
      zipPath - 压缩文件保存的路径,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      withSrcDir - 是否包含被打包目录
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(String srcPath, String zipPath, Charset charset, boolean withSrcDir) throws InstrumentException
      对文件或文件目录进行压缩
      Parameters:
      srcPath - 要压缩的源文件路径 如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
      zipPath - 压缩文件保存的路径,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      charset - 编码
      withSrcDir - 是否包含被打包目录
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, boolean withSrcDir, File... srcFiles) throws InstrumentException
      对文件或文件目录进行压缩 使用默认UTF-8编码
      Parameters:
      zipFile - 生成的Zip文件,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      withSrcDir - 是否包含被打包目录,只针对压缩目录有效 若为false,则只压缩目录下的文件或目录,为true则将本目录也压缩
      srcFiles - 要压缩的源文件或目录
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, Charset charset, boolean withSrcDir, File... srcFiles) throws InstrumentException
      对文件或文件目录进行压缩
      Parameters:
      zipFile - 生成的Zip文件,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      charset - 编码
      withSrcDir - 是否包含被打包目录,只针对压缩目录有效 若为false,则只压缩目录下的文件或目录,为true则将本目录也压缩
      srcFiles - 要压缩的源文件或目录 如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, Charset charset, boolean withSrcDir, FileFilter filter, File... srcFiles) throws InstrumentException
      对文件或文件目录进行压缩
      Parameters:
      zipFile - 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
      charset - 编码
      withSrcDir - 是否包含被打包目录,只针对压缩目录有效。若为false,则只压缩目录下的文件或目录,为true则将本目录也压缩
      filter - 文件过滤器,通过实现此接口,自定义要过滤的文件(过滤掉哪些文件或文件夹不加入压缩)
      srcFiles - 要压缩的源文件或目录。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static void zip(OutputStream out, Charset charset, boolean withSrcDir, FileFilter filter, File... srcFiles)
      对文件或文件目录进行压缩
      Parameters:
      out - 生成的Zip到的目标流,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      charset - 编码
      withSrcDir - 是否包含被打包目录,只针对压缩目录有效。若为false,则只压缩目录下的文件或目录,为true则将本目录也压缩
      filter - 文件过滤器,通过实现此接口,自定义要过滤的文件(过滤掉哪些文件或文件夹不加入压缩)
      srcFiles - 要压缩的源文件或目录。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
    • zip

      public static void zip(ZipOutputStream zipOutputStream, boolean withSrcDir, FileFilter filter, File... srcFiles)
      对文件或文件目录进行压缩
      Parameters:
      zipOutputStream - 生成的Zip到的目标流,自动关闭此流
      withSrcDir - 是否包含被打包目录,只针对压缩目录有效。若为false,则只压缩目录下的文件或目录,为true则将本目录也压缩
      filter - 文件过滤器,通过实现此接口,自定义要过滤的文件(过滤掉哪些文件或文件夹不加入压缩)
      srcFiles - 要压缩的源文件或目录。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
    • zip

      public static File zip(File zipFile, String path, String data) throws InstrumentException
      对流中的数据加入到压缩文件,使用默认UTF-8编码
      Parameters:
      zipFile - 生成的Zip文件,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      path - 流数据在压缩文件中的路径或文件名
      data - 要压缩的数据
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, String path, String data, Charset charset) throws InstrumentException
      对流中的数据加入到压缩文件
      Parameters:
      zipFile - 生成的Zip文件,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      path - 流数据在压缩文件中的路径或文件名
      data - 要压缩的数据
      charset - 编码
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, String path, InputStream in) throws InstrumentException
      对流中的数据加入到压缩文件 使用默认编码UTF-8
      Parameters:
      zipFile - 生成的Zip文件,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      path - 流数据在压缩文件中的路径或文件名
      in - 要压缩的源
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, String path, InputStream in, Charset charset) throws InstrumentException
      对流中的数据加入到压缩文件
      Parameters:
      zipFile - 生成的Zip文件,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      path - 流数据在压缩文件中的路径或文件名
      in - 要压缩的源
      charset - 编码
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, String[] paths, InputStream[] ins) throws InstrumentException
      对流中的数据加入到压缩文件 路径列表和流列表长度必须一致
      Parameters:
      zipFile - 生成的Zip文件,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      paths - 流数据在压缩文件中的路径或文件名
      ins - 要压缩的源,添加完成后自动关闭流
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, String[] paths, InputStream[] ins, Charset charset) throws InstrumentException
      对流中的数据加入到压缩文件 路径列表和流列表长度必须一致
      Parameters:
      zipFile - 生成的Zip文件,包括文件名 注意:zipPath不能是srcPath路径下的子文件夹
      paths - 流数据在压缩文件中的路径或文件名
      ins - 要压缩的源
      charset - 编码
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • zip

      public static void zip(OutputStream out, String[] paths, InputStream[] ins)
      将文件流压缩到目标流中
      Parameters:
      out - 目标流,压缩完成自动关闭
      paths - 流数据在压缩文件中的路径或文件名
      ins - 要压缩的源,添加完成后自动关闭流
    • zip

      public static void zip(ZipOutputStream zipOutputStream, String[] paths, InputStream[] ins) throws InstrumentException
      将文件流压缩到目标流中
      Parameters:
      zipOutputStream - 目标流,压缩完成不关闭
      paths - 流数据在压缩文件中的路径或文件名
      ins - 要压缩的源,添加完成后自动关闭流
      Throws:
      InstrumentException - IO异常
    • zip

      public static File zip(File zipFile, Charset charset, Resource... resources) throws InstrumentException
      对流中的数据加入到压缩文件 路径列表和流列表长度必须一致
      Parameters:
      zipFile - 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
      charset - 编码
      resources - 需要压缩的资源,资源的路径为Resource.getName()
      Returns:
      压缩文件
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(String zipFilePath) throws InstrumentException
      解压到文件名相同的目录中,默认编码UTF-8
      Parameters:
      zipFilePath - 压缩文件路径
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(String zipFilePath, Charset charset) throws InstrumentException
      解压到文件名相同的目录中
      Parameters:
      zipFilePath - 压缩文件路径
      charset - 编码
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(File zipFile) throws InstrumentException
      解压到文件名相同的目录中,使用UTF-8编码
      Parameters:
      zipFile - 压缩文件
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(File zipFile, Charset charset) throws InstrumentException
      解压到文件名相同的目录中
      Parameters:
      zipFile - 压缩文件
      charset - 编码
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(String zipFilePath, String outFileDir) throws InstrumentException
      解压,默认UTF-8编码
      Parameters:
      zipFilePath - 压缩文件的路径
      outFileDir - 解压到的目录
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(String zipFilePath, String outFileDir, Charset charset) throws InstrumentException
      解压
      Parameters:
      zipFilePath - 压缩文件的路径
      outFileDir - 解压到的目录
      charset - 编码
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(File zipFile, File outFile) throws InstrumentException
      解压,默认使用UTF-8编码
      Parameters:
      zipFile - zip文件
      outFile - 解压到的目录
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(File zipFile, File outFile, Charset charset) throws InstrumentException
      解压
      Parameters:
      zipFile - zip文件
      outFile - 解压到的目录
      charset - 编码
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(ZipFile zipFile, File outFile)
      解压
      Parameters:
      zipFile - zip文件,附带编码信息,使用完毕自动关闭
      outFile - 解压到的目录
      Returns:
      解压的目录
      Throws:
      InstrumentException - IO异常
    • unzip

      public static File unzip(InputStream in, File outFile, Charset charset)
      解压 ZIP条目不使用高速缓冲
      Parameters:
      in - zip文件流,使用完毕自动关闭
      outFile - 解压到的目录
      charset - 编码
      Returns:
      解压的目录
    • unzip

      public static File unzip(ZipInputStream zipStream, File outFile)
      解压 ZIP条目不使用高速缓冲
      Parameters:
      zipStream - zip文件流,包含编码信息
      outFile - 解压到的目录
      Returns:
      解压的目录
    • unzipFileBytes

      public static byte[] unzipFileBytes(String zipFilePath, String name)
      从Zip文件中提取指定的文件为bytes
      Parameters:
      zipFilePath - Zip文件
      name - 文件名,如果存在于子文件夹中,此文件名必须包含目录名,例如images/aaa.txt
      Returns:
      文件内容bytes
    • unzipFileBytes

      public static byte[] unzipFileBytes(String zipFilePath, Charset charset, String name)
      从Zip文件中提取指定的文件为bytes
      Parameters:
      zipFilePath - Zip文件
      charset - 编码
      name - 文件名,如果存在于子文件夹中,此文件名必须包含目录名,例如images/aaa.txt
      Returns:
      文件内容bytes
    • unzipFileBytes

      public static byte[] unzipFileBytes(File zipFile, String name)
      从Zip文件中提取指定的文件为bytes
      Parameters:
      zipFile - Zip文件
      name - 文件名,如果存在于子文件夹中,此文件名必须包含目录名,例如images/aaa.txt
      Returns:
      文件内容bytes
    • unzipFileBytes

      public static byte[] unzipFileBytes(File zipFile, Charset charset, String name)
      从Zip文件中提取指定的文件为bytes
      Parameters:
      zipFile - Zip文件
      charset - 编码
      name - 文件名,如果存在于子文件夹中,此文件名必须包含目录名,例如images/aaa.txt
      Returns:
      文件内容bytes
    • gzip

      public static byte[] gzip(String content, String charset) throws InstrumentException
      Gzip压缩处理
      Parameters:
      content - 被压缩的字符串
      charset - 编码
      Returns:
      压缩后的字节流
      Throws:
      InstrumentException - IO异常
    • gzip

      public static byte[] gzip(byte[] buf) throws InstrumentException
      Gzip压缩处理
      Parameters:
      buf - 被压缩的字节流
      Returns:
      压缩后的字节流
      Throws:
      InstrumentException - IO异常
    • gzip

      public static byte[] gzip(File file) throws InstrumentException
      Gzip压缩文件
      Parameters:
      file - 被压缩的文件
      Returns:
      压缩后的字节流
      Throws:
      InstrumentException - IO异常
    • gzip

      public static byte[] gzip(InputStream in) throws InstrumentException
      Gzip压缩文件
      Parameters:
      in - 被压缩的流
      Returns:
      压缩后的字节流
      Throws:
      InstrumentException - IO异常
    • gzip

      public static byte[] gzip(InputStream in, int length) throws InstrumentException
      Gzip压缩文件
      Parameters:
      in - 被压缩的流
      length - 预估长度
      Returns:
      压缩后的字节流
      Throws:
      InstrumentException - IO异常
    • unGzip

      public static String unGzip(byte[] buf, String charset) throws InstrumentException
      Gzip解压缩处理
      Parameters:
      buf - 压缩过的字节流
      charset - 编码
      Returns:
      解压后的字符串
      Throws:
      InstrumentException - IO异常
    • unGzip

      public static byte[] unGzip(byte[] buf) throws InstrumentException
      Gzip解压处理
      Parameters:
      buf - buf
      Returns:
      bytes
      Throws:
      InstrumentException - IO异常
    • unGzip

      public static byte[] unGzip(InputStream in) throws InstrumentException
      Gzip解压处理
      Parameters:
      in - Gzip数据
      Returns:
      解压后的数据
      Throws:
      InstrumentException - IO异常
    • unGzip

      public static byte[] unGzip(InputStream in, int length) throws InstrumentException
      Gzip解压处理
      Parameters:
      in - Gzip数据
      length - 估算长度,如果无法确定请传入DEFAULT_BYTE_ARRAY_LENGTH
      Returns:
      解压后的数据
      Throws:
      InstrumentException - IO异常
    • zlib

      public static byte[] zlib(String content, String charset, int level)
      Zlib压缩处理
      Parameters:
      content - 被压缩的字符串
      charset - 编码
      level - 压缩级别,1~9
      Returns:
      压缩后的字节流
    • zlib

      public static byte[] zlib(File file, int level)
      Zlib压缩文件
      Parameters:
      file - 被压缩的文件
      level - 压缩级别
      Returns:
      压缩后的字节流
    • zlib

      public static byte[] zlib(byte[] buf, int level)
      打成Zlib压缩包
      Parameters:
      buf - 数据
      level - 压缩级别,0~9
      Returns:
      压缩后的bytes
    • zlib

      public static byte[] zlib(InputStream in, int level)
      打成Zlib压缩包
      Parameters:
      in - 数据流
      level - 压缩级别,0~9
      Returns:
      压缩后的bytes
    • zlib

      public static byte[] zlib(InputStream in, int level, int length)
      打成Zlib压缩包
      Parameters:
      in - 数据流
      level - 压缩级别,0~9
      length - 预估大小
      Returns:
      压缩后的bytes
    • unZlib

      public static String unZlib(byte[] buf, String charset)
      Zlib解压缩处理
      Parameters:
      buf - 压缩过的字节流
      charset - 编码
      Returns:
      解压后的字符串
    • unZlib

      public static byte[] unZlib(byte[] buf)
      解压缩zlib
      Parameters:
      buf - 数据
      Returns:
      解压后的bytes
    • unZlib

      public static byte[] unZlib(InputStream in)
      解压缩zlib
      Parameters:
      in - 数据流
      Returns:
      解压后的bytes
    • unZlib

      public static byte[] unZlib(InputStream in, int length)
      解压缩zlib
      Parameters:
      in - 数据流
      length - 预估长度
      Returns:
      解压后的bytes
    • get

      public static InputStream get(File zipFile, Charset charset, String path)
      获取压缩包中的指定文件流
      Parameters:
      zipFile - 压缩文件
      charset - 编码
      path - 需要提取文件的文件名或路径
      Returns:
      压缩文件流,如果未找到返回null
    • get

      public static InputStream get(ZipFile zipFile, String path)
      获取压缩包中的指定文件流
      Parameters:
      zipFile - 压缩文件
      path - 需要提取文件的文件名或路径
      Returns:
      压缩文件流,如果未找到返回null
    • get

      public static InputStream get(ZipFile zipFile, ZipEntry zipEntry)
      获取指定ZipEntry的流,用于读取这个entry的内容
      Parameters:
      zipFile - ZipFile
      zipEntry - ZipEntry
      Returns:
    • get

      public static void get(ZipFile zipFile, Consumer<ZipEntry> consumer)
      读取并处理Zip文件中的每一个ZipEntry
      Parameters:
      zipFile - Zip文件
      consumer - ZipEntry处理器
    • get

      public static void get(ZipInputStream zipStream, Consumer<ZipEntry> consumer)
      读取并处理Zip流中的每一个ZipEntry
      Parameters:
      zipStream - zip文件流,包含编码信息
      consumer - ZipEntry处理器
    • zipFile

      public static ZipFile zipFile(File file, Charset charset)
      将Zip文件转换为ZipFile
      Parameters:
      file - zip文件
      charset - 解析zip文件的编码,null表示Charset.UTF_8
      Returns:
      ZipFile
    • listFileNames

      public static List<String> listFileNames(ZipFile zipFile, String dir)
      获取Zip文件中指定目录下的所有文件,只显示文件,不显示目录 此方法并不会关闭ZipFile
      Parameters:
      zipFile - Zip文件
      dir - 目录前缀(目录前缀不包含开头的/)
      Returns:
      文件列表
    • append

      public static void append(Path zipPath, Path appendFilePath, CopyOption... options) throws IOException
      在zip文件中添加新文件或目录 新文件添加在zip根目录,文件夹包括其本身和内容 如果待添加文件夹是系统根路径(如/或c:/),则只复制文件夹下的内容
      Parameters:
      zipPath - zip文件的Path
      appendFilePath - 待添加文件Path(可以是文件夹)
      options - 拷贝选项,可选是否覆盖等
      Throws:
      IOException - IO异常
    • create

      public static FileSystem create(String path)
      创建 FileSystem
      Parameters:
      path - 文件路径,可以是目录或Zip文件等
      Returns:
      FileSystem
    • createZip

      public static FileSystem createZip(String path)
      创建 Zip的FileSystem,默认UTF-8编码
      Parameters:
      path - 文件路径,可以是目录或Zip文件等
      Returns:
      FileSystem
    • createZip

      public static FileSystem createZip(String path, Charset charset)
      创建 Zip的FileSystem
      Parameters:
      path - 文件路径,可以是目录或Zip文件等
      charset - 编码
      Returns:
      FileSystem