com.xiaoleilu.hutool.util.ZipUtil.zip()方法的使用及代码示例

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

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

ZipUtil.zip介绍

[英]打包到当前目录,使用默认编码UTF-8
[中]打包到当前目录,使用默认编码UTF-8

代码示例

代码示例来源: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. /**
  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. * 对文件或文件目录进行压缩<br>
  3. *
  4. * @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
  5. * @param zipPath 压缩文件保存的路径,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param withSrcDir 是否包含被打包目录
  7. * @return 压缩文件
  8. * @throws UtilException IO异常
  9. */
  10. public static File zip(String srcPath, String zipPath, boolean withSrcDir) throws UtilException {
  11. return zip(srcPath, zipPath, DEFAULT_CHARSET, withSrcDir);
  12. }

代码示例来源: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

  1. /**
  2. * 对文件或文件目录进行压缩<br>
  3. * 不包含被打包目录
  4. *
  5. * @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
  6. * @param zipPath 压缩文件保存的路径,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  7. * @return 压缩好的Zip文件
  8. * @throws IOException
  9. */
  10. public static File zip(String srcPath, String zipPath) throws IOException {
  11. return zip(srcPath, zipPath, false);
  12. }

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

  1. /**
  2. * 对文件或文件目录进行压缩<br>
  3. * 不包含被打包目录
  4. *
  5. * @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
  6. * @param zipPath 压缩文件保存的路径,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  7. * @return 压缩好的Zip文件
  8. * @throws UtilException IO异常
  9. */
  10. public static File zip(String srcPath, String zipPath) throws UtilException {
  11. return zip(srcPath, zipPath, false);
  12. }

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

  1. /**
  2. * 对流中的数据加入到压缩文件<br>
  3. *
  4. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  5. * @param path 流数据在压缩文件中的路径或文件名
  6. * @param in 要压缩的源
  7. * @param charset 编码
  8. * @return 压缩文件
  9. * @throws UtilException IO异常
  10. * @since 3.2.2
  11. */
  12. public static File zip(File zipFile, String path, InputStream in, Charset charset) throws UtilException {
  13. return zip(zipFile, new String[] {path}, new InputStream[] {in}, charset);
  14. }

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

  1. /**
  2. * 对流中的数据加入到压缩文件,使用默认UTF-8编码
  3. *
  4. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  5. * @param path 流数据在压缩文件中的路径或文件名
  6. * @param data 要压缩的数据
  7. * @return 压缩文件
  8. * @throws UtilException IO异常
  9. * @since 3.0.6
  10. */
  11. public static File zip(File zipFile, String path, String data) throws UtilException {
  12. return zip(zipFile, path, data, DEFAULT_CHARSET);
  13. }

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

  1. /**
  2. * 对流中的数据加入到压缩文件<br>
  3. * 路径列表和流列表长度必须一致
  4. *
  5. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param paths 流数据在压缩文件中的路径或文件名
  7. * @param ins 要压缩的源
  8. * @return 压缩文件
  9. * @throws UtilException IO异常
  10. * @since 3.0.9
  11. */
  12. public static File zip(File zipFile, String[] paths, InputStream[] ins) throws UtilException {
  13. return zip(zipFile, paths, ins, DEFAULT_CHARSET);
  14. }

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

  1. /**
  2. * 对流中的数据加入到压缩文件<br>
  3. * 使用默认编码UTF-8
  4. *
  5. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param path 流数据在压缩文件中的路径或文件名
  7. * @param in 要压缩的源
  8. * @return 压缩文件
  9. * @throws UtilException IO异常
  10. * @since 3.0.6
  11. */
  12. public static File zip(File zipFile, String path, InputStream in) throws UtilException {
  13. return zip(zipFile, path, in, DEFAULT_CHARSET);
  14. }

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

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

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

  1. /**
  2. * 打包到当前目录
  3. *
  4. * @param srcPath 源文件路径
  5. * @return 打包好的压缩文件
  6. * @throws IOException
  7. */
  8. public static File zip(String srcPath) throws IOException {
  9. return zip(FileUtil.file(srcPath));
  10. }

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

  1. /**
  2. * 对流中的数据加入到压缩文件<br>
  3. *
  4. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  5. * @param path 流数据在压缩文件中的路径或文件名
  6. * @param data 要压缩的数据
  7. * @param charset 编码
  8. * @return 压缩文件
  9. * @throws UtilException IO异常
  10. * @since 3.2.2
  11. */
  12. public static File zip(File zipFile, String path, String data, Charset charset) throws UtilException {
  13. return zip(zipFile, path, IoUtil.toStream(data, charset), charset);
  14. }

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

  1. /**
  2. * 打包到当前目录
  3. *
  4. * @param srcFile 源文件或目录
  5. * @return 打包好的压缩文件
  6. * @throws IOException
  7. */
  8. public static File zip(File srcFile) throws IOException {
  9. File zipFile = FileUtil.file(srcFile.getParentFile(), FileUtil.mainName(srcFile) + ".zip");
  10. zip(zipFile, false, srcFile);
  11. return zipFile;
  12. }

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

  1. /**
  2. * 打包到当前目录
  3. *
  4. * @param srcFile 源文件或目录
  5. * @param charset 编码
  6. * @return 打包好的压缩文件
  7. * @throws UtilException IO异常
  8. */
  9. public static File zip(File srcFile, Charset charset) throws UtilException {
  10. File zipFile = FileUtil.file(srcFile.getParentFile(), FileUtil.mainName(srcFile) + ".zip");
  11. zip(zipFile, charset, false, srcFile);
  12. return zipFile;
  13. }

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

  1. /**
  2. * 对文件或文件目录进行压缩<br>
  3. *
  4. * @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
  5. * @param zipPath 压缩文件保存的路径,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param withSrcDir 是否包含被打包目录
  7. * @throws IOException
  8. * @throws Exception
  9. */
  10. public static File zip(String srcPath, String zipPath, boolean withSrcDir) throws IOException {
  11. File srcFile = FileUtil.file(srcPath);
  12. File zipFile = FileUtil.file(zipPath);
  13. zip(zipFile, withSrcDir, srcFile);
  14. return zipFile;
  15. }

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

  1. /**
  2. * 对文件或文件目录进行压缩<br>
  3. *
  4. * @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
  5. * @param zipPath 压缩文件保存的路径,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param charset 编码
  7. * @param withSrcDir 是否包含被打包目录
  8. * @return 压缩文件
  9. * @throws UtilException IO异常
  10. */
  11. public static File zip(String srcPath, String zipPath, Charset charset, boolean withSrcDir) throws UtilException {
  12. File srcFile = FileUtil.file(srcPath);
  13. File zipFile = FileUtil.file(zipPath);
  14. zip(zipFile, charset, withSrcDir, srcFile);
  15. return zipFile;
  16. }

代码示例来源: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-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. zip(out, srcRootDir, childFile);

相关文章