DownLoadZip.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.hcks.cmfds.commons.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.zip.ZipOutputStream;
  7. public class DownLoadZip {
  8. // 创建文件夹
  9. public File createDir(String path) {
  10. File dirFile = null;
  11. try {
  12. dirFile = new File(path);
  13. if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
  14. dirFile.mkdirs();
  15. }
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. return dirFile;
  20. }
  21. // 创建文件
  22. public File createFile(String path) {
  23. File file = new File(path);
  24. try {
  25. file.createNewFile();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. return file;
  30. }
  31. // 删除文件
  32. public boolean delFile(String path) throws Exception {
  33. boolean result = false;
  34. File file = new File(path);
  35. if (file.exists() && file.isFile()) {
  36. file.delete();
  37. result = true;
  38. }
  39. return result;
  40. }
  41. // 删除文件及文件夹
  42. public boolean delDir(File folder) {
  43. boolean result = false;
  44. try {
  45. String childs[] = folder.list();
  46. if (childs == null || childs.length <= 0) {
  47. if (folder.delete()) {
  48. result = true;
  49. }
  50. } else {
  51. for (int i = 0; i < childs.length; i++) {
  52. String childName = childs[i];
  53. String childPath = folder.getPath() + File.separator + childName;
  54. File filePath = new File(childPath);
  55. if (filePath.exists() && filePath.isFile()) {
  56. if (filePath.delete()) {
  57. result = true;
  58. } else {
  59. result = false;
  60. break;
  61. }
  62. } else if (filePath.exists() && filePath.isDirectory()) {
  63. if (delDir(filePath)) {
  64. result = true;
  65. } else {
  66. result = false;
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. folder.delete();
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. result = false;
  76. }
  77. return result;
  78. }
  79. /*
  80. * @param zipFilePath
  81. * 打包文件存放路径
  82. * @param inputFolderName
  83. * 需要打包的文件夹
  84. * @throws Exception
  85. */
  86. public void zip(String zipFilePath, String inputFolderName)
  87. throws Exception {
  88. String zipFileName = zipFilePath; // 打包后文件名字
  89. File zipFile = new File(inputFolderName);
  90. zip(zipFileName, zipFile);
  91. }
  92. private void zip(String zipFileName, File inputFolder) throws Exception {
  93. FileOutputStream fileOut = new FileOutputStream(zipFileName);
  94. ZipOutputStream out = new ZipOutputStream(fileOut);
  95. zip(out, inputFolder, "");
  96. out.close();
  97. fileOut.close();
  98. }
  99. private void zip(ZipOutputStream out, File inputFolder, String base)
  100. throws Exception {
  101. if (inputFolder.isDirectory()) {
  102. File[] fl = inputFolder.listFiles();
  103. out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
  104. base = base.length() == 0 ? "" : base + "/";
  105. for (int i = 0; i < fl.length; i++) {
  106. zip(out, fl[i], base + fl[i].getName());
  107. }
  108. } else {
  109. out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
  110. FileInputStream in = new FileInputStream(inputFolder);
  111. int b;
  112. //System.out.println(base);
  113. while ((b = in.read()) != -1) {
  114. out.write(b);
  115. }
  116. in.close();
  117. }
  118. }
  119. }