cn.hutool.core.util.ZipUtil.addFile()方法的使用及代码示例

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

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

ZipUtil.addFile介绍

[英]添加文件到压缩包
[中]添加文件到压缩包

代码示例

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

  1. /**
  2. * 添加文件到压缩包
  3. *
  4. * @param file 需要压缩的文件
  5. * @param path 在压缩文件中的路径
  6. * @param out 压缩文件存储对象
  7. * @throws UtilException IO异常
  8. * @since 4.0.5
  9. */
  10. private static void addFile(File file, String path, ZipOutputStream out) throws UtilException {
  11. BufferedInputStream in = null;
  12. try {
  13. in = FileUtil.getInputStream(file);
  14. addFile(in, path, out);
  15. } finally {
  16. IoUtil.close(in);
  17. }
  18. }

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

  1. /**
  2. * 添加文件到压缩包
  3. *
  4. * @param file 需要压缩的文件
  5. * @param path 在压缩文件中的路径
  6. * @param out 压缩文件存储对象
  7. * @throws UtilException IO异常
  8. * @since 4.0.5
  9. */
  10. private static void addFile(File file, String path, ZipOutputStream out) throws UtilException {
  11. BufferedInputStream in = null;
  12. try {
  13. in = FileUtil.getInputStream(file);
  14. addFile(in, path, out);
  15. } finally {
  16. IoUtil.close(in);
  17. }
  18. }

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

  1. /**
  2. * 对流中的数据加入到压缩文件<br>
  3. * 路径列表和流列表长度必须一致
  4. *
  5. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param paths 流数据在压缩文件中的路径或文件名
  7. * @param ins 要压缩的源
  8. * @param charset 编码
  9. * @return 压缩文件
  10. * @throws UtilException IO异常
  11. * @since 3.0.9
  12. */
  13. public static File zip(File zipFile, String[] paths, InputStream[] ins, Charset charset) throws UtilException {
  14. if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
  15. throw new IllegalArgumentException("Paths or ins is empty !");
  16. }
  17. if (paths.length != ins.length) {
  18. throw new IllegalArgumentException("Paths length is not equals to ins length !");
  19. }
  20. ZipOutputStream out = null;
  21. try {
  22. out = getZipOutputStream(zipFile, charset);
  23. for (int i = 0; i < paths.length; i++) {
  24. addFile(ins[i], paths[i], out);
  25. }
  26. } finally {
  27. IoUtil.close(out);
  28. }
  29. return zipFile;
  30. }

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

  1. /**
  2. * 对流中的数据加入到压缩文件<br>
  3. * 路径列表和流列表长度必须一致
  4. *
  5. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param paths 流数据在压缩文件中的路径或文件名
  7. * @param ins 要压缩的源
  8. * @param charset 编码
  9. * @return 压缩文件
  10. * @throws UtilException IO异常
  11. * @since 3.0.9
  12. */
  13. public static File zip(File zipFile, String[] paths, InputStream[] ins, Charset charset) throws UtilException {
  14. if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
  15. throw new IllegalArgumentException("Paths or ins is empty !");
  16. }
  17. if (paths.length != ins.length) {
  18. throw new IllegalArgumentException("Paths length is not equals to ins length !");
  19. }
  20. ZipOutputStream out = null;
  21. try {
  22. out = getZipOutputStream(zipFile, charset);
  23. for (int i = 0; i < paths.length; i++) {
  24. addFile(ins[i], paths[i], out);
  25. }
  26. } finally {
  27. IoUtil.close(out);
  28. }
  29. return zipFile;
  30. }

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

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

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

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

代码示例来源:origin: cn.hutool/hutool-all

  1. /**
  2. * 添加文件到压缩包
  3. *
  4. * @param file 需要压缩的文件
  5. * @param path 在压缩文件中的路径
  6. * @param out 压缩文件存储对象
  7. * @throws UtilException IO异常
  8. * @since 4.0.5
  9. */
  10. private static void addFile(File file, String path, ZipOutputStream out) throws UtilException {
  11. BufferedInputStream in = null;
  12. try {
  13. in = FileUtil.getInputStream(file);
  14. addFile(in, path, out);
  15. } finally {
  16. IoUtil.close(in);
  17. }
  18. }

代码示例来源:origin: cn.hutool/hutool-all

  1. /**
  2. * 对流中的数据加入到压缩文件<br>
  3. * 路径列表和流列表长度必须一致
  4. *
  5. * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
  6. * @param paths 流数据在压缩文件中的路径或文件名
  7. * @param ins 要压缩的源
  8. * @param charset 编码
  9. * @return 压缩文件
  10. * @throws UtilException IO异常
  11. * @since 3.0.9
  12. */
  13. public static File zip(File zipFile, String[] paths, InputStream[] ins, Charset charset) throws UtilException {
  14. if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
  15. throw new IllegalArgumentException("Paths or ins is empty !");
  16. }
  17. if (paths.length != ins.length) {
  18. throw new IllegalArgumentException("Paths length is not equals to ins length !");
  19. }
  20. ZipOutputStream out = null;
  21. try {
  22. out = getZipOutputStream(zipFile, charset);
  23. for (int i = 0; i < paths.length; i++) {
  24. addFile(ins[i], paths[i], out);
  25. }
  26. } finally {
  27. IoUtil.close(out);
  28. }
  29. return zipFile;
  30. }

代码示例来源:origin: cn.hutool/hutool-all

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

相关文章