首页 / JAVA / (Java) 文件打包工具类
(Java) 文件打包工具类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了(Java) 文件打包工具类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3416字,纯文字阅读大概需要5分钟。
内容图文
![(Java) 文件打包工具类](/upload/InfoBanner/zyjiaocheng/1046/64273743417f4c9ab8759e4bb0da98ae.jpg)
依赖文件
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> < dependency > < groupId >org.apache.ant</groupId><artifactId>ant</artifactId><version>1.10.7</version></dependency>
package com.newpay.common; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; /** * 打包生成压缩文件 */publicclass PackUtils { privatestaticfinal Logger log = LoggerFactory.getLogger(PackUtils.class); publicstaticvoid main(String[] args) { createZip("F:\\HelloWorld", "F:\\zyu.zip", false); } /** * 创建ZIP文件 * * @param sourcePath 文件或文件夹路径 * @param zipPath 生成的zip文件保存路径(包括文件名) * @param isDrop 是否删除原文件:true删除、false不删除 */publicstaticvoid createZip(String sourcePath, String zipPath, Boolean isDrop) { FileOutputStream fos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(zipPath); zos = new ZipOutputStream(fos); zos.setEncoding("gbk");//此处修改字节码方式。 //createXmlFile(sourcePath,"293.xml"); writeZip(new File(sourcePath), "", zos, isDrop); } catch (FileNotFoundException e) { log.error("创建ZIP文件失败", e); } finally { try { if (zos != null) { zos.close(); } } catch (IOException e) { log.error("创建ZIP文件失败", e); } } } /** * 清空文件和文件目录 * * @param f */publicstaticvoid clean(File f) throws Exception { String cs[] = f.list(); if (cs == null || cs.length <= 0) { System.out.println("delFile:[ " + f + " ]"); boolean isDelete = f.delete(); if (!isDelete) { System.out.println("delFile:[ " + f.getName() + "文件删除失败!" + " ]"); thrownew Exception(f.getName() + "文件删除失败!"); } } else { for (int i = 0; i < cs.length; i++) { String cn = cs[i]; String cp = f.getPath() + File.separator + cn; File f2 = new File(cp); if (f2.exists() && f2.isFile()) { System.out.println("delFile:[ " + f2 + " ]"); boolean isDelete = f2.delete(); if (!isDelete) { System.out.println("delFile:[ " + f2.getName() + "文件删除失败!" + " ]"); thrownew Exception(f2.getName() + "文件删除失败!"); } } elseif (f2.exists() && f2.isDirectory()) { clean(f2); } } System.out.println("delFile:[ " + f + " ]"); boolean isDelete = f.delete(); if (!isDelete) { System.out.println("delFile:[ " + f.getName() + "文件删除失败!" + " ]"); thrownew Exception(f.getName() + "文件删除失败!"); } } } privatestaticvoid writeZip(File file, String parentPath, ZipOutputStream zos, Boolean isDrop) { if (file.exists()) { if (file.isDirectory()) {//处理文件夹 parentPath += file.getName() + File.separator; File[] files = file.listFiles(); if (files.length != 0) { for (File f : files) { writeZip(f, parentPath, zos, isDrop); } } else { //空目录则创建当前目录try { zos.putNextEntry(new ZipEntry(parentPath)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { FileInputStream fis = null; try { fis = new FileInputStream(file); ZipEntry ze = new ZipEntry(parentPath + file.getName()); zos.putNextEntry(ze); byte[] content = newbyte[1024]; int len; while ((len = fis.read(content)) != -1) { zos.write(content, 0, len); zos.flush(); } } catch (FileNotFoundException e) { log.error("创建ZIP文件失败", e); } catch (IOException e) { log.error("创建ZIP文件失败", e); } finally { try { if (fis != null) { fis.close(); } if (isDrop) { clean(file); } } catch (IOException e) { log.error("创建ZIP文件失败", e); } catch (Exception e) { e.printStackTrace(); } } } } } }
原文:https://www.cnblogs.com/zyulike/p/12919093.html
内容总结
以上是互联网集市为您收集整理的(Java) 文件打包工具类全部内容,希望文章能够帮你解决(Java) 文件打包工具类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。