Java解压zip文件完整代码分享
程序员文章站
2023-12-20 22:50:28
关于java解压zip文件,我觉得也没啥好多说的,就是干呗。。代码如下:
package com.lanyuan.assembly.util;
import j...
关于java解压zip文件,我觉得也没啥好多说的,就是干呗。。代码如下:
package com.lanyuan.assembly.util; import java.io.bufferedoutputstream; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.util.enumeration; import org.apache.tools.zip.zipentry; import org.apache.tools.zip.zipfile; /** * 解压zip文件工具类 * @author zhangyongbo * */ public class ziputil { private static final int buffer = 2048; /** * 解压zip文件 * @param path 文件目录 */ public static void unzip(string path) { int count = -1; string savepath = ""; file file = null; inputstream is = null; fileoutputstream fos = null; bufferedoutputstream bos = null; savepath = path.substring(0, path.lastindexof(".")) + file.separator; //保存解压文件目录 new file(savepath).mkdir(); //创建保存目录 zipfile zipfile = null; try { zipfile = new zipfile(path,"gbk"); //解决中文乱码问题 enumeration<?> entries = zipfile.getentries(); while(entries.hasmoreelements()) { byte buf[] = new byte[buffer]; zipentry entry = (zipentry)entries.nextelement(); string filename = entry.getname(); boolean ismkdir = false; if(filename.lastindexof("/") != -1){ //检查此文件是否带有文件夹 ismkdir = true; } filename = savepath + filename; if(entry.isdirectory()){ //如果是文件夹先创建 file = new file(filename); file.mkdirs(); continue; } file = new file(filename); if(!file.exists()){ //如果是目录先创建 if(ismkdir){ new file(filename.substring(0, filename.lastindexof("/"))).mkdirs(); //目录先创建 } } file.createnewfile(); //创建文件 is = zipfile.getinputstream(entry); fos = new fileoutputstream(file); bos = new bufferedoutputstream(fos, buffer); while((count = is.read(buf)) > -1) { bos.write(buf, 0, count); } bos.flush(); bos.close(); fos.close(); is.close(); } zipfile.close(); }catch(ioexception ioe){ ioe.printstacktrace(); }finally{ try{ if(bos != null){ bos.close(); } if(fos != null) { fos.close(); } if(is != null){ is.close(); } if(zipfile != null){ zipfile.close(); } }catch(exception e) { e.printstacktrace(); } } } /*public static void main(string[] args) { unzip("f:\\110000002.zip"); string f = "f:\\110000002"; file file = new file(f); string[] test=file.list(); for(int i=0;i<test.length;i++){ system.out.println(test[i]); } system.out.println("------------------"); string filename = ""; file[] templist = file.listfiles(); for (int i = 0; i < templist.length; i++) { if (templist[i].isfile()) { system.out.println("文 件:"+templist[i]); filename = templist[i].getname(); system.out.println("文件名:"+filename); } if (templist[i].isdirectory()) { system.out.println("文件夹:"+templist[i]); } } } */ }
上面是第一种的代码示例,接着是另外一种,代码如下:
import java.io.*; import java.nio.charset.charset; import java.util.enumeration; import java.util.zip.zipentry; import java.util.zip.zipfile; /** * created by wzj on 2016/9/9. */ public class uzipfile { /** * 解压到指定目录 */ public static void unzipfiles(string zippath,string descdir)throws ioexception { unzipfiles(new file(zippath), descdir); } /** * 解压文件到指定目录 */ @suppresswarnings("rawtypes") public static void unzipfiles(file zipfile,string descdir)throws ioexception { file pathfile = new file(descdir); if(!pathfile.exists()) { pathfile.mkdirs(); } //解决zip文件中有中文目录或者中文文件 zipfile zip = new zipfile(zipfile, charset.forname("gbk")); for(enumeration entries = zip.entries(); entries.hasmoreelements();) { zipentry entry = (zipentry)entries.nextelement(); string zipentryname = entry.getname(); inputstream in = zip.getinputstream(entry); string outpath = (descdir+zipentryname).replaceall("\\*", "/");; //判断路径是否存在,不存在则创建文件路径 file file = new file(outpath.substring(0, outpath.lastindexof('/'))); if(!file.exists()) { file.mkdirs(); } //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 if(new file(outpath).isdirectory()) { continue; } //输出文件路径信息 system.out.println(outpath); outputstream out = new fileoutputstream(outpath); byte[] buf1 = new byte[1024]; int len; while((len=in.read(buf1))>0) { out.write(buf1,0,len); } in.close(); out.close(); } system.out.println("******************解压完毕********************"); } public static void main(string[] args) throws ioexception { /** * 解压文件 */ file zipfile = new file("d:/资料.zip"); string path = "d:/zipfile/"; unzipfiles(zipfile, path); } }
测试结果
d:/zipfile/资料/三大框架所有题.htm d:/zipfile/资料/三大框架所有题_files/bootstrap.css d:/zipfile/资料/三大框架所有题_files/bootstrap.js d:/zipfile/资料/三大框架所有题_files/css_global.css d:/zipfile/资料/三大框架所有题_files/jquery.js d:/zipfile/资料/三大框架所有题_files/logo.png d:/zipfile/资料/三大框架所有题_files/scripts(1).php d:/zipfile/资料/三大框架所有题_files/scripts(2).php d:/zipfile/资料/三大框架所有题_files/scripts.js d:/zipfile/资料/三大框架所有题_files/scripts.php d:/zipfile/资料/三大框架所有题_files/transparent.gif d:/zipfile/资料/回顾.txt d:/zipfile/资料/源码/day29_00_struts2interceptor/.classpath d:/zipfile/资料/源码/day29_00_struts2interceptor/.mymetadata d:/zipfile/资料/源码/day29_00_struts2interceptor/.project d:/zipfile/资料/源码/day29_00_struts2interceptor/.settings/.jsdtscope d:/zipfile/资料/源码/day29_00_struts2interceptor/.settings/com.genuitec.eclipse.j2eedt.core.prefs d:/zipfile/资料/源码/day29_00_struts2interceptor/.settings/org.eclipse.jdt.core.prefs d:/zipfile/资料/源码/day29_00_struts2interceptor/.settings/org.eclipse.wst.common.component d:/zipfile/资料/源码/day29_00_struts2interceptor/.settings/org.eclipse.wst.common.project.facet.core.xml d:/zipfile/资料/源码/day29_00_struts2interceptor/.settings/org.eclipse.wst.jsdt.ui.supertype.container d:/zipfile/资料/源码/day29_00_struts2interceptor/.settings/org.eclipse.wst.jsdt.ui.supertype.name d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/1.jsp d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/meta-inf/manifest.mf d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/classes/com/itheima/action/demo1action.class d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/classes/com/itheima/action/useraction.class d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/classes/com/itheima/interceptors/demo1interceptor.class d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/classes/com/itheima/interceptors/logincheckinterceptor.class d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/classes/struts.xml d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/asm-3.3.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/asm-commons-3.3.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/asm-tree-3.3.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/commons-fileupload-1.3.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/commons-io-2.0.1.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/commons-lang3-3.1.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/commons-logging-1.1.3.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/freemarker-2.3.19.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/javassist-3.11.0.ga.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/log4j-1.2.17.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/ognl-3.0.6.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/struts2-core-2.3.15.3.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/lib/xwork-core-2.3.15.3.jar d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/web-inf/web.xml d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/index.jsp d:/zipfile/资料/源码/day29_00_struts2interceptor/webroot/login.jsp d:/zipfile/资料/源码/day29_00_struts2interceptor/src/com/itheima/action/demo1action.java d:/zipfile/资料/源码/day29_00_struts2interceptor/src/com/itheima/action/useraction.java d:/zipfile/资料/源码/day29_00_struts2interceptor/src/com/itheima/interceptors/demo1interceptor.java d:/zipfile/资料/源码/day29_00_struts2interceptor/src/com/itheima/interceptors/logincheckinterceptor.java d:/zipfile/资料/源码/day29_00_struts2interceptor/src/struts.xml d:/zipfile/资料/源码/day29_01_struts2upload/.classpath d:/zipfile/资料/源码/day29_01_struts2upload/.mymetadata d:/zipfile/资料/源码/day29_01_struts2upload/.project d:/zipfile/资料/源码/day29_01_struts2upload/.settings/.jsdtscope d:/zipfile/资料/源码/day29_01_struts2upload/.settings/com.genuitec.eclipse.j2eedt.core.prefs d:/zipfile/资料/源码/day29_01_struts2upload/.settings/org.eclipse.jdt.core.prefs d:/zipfile/资料/源码/day29_01_struts2upload/.settings/org.eclipse.wst.common.component d:/zipfile/资料/源码/day29_01_struts2upload/.settings/org.eclipse.wst.common.project.facet.core.xml d:/zipfile/资料/源码/day29_01_struts2upload/.settings/org.eclipse.wst.jsdt.ui.supertype.container d:/zipfile/资料/源码/day29_01_struts2upload/.settings/org.eclipse.wst.jsdt.ui.supertype.name d:/zipfile/资料/源码/day29_01_struts2upload/webroot/1.jsp d:/zipfile/资料/源码/day29_01_struts2upload/webroot/2.jsp d:/zipfile/资料/源码/day29_01_struts2upload/webroot/meta-inf/manifest.mf d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/classes/com/itheima/action/downloadaction.class d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/classes/com/itheima/action/upload1action.class d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/classes/com/itheima/action/upload1action_zh_cn.properties d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/classes/com/itheima/action/upload2action.class d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/classes/struts.xml d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/classes/美女.jpg d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/asm-3.3.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/asm-commons-3.3.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/asm-tree-3.3.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/commons-fileupload-1.3.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/commons-io-2.0.1.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/commons-lang3-3.1.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/commons-logging-1.1.3.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/freemarker-2.3.19.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/javassist-3.11.0.ga.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/log4j-1.2.17.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/ognl-3.0.6.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/struts2-core-2.3.15.3.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/lib/xwork-core-2.3.15.3.jar d:/zipfile/资料/源码/day29_01_struts2upload/webroot/web-inf/web.xml d:/zipfile/资料/源码/day29_01_struts2upload/webroot/success.jsp d:/zipfile/资料/源码/day29_01_struts2upload/src/com/itheima/action/downloadaction.java d:/zipfile/资料/源码/day29_01_struts2upload/src/com/itheima/action/upload1action.java d:/zipfile/资料/源码/day29_01_struts2upload/src/com/itheima/action/upload1action_zh_cn.properties d:/zipfile/资料/源码/day29_01_struts2upload/src/com/itheima/action/upload2action.java d:/zipfile/资料/源码/day29_01_struts2upload/src/struts.xml d:/zipfile/资料/源码/day29_01_struts2upload/src/美女.jpg d:/zipfile/资料/源码/day29_02_struts2ognl/.classpath d:/zipfile/资料/源码/day29_02_struts2ognl/.mymetadata d:/zipfile/资料/源码/day29_02_struts2ognl/.project d:/zipfile/资料/源码/day29_02_struts2ognl/.settings/.jsdtscope d:/zipfile/资料/源码/day29_02_struts2ognl/.settings/com.genuitec.eclipse.j2eedt.core.prefs d:/zipfile/资料/源码/day29_02_struts2ognl/.settings/org.eclipse.jdt.core.prefs d:/zipfile/资料/源码/day29_02_struts2ognl/.settings/org.eclipse.wst.common.component d:/zipfile/资料/源码/day29_02_struts2ognl/.settings/org.eclipse.wst.common.project.facet.core.xml d:/zipfile/资料/源码/day29_02_struts2ognl/.settings/org.eclipse.wst.jsdt.ui.supertype.container d:/zipfile/资料/源码/day29_02_struts2ognl/.settings/org.eclipse.wst.jsdt.ui.supertype.name d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/1.jsp d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/2.jsp d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/3.jsp d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/4.jsp d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/5.jsp d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/6.jsp d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/7.jsp d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/meta-inf/manifest.mf d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/classes/com/itheima/action/demo1action.class d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/classes/com/itheima/action/demo2action.class d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/classes/com/itheima/action/demo3action.class d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/classes/com/itheima/domain/user.class d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/classes/struts.xml d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/asm-3.3.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/asm-commons-3.3.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/asm-tree-3.3.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/commons-fileupload-1.3.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/commons-io-2.0.1.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/commons-lang3-3.1.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/commons-logging-1.1.3.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/freemarker-2.3.19.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/javassist-3.11.0.ga.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/log4j-1.2.17.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/ognl-3.0.6.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/struts2-core-2.3.15.3.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/lib/xwork-core-2.3.15.3.jar d:/zipfile/资料/源码/day29_02_struts2ognl/webroot/web-inf/web.xml d:/zipfile/资料/源码/day29_02_struts2ognl/src/com/itheima/action/demo1action.java d:/zipfile/资料/源码/day29_02_struts2ognl/src/com/itheima/action/demo2action.java d:/zipfile/资料/源码/day29_02_struts2ognl/src/com/itheima/action/demo3action.java d:/zipfile/资料/源码/day29_02_struts2ognl/src/com/itheima/domain/user.java d:/zipfile/资料/源码/day29_02_struts2ognl/src/struts.xml d:/zipfile/资料/课堂笔记.doc ******************解压完毕********************
总结
以上就是java解压zip文件完整代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:java编程接口调用的作用及代码分享、java并发学习之blockingqueue实现生产者消费者详解、等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!