在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java zip 工具类

2016-12-20 13:16| 发布者: zhangjf| 查看: 651| 评论: 0

摘要: package com.topsoft.websites.utils; import java.io.*;import java.util.logging.Logger;import java.util.zip.*; /** * Created by sunyameng on 14-3-10. */public class ZipUtil { private final static L ...
  1. package com.topsoft.websites.utils;
  2. import java.io.*;
  3. import java.util.logging.Logger;
  4. import java.util.zip.*;
  5. /**
  6. * Created by sunyameng on 14-3-10.
  7. */
  8. public class ZipUtil {
  9. private final static Logger logger = Logger.getLogger(ZipUtil.class.getName());
  10. private static final int BUFFER = 1024 * 10;
  11. /**
  12. * 将指定目录压缩到和该目录同名的zip文件,自定义压缩路径
  13. *
  14. * @param sourceFilePath 目标文件路径
  15. * @param zipFilePath 指定zip文件路径
  16. * @return
  17. */
  18. public static boolean zip(String sourceFilePath, String zipFilePath,String zipFileName) {
  19. boolean result = false;
  20. File source = new File(sourceFilePath);
  21. if (!source.exists()) {
  22. logger.info(sourceFilePath + " doesn't exist.");
  23. return result;
  24. }
  25. if (!source.isDirectory()) {
  26. logger.info(sourceFilePath + " is not a directory.");
  27. return result;
  28. }
  29. File zipFile = new File(zipFilePath + File.separator + zipFileName + ".zip");
  30. if (zipFile.exists()) {
  31. logger.info(zipFile.getName() + " is already exist.");
  32. return result;
  33. } else {
  34. if (!zipFile.getParentFile().exists()) {
  35. if (!zipFile.getParentFile().mkdirs()) {
  36. logger.info("cann't create file " + zipFileName);
  37. return result;
  38. }
  39. }
  40. }
  41. logger.info("creating zip file...");
  42. FileOutputStream dest = null;
  43. ZipOutputStream out = null;
  44. try {
  45. dest = new FileOutputStream(zipFile);
  46. CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
  47. out = new ZipOutputStream(new BufferedOutputStream(checksum));
  48. out.setMethod(ZipOutputStream.DEFLATED);
  49. compress(source, out, source.getName());
  50. result = true;
  51. } catch (FileNotFoundException e) {
  52. e.printStackTrace();
  53. } finally {
  54. if (out != null) {
  55. try {
  56. out.closeEntry();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. try {
  61. out.close();
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. }
  67. if (result) {
  68. logger.info("done.");
  69. } else {
  70. logger.info("fail.");
  71. }
  72. return result;
  73. }
  74. private static void compress(File file, ZipOutputStream out, String mainFileName) {
  75. int index = file.getAbsolutePath().indexOf(mainFileName);
  76. String entryName = file.getAbsolutePath().substring(index);
  77. //System.out.println(entryName);
  78. if (file.isFile()) {
  79. FileInputStream fi = null;
  80. BufferedInputStream origin = null;
  81. try {
  82. fi = new FileInputStream(file);
  83. origin = new BufferedInputStream(fi, BUFFER);
  84. ZipEntry entry = new ZipEntry(entryName);
  85. out.putNextEntry(entry);
  86. byte[] data = new byte[BUFFER];
  87. int count;
  88. while ((count = origin.read(data, 0, BUFFER)) != -1) {
  89. out.write(data, 0, count);
  90. }
  91. } catch (FileNotFoundException e) {
  92. e.printStackTrace();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. } finally {
  96. if (origin != null) {
  97. try {
  98. origin.close();
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104. } else if (file.isDirectory()) {
  105. try {
  106. out.putNextEntry(new ZipEntry(entryName+File.separator));
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. File[] fs = file.listFiles();
  111. if (fs != null && fs.length > 0) {
  112. for (File f : fs) {
  113. compress(f, out, mainFileName);
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * 将zip文件解压到指定的目录,该zip文件必须是使用该类的zip方法压缩的文件
  120. *
  121. * @param zipFile 要解压的zip文件
  122. * @param destPath 指定解压到的目录
  123. * @return
  124. */
  125. public static boolean unzip(File zipFile, String destPath) {
  126. boolean result = false;
  127. if (!zipFile.exists()) {
  128. logger.info(zipFile.getName() + " doesn't exist.");
  129. return result;
  130. }
  131. File target = new File(destPath);
  132. if (!target.exists()) {
  133. if (!target.mkdirs()) {
  134. logger.info("cann't create file " + target.getName());
  135. return result;
  136. }
  137. }
  138. String mainFileName = zipFile.getName().replace(".zip", "");
  139. File targetFile = new File(destPath + File.separator + mainFileName);
  140. if (targetFile.exists()) {
  141. logger.info(targetFile.getName() + " already exist.");
  142. return result;
  143. }
  144. ZipInputStream zis = null;
  145. logger.info("start unzip file ...");
  146. try {
  147. FileInputStream fis = new FileInputStream(zipFile);
  148. CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
  149. zis = new ZipInputStream(new BufferedInputStream(checksum));
  150. ZipEntry entry;
  151. while ((entry = zis.getNextEntry()) != null) {
  152. int count;
  153. byte data[] = new byte[BUFFER];
  154. String entryName = entry.getName();
  155. //logger.info(entryName);
  156. String newEntryName = destPath + File.separator + entryName;
  157. newEntryName=newEntryName.replaceAll("\\", "/");
  158. File f = new File(newEntryName);
  159. if(newEntryName.endsWith("/")){
  160. if(!f.exists()){
  161. if(!f.mkdirs()) {
  162. throw new RuntimeException("can't create directory " + f.getName());
  163. }
  164. }
  165. }else{
  166. File temp=f.getParentFile();
  167. if (!temp.exists()) {
  168. if (!temp.mkdirs()) {
  169. throw new RuntimeException("create file " + temp.getName() + " fail");
  170. }
  171. }
  172. FileOutputStream fos = new FileOutputStream(f);
  173. BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
  174. while ((count = zis.read(data, 0, BUFFER)) != -1) {
  175. dest.write(data, 0, count);
  176. }
  177. dest.flush();
  178. dest.close();
  179. }
  180. }
  181. result = true;
  182. } catch (FileNotFoundException e) {
  183. e.printStackTrace();
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. } finally {
  187. if (zis != null) {
  188. try {
  189. zis.close();
  190. } catch (IOException e) {
  191. e.printStackTrace();
  192. }
  193. }
  194. }
  195. if (result) {
  196. logger.info("done.");
  197. } else {
  198. logger.info("fail.");
  199. }
  200. return result;
  201. }
  202. public static void main(String[] args) throws IOException {
  203. // String path="D:\temp\B";
  204. // ZipUtil.zip(path,"d:/temp/c","anhuigs123");
  205. String zipfile ="D:\temp\c\B.zip";
  206. File zipFile = new File(zipfile);
  207. String output="D:\temp\c";
  208. ZipUtil.unzip(zipFile, output);
  209. }
  210. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 15:32

Copyright 2015-2025 djqfx

返回顶部