cn.hutool.core.util.ZipUtil类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(2803)

本文整理了Java中cn.hutool.core.util.ZipUtil类的一些代码示例,展示了ZipUtil类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipUtil类的具体详情如下:
包路径:cn.hutool.core.util.ZipUtil
类名称:ZipUtil

ZipUtil介绍

[英]压缩工具类
[中]压缩工具类

代码示例

代码示例来源:origin: looly/hutool

/**
 * 解压到文件名相同的目录中,默认编码UTF-8
 * 
 * @param zipFilePath 压缩文件路径
 * @return 解压的目录
 * @throws UtilException IO异常
 */
public static File unzip(String zipFilePath) throws UtilException {
  return unzip(zipFilePath, DEFAULT_CHARSET);
}

代码示例来源:origin: looly/hutool

/**
 * 打包到当前目录,使用默认编码UTF-8
 * 
 * @param srcFile 源文件或目录
 * @return 打包好的压缩文件
 * @throws UtilException IO异常
 */
public static File zip(File srcFile) throws UtilException {
  return zip(srcFile, DEFAULT_CHARSET);
}

代码示例来源:origin: looly/hutool

/**
 * 打成Zlib压缩包
 * 
 * @param in 数据流
 * @param level 压缩级别,0~9
 * @param length 预估大小
 * @return 压缩后的bytes
 * @since 4.1.19
 */
public static byte[] zlib(InputStream in, int level, int length) {
  final ByteArrayOutputStream out = new ByteArrayOutputStream(length);
  deflater(in, out, level);
  return out.toByteArray();
}

代码示例来源:origin: looly/hutool

/**
 * 递归压缩文件夹<br>
 * srcRootDir决定了路径截取的位置,例如:<br>
 * file的路径为d:/a/b/c/d.txt,srcRootDir为d:/a/b,则压缩后的文件与目录为结构为c/d.txt
 * 
 * @param out 压缩文件存储对象
 * @param srcRootDir 被压缩的文件夹根目录
 * @param file 当前递归压缩的文件或目录对象
 * @throws UtilException IO异常
 */
private static void zip(File file, String srcRootDir, ZipOutputStream out) throws UtilException {
  if (file == null) {
    return;
  }
  final String subPath = FileUtil.subPath(srcRootDir, file); // 获取文件相对于压缩文件夹根目录的子路径
  if (file.isDirectory()) {// 如果是目录,则压缩压缩目录中的文件或子目录
    final File[] files = file.listFiles();
    if (ArrayUtil.isEmpty(files) && StrUtil.isNotEmpty(subPath)) {
      // 加入目录,只有空目录时才加入目录,非空时会在创建文件时自动添加父级目录
      addDir(subPath, out);
    }
    // 压缩目录下的子文件或目录
    for (File childFile : files) {
      zip(childFile, srcRootDir, out);
    }
  } else {// 如果是文件或其它符号,则直接压缩该文件
    addFile(file, subPath, out);
  }
}

代码示例来源:origin: looly/hutool

validateFiles(zipFile, srcFiles);
try (ZipOutputStream out = getZipOutputStream(zipFile, charset)) {
  String srcRootDir;
  for (File srcFile : srcFiles) {
    zip(srcFile, srcRootDir, out);
    out.flush();

代码示例来源:origin: looly/hutool

/**
 * Gzip压缩文件
 * 
 * @param in 被压缩的流
 * @return 压缩后的字节流
 * @throws UtilException IO异常
 * @since 4.1.18
 */
public static byte[] gzip(InputStream in) throws UtilException {
  return gzip(in, DEFAULT_BYTE_ARRAY_LENGTH);
}

代码示例来源:origin: looly/hutool

/**
 * 对流中的数据加入到压缩文件<br>
 * 路径列表和流列表长度必须一致
 * 
 * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
 * @param paths 流数据在压缩文件中的路径或文件名
 * @param ins 要压缩的源
 * @param charset 编码
 * @return 压缩文件
 * @throws UtilException IO异常
 * @since 3.0.9
 */
public static File zip(File zipFile, String[] paths, InputStream[] ins, Charset charset) throws UtilException {
  if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
    throw new IllegalArgumentException("Paths or ins is empty !");
  }
  if (paths.length != ins.length) {
    throw new IllegalArgumentException("Paths length is not equals to ins length !");
  }
  ZipOutputStream out = null;
  try {
    out = getZipOutputStream(zipFile, charset);
    for (int i = 0; i < paths.length; i++) {
      addFile(ins[i], paths[i], out);
    }
  } finally {
    IoUtil.close(out);
  }
  return zipFile;
}

代码示例来源:origin: looly/hutool

/**
 * 在压缩包中新建目录
 * 
 * @param path 压缩的路径
 * @param out 压缩文件存储对象
 * @throws UtilException IO异常
 */
private static void addDir(String path, ZipOutputStream out) throws UtilException {
  path = StrUtil.addSuffixIfNot(path, StrUtil.SLASH);
  try {
    out.putNextEntry(new ZipEntry(path));
  } catch (IOException e) {
    throw new UtilException(e);
  } finally {
    closeEntry(out);
  }
}

代码示例来源:origin: looly/hutool

/**
 * 获得 {@link ZipOutputStream}
 * 
 * @param zipFile 压缩文件
 * @param charset 编码
 * @return {@link ZipOutputStream}
 */
private static ZipOutputStream getZipOutputStream(File zipFile, Charset charset) {
  return getZipOutputStream(FileUtil.getOutputStream(zipFile), charset);
}

代码示例来源:origin: looly/hutool

/**
 * 解压缩zlib
 * 
 * @param in 数据流
 * @param length 预估长度
 * @return 解压后的bytes
 * @since 4.1.19
 */
public static byte[] unZlib(InputStream in, int length) {
  final ByteArrayOutputStream out = new ByteArrayOutputStream(length);
  inflater(in, out);
  return out.toByteArray();
}

代码示例来源:origin: looly/hutool

/**
 * 添加文件到压缩包
 * 
 * @param file 需要压缩的文件
 * @param path 在压缩文件中的路径
 * @param out 压缩文件存储对象
 * @throws UtilException IO异常
 * @since 4.0.5
 */
private static void addFile(File file, String path, ZipOutputStream out) throws UtilException {
  BufferedInputStream in = null;
  try {
    in = FileUtil.getInputStream(file);
    addFile(in, path, out);
  } finally {
    IoUtil.close(in);
  }
}

代码示例来源:origin: looly/hutool

} else {
  FileUtil.touch(outItemFile);
  copy(zipFileObj, zipEntry, outItemFile);

代码示例来源:origin: looly/hutool

/**
 * 递归压缩文件夹<br>
 * srcRootDir决定了路径截取的位置,例如:<br>
 * file的路径为d:/a/b/c/d.txt,srcRootDir为d:/a/b,则压缩后的文件与目录为结构为c/d.txt
 * 
 * @param out 压缩文件存储对象
 * @param srcRootDir 被压缩的文件夹根目录
 * @param file 当前递归压缩的文件或目录对象
 * @throws UtilException IO异常
 */
private static void zip(File file, String srcRootDir, ZipOutputStream out) throws UtilException {
  if (file == null) {
    return;
  }
  final String subPath = FileUtil.subPath(srcRootDir, file); // 获取文件相对于压缩文件夹根目录的子路径
  if (file.isDirectory()) {// 如果是目录,则压缩压缩目录中的文件或子目录
    final File[] files = file.listFiles();
    if (ArrayUtil.isEmpty(files) && StrUtil.isNotEmpty(subPath)) {
      // 加入目录,只有空目录时才加入目录,非空时会在创建文件时自动添加父级目录
      addDir(subPath, out);
    }
    // 压缩目录下的子文件或目录
    for (File childFile : files) {
      zip(childFile, srcRootDir, out);
    }
  } else {// 如果是文件或其它符号,则直接压缩该文件
    addFile(file, subPath, out);
  }
}

代码示例来源:origin: looly/hutool

validateFiles(zipFile, srcFiles);
try (ZipOutputStream out = getZipOutputStream(zipFile, charset)) {
  String srcRootDir;
  for (File srcFile : srcFiles) {
    zip(srcFile, srcRootDir, out);
    out.flush();

代码示例来源:origin: looly/hutool

/**
 * Gzip压缩文件
 * 
 * @param in 被压缩的流
 * @return 压缩后的字节流
 * @throws UtilException IO异常
 * @since 4.1.18
 */
public static byte[] gzip(InputStream in) throws UtilException {
  return gzip(in, DEFAULT_BYTE_ARRAY_LENGTH);
}

代码示例来源:origin: looly/hutool

/**
 * 对流中的数据加入到压缩文件<br>
 * 路径列表和流列表长度必须一致
 * 
 * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
 * @param paths 流数据在压缩文件中的路径或文件名
 * @param ins 要压缩的源
 * @param charset 编码
 * @return 压缩文件
 * @throws UtilException IO异常
 * @since 3.0.9
 */
public static File zip(File zipFile, String[] paths, InputStream[] ins, Charset charset) throws UtilException {
  if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
    throw new IllegalArgumentException("Paths or ins is empty !");
  }
  if (paths.length != ins.length) {
    throw new IllegalArgumentException("Paths length is not equals to ins length !");
  }
  ZipOutputStream out = null;
  try {
    out = getZipOutputStream(zipFile, charset);
    for (int i = 0; i < paths.length; i++) {
      addFile(ins[i], paths[i], out);
    }
  } finally {
    IoUtil.close(out);
  }
  return zipFile;
}

代码示例来源:origin: looly/hutool

/**
 * 在压缩包中新建目录
 * 
 * @param path 压缩的路径
 * @param out 压缩文件存储对象
 * @throws UtilException IO异常
 */
private static void addDir(String path, ZipOutputStream out) throws UtilException {
  path = StrUtil.addSuffixIfNot(path, StrUtil.SLASH);
  try {
    out.putNextEntry(new ZipEntry(path));
  } catch (IOException e) {
    throw new UtilException(e);
  } finally {
    closeEntry(out);
  }
}

代码示例来源:origin: looly/hutool

/**
 * 获得 {@link ZipOutputStream}
 * 
 * @param zipFile 压缩文件
 * @param charset 编码
 * @return {@link ZipOutputStream}
 */
private static ZipOutputStream getZipOutputStream(File zipFile, Charset charset) {
  return getZipOutputStream(FileUtil.getOutputStream(zipFile), charset);
}

代码示例来源:origin: looly/hutool

/**
 * 解压缩zlib
 * 
 * @param in 数据流
 * @param length 预估长度
 * @return 解压后的bytes
 * @since 4.1.19
 */
public static byte[] unZlib(InputStream in, int length) {
  final ByteArrayOutputStream out = new ByteArrayOutputStream(length);
  inflater(in, out);
  return out.toByteArray();
}

代码示例来源:origin: looly/hutool

/**
 * 添加文件到压缩包
 * 
 * @param file 需要压缩的文件
 * @param path 在压缩文件中的路径
 * @param out 压缩文件存储对象
 * @throws UtilException IO异常
 * @since 4.0.5
 */
private static void addFile(File file, String path, ZipOutputStream out) throws UtilException {
  BufferedInputStream in = null;
  try {
    in = FileUtil.getInputStream(file);
    addFile(in, path, out);
  } finally {
    IoUtil.close(in);
  }
}

相关文章