9f3bc83388f054ef8ab996581a473255fecde8a2.svn-base 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package cn.com.goldenwater.dcproj.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.BufferedInputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipOutputStream;
  10. public class FileUtil {
  11. private static Logger log = LoggerFactory.getLogger(FileUtil.class);
  12. /**
  13. * 压缩文件
  14. *
  15. * @param sourceFilePath 源文件路径
  16. * @param zipFilePath 压缩后文件存储路径
  17. * @param zipFilename 压缩文件名
  18. */
  19. public static void compressToZip(String sourceFilePath, String zipFilePath, String zipFilename) {
  20. File sourceFile = new File(sourceFilePath);
  21. File zipPath = new File(zipFilePath);
  22. if (!zipPath.exists()) {
  23. zipPath.mkdirs();
  24. }
  25. File zipFile = new File(zipPath + File.separator + zipFilename);
  26. try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
  27. writeZip(sourceFile, "", zos);
  28. //文件压缩完成后,删除被压缩文件
  29. boolean flag = deleteDir(sourceFile);
  30. log.info("删除被压缩文件[" + sourceFile + "]标志:{}", flag);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. throw new RuntimeException(e.getMessage(), e.getCause());
  34. }
  35. }
  36. /**
  37. * 遍历所有文件,压缩
  38. *
  39. * @param file 源文件目录
  40. * @param parentPath 压缩文件目录
  41. * @param zos 文件流
  42. */
  43. public static void writeZip(File file, String parentPath, ZipOutputStream zos) {
  44. if (file.isDirectory()) {
  45. //目录
  46. parentPath += file.getName() + File.separator;
  47. File[] files = file.listFiles();
  48. if (files != null) {
  49. for (File f : files) {
  50. writeZip(f, parentPath, zos);
  51. }
  52. }
  53. } else {
  54. //文件
  55. try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
  56. //指定zip文件夹
  57. ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
  58. zos.putNextEntry(zipEntry);
  59. int len;
  60. byte[] buffer = new byte[1024 * 10];
  61. while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
  62. zos.write(buffer, 0, len);
  63. zos.flush();
  64. }
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. throw new RuntimeException(e.getMessage(), e.getCause());
  68. }
  69. }
  70. }
  71. /**
  72. * 删除文件夹
  73. *
  74. * @param dir
  75. * @return
  76. */
  77. public static boolean deleteDir(File dir) {
  78. if (dir.isDirectory()) {
  79. String[] children = dir.list();
  80. if (children != null) {
  81. for (String child : children) {
  82. boolean success = deleteDir(new File(dir, child));
  83. if (!success) {
  84. return false;
  85. }
  86. }
  87. }
  88. }
  89. //删除空文件夹
  90. return dir.delete();
  91. }
  92. }