com.xiaoleilu.hutool.util.ZipUtil类的使用及代码示例

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

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

ZipUtil介绍

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

代码示例

代码示例来源:origin: com.xiaoleilu/hutool-core

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

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * 打包到当前目录,使用默认编码UTF-8
  3. *
  4. * @param srcPath 源文件路径
  5. * @return 打包好的压缩文件
  6. * @throws UtilException IO异常
  7. */
  8. public static File zip(String srcPath) throws UtilException {
  9. return zip(srcPath, DEFAULT_CHARSET);
  10. }

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. validateFiles(zipFile, srcFiles);
  2. out = getZipOutputStream(zipFile, charset);
  3. for (File srcFile : srcFiles) {
  4. zip(srcFile, srcRootDir, out);
  5. out.flush();

代码示例来源:origin: com.xiaoleilu/hutool

  1. /**
  2. * 对文件或文件目录进行压缩<br>
  3. *
  4. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  5. * @param withSrcDir 是否包含被打包目录
  6. * @param srcFiles 要压缩的源文件或目录。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
  7. * @throws IOException
  8. */
  9. public static void zip(File zipFile, boolean withSrcDir, File... srcFiles) throws IOException {
  10. validateFiles(zipFile, srcFiles);
  11. ZipOutputStream out = null;
  12. try {
  13. out = new ZipOutputStream(new CheckedOutputStream(FileUtil.getOutputStream(zipFile), new CRC32()));
  14. for (File srcFile : srcFiles) {
  15. // 如果只是压缩一个文件,则需要截取该文件的父目录
  16. String srcRootDir = srcFile.getCanonicalPath();
  17. if (srcFile.isFile() || withSrcDir) {
  18. srcRootDir = srcFile.getParent();
  19. }
  20. // 调用递归压缩方法进行目录或文件压缩
  21. zip(out, srcRootDir, srcFile);
  22. out.flush();
  23. }
  24. } catch (IOException e) {
  25. throw e;
  26. } finally {
  27. IoUtil.close(out);
  28. }
  29. }

代码示例来源:origin: com.xiaoleilu/hutool

  1. } finally {
  2. IoUtil.close(in);
  3. closeEntry(out);
  4. zip(out, srcRootDir, childFile);

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. if(StrUtil.isNotEmpty(subPath)) {
  2. zipDir(subPath, out);
  3. zip(childFile, srcRootDir, out);
  4. try {
  5. in = FileUtil.getInputStream(file);
  6. zip(in, subPath, out);
  7. } finally {
  8. IoUtil.close(in);

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. out = getZipOutputStream(zipFile, charset);
  2. for(int i = 0; i < paths.length; i++) {
  3. zip(ins[i], paths[i], out);

代码示例来源:origin: com.xiaoleilu/hutool

  1. /**
  2. * Gzip解压缩处理
  3. *
  4. * @param buf 压缩过的字节流
  5. * @param charset 编码
  6. * @return 解压后的字符串
  7. * @throws IOException
  8. */
  9. public static String unGzip(byte[] buf, String charset) throws IOException {
  10. return StrUtil.str(unGzip(buf), charset);
  11. }

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * Gzip压缩处理
  3. *
  4. * @param content 被压缩的字符串
  5. * @param charset 编码
  6. * @return 压缩后的字节流
  7. * @throws UtilException IO异常
  8. */
  9. public static byte[] gzip(String content, String charset) throws UtilException {
  10. return gzip(StrUtil.bytes(content, charset));
  11. }

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * 压缩一个目录到Zip
  3. *
  4. * @param path 压缩的路径
  5. * @param out 压缩文件存储对象
  6. * @throws UtilException IO异常
  7. */
  8. private static void zipDir(String path, ZipOutputStream out) throws UtilException {
  9. path = StrUtil.addSuffixIfNot(path, StrUtil.SLASH);
  10. try {
  11. out.putNextEntry(new ZipEntry(path));
  12. } catch (IOException e) {
  13. throw new UtilException(e);
  14. } finally {
  15. closeEntry(out);
  16. }
  17. }

代码示例来源:origin: com.xiaoleilu/hutool

  1. /**
  2. * 解压
  3. *
  4. * @param zipFile zip文件
  5. * @param outFile 解压到的目录
  6. * @return 解压的目录
  7. * @throws IOException
  8. */
  9. @SuppressWarnings("unchecked")
  10. public static File unzip(File zipFile, File outFile) throws IOException {
  11. final ZipFile zipFileObj = new ZipFile(zipFile);
  12. final Enumeration<ZipEntry> em = (Enumeration<ZipEntry>) zipFileObj.entries();
  13. ZipEntry zipEntry = null;
  14. File outItemFile = null;
  15. while (em.hasMoreElements()) {
  16. zipEntry = em.nextElement();
  17. outItemFile = new File(outFile, zipEntry.getName());
  18. log.debug("UNZIP {}", outItemFile.getPath());
  19. if (zipEntry.isDirectory()) {
  20. outItemFile.mkdirs();
  21. } else {
  22. FileUtil.touch(outItemFile);
  23. copy(zipFileObj, zipEntry, outItemFile);
  24. }
  25. }
  26. IoUtil.close(zipFileObj);
  27. return outFile;
  28. }

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * 获得 {@link ZipOutputStream}
  3. *
  4. * @param zipFile 压缩文件
  5. * @param charset 编码
  6. * @return {@link ZipOutputStream}
  7. */
  8. private static ZipOutputStream getZipOutputStream(File zipFile, Charset charset) {
  9. return getZipOutputStream(FileUtil.getOutputStream(zipFile), charset);
  10. }

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * Gzip解压缩处理
  3. *
  4. * @param buf 压缩过的字节流
  5. * @param charset 编码
  6. * @return 解压后的字符串
  7. * @throws UtilException IO异常
  8. */
  9. public static String unGzip(byte[] buf, String charset) throws UtilException {
  10. return StrUtil.str(unGzip(buf), charset);
  11. }

代码示例来源:origin: com.xiaoleilu/hutool

  1. /**
  2. * Gzip压缩处理
  3. *
  4. * @param content 被压缩的字符串
  5. * @param charset 编码
  6. * @return 压缩后的字节流
  7. * @throws IOException
  8. */
  9. public static byte[] gzip(String content, String charset) throws IOException {
  10. return gzip(StrUtil.bytes(content, charset));
  11. }

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * 递归压缩流中的数据,不关闭输入流
  3. *
  4. * @param in 需要压缩的输入流
  5. * @param path 压缩的路径
  6. * @param out 压缩文件存储对象
  7. * @throws UtilException IO异常
  8. */
  9. private static void zip(InputStream in, String path, ZipOutputStream out) throws UtilException {
  10. if(null == in) {
  11. return;
  12. }
  13. try {
  14. out.putNextEntry(new ZipEntry(path));
  15. IoUtil.copy(in, out);
  16. } catch (IOException e) {
  17. throw new UtilException(e);
  18. } finally {
  19. closeEntry(out);
  20. }
  21. }

代码示例来源:origin: com.xiaoleilu/hutool-core

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

代码示例来源:origin: com.xiaoleilu/hutool-core

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

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * 解压,默认使用UTF-8编码
  3. *
  4. * @param zipFile zip文件
  5. * @param outFile 解压到的目录
  6. * @return 解压的目录
  7. * @throws UtilException IO异常
  8. */
  9. public static File unzip(File zipFile, File outFile) throws UtilException {
  10. return unzip(zipFile, outFile, DEFAULT_CHARSET);
  11. }

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * 对文件或文件目录进行压缩<br>
  3. * 使用默认UTF-8编码
  4. *
  5. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param withSrcDir 是否包含被打包目录
  7. * @param srcFiles 要压缩的源文件或目录。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
  8. * @return 压缩文件
  9. * @throws UtilException IO异常
  10. */
  11. public static File zip(File zipFile, boolean withSrcDir, File... srcFiles) throws UtilException {
  12. return zip(zipFile, DEFAULT_CHARSET, withSrcDir, srcFiles);
  13. }

代码示例来源:origin: com.xiaoleilu/hutool-core

  1. /**
  2. * 解压到文件名相同的目录中,使用UTF-8编码
  3. *
  4. * @param zipFile 压缩文件
  5. * @return 解压的目录
  6. * @throws UtilException IO异常
  7. * @since 3.2.2
  8. */
  9. public static File unzip(File zipFile) throws UtilException {
  10. return unzip(zipFile, DEFAULT_CHARSET);
  11. }

相关文章