ZipUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package cn.enilu.flash.utils;
  2. import cn.enilu.flash.bean.entity.system.FileInfo;
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.Enumeration;
  6. import java.util.List;
  7. import java.util.zip.ZipEntry;
  8. import java.util.zip.ZipFile;
  9. import java.util.zip.ZipOutputStream;
  10. /**
  11. * 压缩和解压缩
  12. *
  13. * @author
  14. */
  15. public class ZipUtils {
  16. /**
  17. * 日志记录,为静态变量,防止多次实例化
  18. *
  19. * */
  20. // private static Logger log = Logger.getLogger(ZipUtils.class);
  21. /**
  22. * 每次读入大小
  23. */
  24. private static final int BUFFER = 4096;
  25. /**
  26. * 做成压缩文件
  27. *
  28. * @param baseDir 压缩文件夹
  29. * @param zipFileName 生成压缩文件名
  30. * @return 是否成功
  31. */
  32. public static boolean zipFile(String baseDir, String zipFileName) {
  33. try {
  34. // 获取文件夹下所有文件
  35. List<File> fileList = getSubFiles(new File(baseDir));
  36. ZipOutputStream zos;
  37. zos = new ZipOutputStream(new FileOutputStream(zipFileName));
  38. for (int i = 0; i < fileList.size(); i++) {
  39. File f = fileList.get(i);
  40. zipAddFile(zos, f, getAbsFileName(baseDir, f));
  41. }
  42. zos.close();
  43. return true;
  44. } catch (Exception e) {
  45. // log.error("生成压缩文件错误:错误原因如下:" + e.getMessage());
  46. }
  47. return false;
  48. }
  49. /**
  50. * 做成压缩文件
  51. *
  52. * @param baseDir 压缩文件夹
  53. * @return 是否成功
  54. */
  55. public static String zipFile(String baseDir) {
  56. ZipOutputStream zos = null;
  57. try {
  58. File base = new File(baseDir);
  59. String target = base.getPath() + ".zip";
  60. // 获取文件夹下所有文件
  61. List<File> fileList = getSubFiles(base);
  62. zos = new ZipOutputStream(new FileOutputStream(target));
  63. for (int i = 0; i < fileList.size(); i++) {
  64. File f = fileList.get(i);
  65. zipAddFile(zos, f, getAbsFileName(baseDir, f));
  66. }
  67. return target;
  68. } catch (Exception e) {
  69. // log.error("生成压缩文件错误:错误原因如下:" + e.getMessage());
  70. } finally {
  71. try {
  72. if (zos != null) {
  73. zos.close();
  74. }
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. return null;
  80. }
  81. /**
  82. * 做成压缩文件
  83. *
  84. * @param fileList 文件列表
  85. * @param outputStream 输出流
  86. * @return 是否成功
  87. */
  88. public static boolean zipFiles(List<FileInfo> fileList, OutputStream outputStream) {
  89. ZipOutputStream zos = null;
  90. try {
  91. // 获取文件夹下所有文件
  92. zos = new ZipOutputStream(outputStream);
  93. for (FileInfo fileInfo : fileList) {
  94. File file = new File(fileInfo.getAblatePath());
  95. if (file.exists()) {
  96. zipAddFile(zos, file, fileInfo.getOriginalFileName());
  97. }
  98. }
  99. return true;
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. } finally {
  103. try {
  104. if (zos != null) {
  105. zos.close();
  106. }
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. return false;
  112. }
  113. /**
  114. * 压缩包中添加文件
  115. *
  116. * @param zos 压缩包对象
  117. * @param file 文件对象
  118. * @param filePath 在压缩包中的路径
  119. * @return 是否成功
  120. */
  121. private static boolean zipAddFile(ZipOutputStream zos, File file,
  122. String filePath) throws FileNotFoundException, IOException {
  123. ZipEntry ze = new ZipEntry(filePath);
  124. ze.setSize(file.length());
  125. ze.setTime(file.lastModified());
  126. zos.putNextEntry(ze);
  127. InputStream is = new BufferedInputStream(new FileInputStream(file));
  128. int readLen = 0;
  129. byte[] buf = new byte[BUFFER];
  130. while ((readLen = is.read(buf, 0, BUFFER)) != -1) {
  131. zos.write(buf, 0, readLen);
  132. }
  133. // zos.setEncoding("GBK");
  134. is.close();
  135. return true;
  136. }
  137. /**
  138. * 解压缩文件
  139. *
  140. * @param zipName 包的文件名
  141. * @param targetDirName 需解压到的目录
  142. * @return 是否成功
  143. */
  144. public static boolean upZipFile(String zipName, String targetDirName) {
  145. ZipFile zipFile = null;
  146. try {
  147. zipFile = new ZipFile(zipName);
  148. if (!targetDirName.endsWith(File.separator)) {
  149. targetDirName += File.separator;
  150. }
  151. ZipEntry zn = null;
  152. String entryName = null;
  153. String targetFileName = null;
  154. byte[] buffer = new byte[BUFFER];
  155. int readLen = 0;
  156. // 获取ZIP文件里所有的文件条目的名字
  157. Enumeration<?> entrys = zipFile.entries();
  158. while (entrys.hasMoreElements()) {
  159. zn = (ZipEntry) entrys.nextElement();
  160. entryName = zn.getName();
  161. targetFileName = targetDirName + entryName;
  162. if (zn.isDirectory()) {
  163. // 如果zn是一个目录,则创建目录
  164. new File(targetFileName).mkdirs();
  165. continue;
  166. } else {
  167. // 如果zn是一个文件,则创建父目录
  168. new File(targetFileName).getParentFile().mkdirs();
  169. }
  170. // 创建文件
  171. File targetFile = new File(targetFileName);
  172. FileOutputStream os = new FileOutputStream(targetFile);
  173. // 从ZipFile对象中打开entry的输入流
  174. InputStream is = zipFile.getInputStream(zn);
  175. while ((readLen = is.read(buffer)) != -1) {
  176. os.write(buffer, 0, readLen);
  177. }
  178. // 关闭流
  179. os.close();
  180. is.close();
  181. }
  182. return true;
  183. } catch (Exception e) {
  184. // log.error("解压缩错误:错误原因如下:" + e.getMessage());
  185. } finally {
  186. try {
  187. zipFile.close();
  188. } catch (Exception e) {
  189. }
  190. }
  191. return false;
  192. }
  193. /**
  194. * 生成文件在压缩包中的路径
  195. *
  196. * @param baseDir 压缩包所在文件夹路径
  197. * @param realFile 文件对象
  198. * @return 文件在压缩包中的路径
  199. */
  200. private static String getAbsFileName(String baseDir, File realFile) {
  201. File real = realFile;
  202. File base = new File(baseDir);
  203. String ret = real.getName();
  204. while (true) {
  205. real = real.getParentFile();
  206. if (real == null) {
  207. break;
  208. }
  209. if (real.equals(base)) {
  210. break;
  211. } else {
  212. ret = real.getName() + "/" + ret;
  213. }
  214. }
  215. return ret;
  216. }
  217. /**
  218. * 获取文件夹下所有文件
  219. *
  220. * @param baseDir 文件夹路径
  221. * @return 文件集合
  222. */
  223. private static List<File> getSubFiles(File baseDir) {
  224. List<File> ret = new ArrayList<File>();
  225. File[] tmp = baseDir.listFiles();
  226. for (int i = 0; i < tmp.length; i++) {
  227. if (tmp[i].isFile()) {
  228. ret.add(tmp[i]);
  229. }
  230. if (tmp[i].isDirectory()) {
  231. ret.addAll(getSubFiles(tmp[i]));
  232. }
  233. }
  234. return ret;
  235. }
  236. public static boolean isExist(String path) {
  237. File file = new File(path);
  238. return file.exists();
  239. }
  240. public static boolean createDirectory(String path) {
  241. File file = new File(path);
  242. if (!isExist(path)) {
  243. file.mkdirs();
  244. }
  245. return true;
  246. }
  247. public static boolean deleteDirectory(String path) {
  248. File f = new File(path);
  249. if (isExist(path)) {
  250. if (f.isDirectory()) {
  251. File[] files = f.listFiles();
  252. for (int i = 0; i < files.length; i++) {
  253. deleteDirectory(files[i].getPath());
  254. }
  255. f.delete();
  256. } else if (f.isFile()) {
  257. f.delete();
  258. }
  259. }
  260. return true;
  261. }
  262. }