|
使用Java解压缩文件
作者无: 加入时间:2008-11-20 17:20:41 点击次数:3 次package chapter03; import java.io.*; import java.util.Enumeration; import java.util.zip.*; public class Test07 { static final int BUFFER = 2048; public static void unZip()throws Exception{ String fileName = "E:\\jdbc.zip"; String filePath = "E:\\aaa\\"; ZipFile zipFile = new ZipFile(fileName); Enumeration emu = zipFile.entries(); int i=0; while(emu.hasMoreElements()){ ZipEntry entry = (ZipEntry)emu.nextElement(); //会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。 if (entry.isDirectory()){ System.out.println("entry is a directory"); new File(filePath + entry.getName()).mkdirs(); continue; } BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); File file = new File(filePath + entry.getName()); //加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件 //而这个文件所在的目录还没有出现过,所以要建出目录来。 //File parent = file.getParentFile(); //if(parent != null && (!parent.exists())){ // parent.mkdirs(); //} FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream bos = new ObjectOutputStream(fos); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1){ bos.write(data, 0, count); } bos.flush(); bos.close(); bis.close(); } zipFile.close(); } public static void main(String [] args) throws Exception{ String from="e:\\jdbc.zip"; String to="e:\\aaa\\"; unZip1(from,to); } public static void unZip1(String from,String to)throws Exception{ //String from="e:\\aaa.zip"; //String to="E:\\aaa\\"; if(!new File(to).exists()){ new File(to).mkdir(); } ZipFile file=new ZipFile(from); Enumeration e=file.entries(); int byte_read; byte [] buffer=new byte[4096]; while(e.hasMoreElements()){ ZipEntry entry=(ZipEntry)e.nextElement(); if(entry.isDirectory()){ //String filename=entry.getName(); new File(to+entry.getName()).mkdirs(); continue; } DataInputStream ois=new DataInputStream(file.getInputStream(entry)); File f=new File(to+entry.getName()); FileOutputStream fos=new FileOutputStream(f); ObjectOutputStream oos=new ObjectOutputStream(fos); while((byte_read=ois.read(buffer))!=-1){ oos.write(buffer,0,byte_read); } oos.flush(); oos.close(); ois.close(); } } } 上一篇: jsp 实现在线人数统计 下一篇: Ajax 从XML到生成表格 |
|||||||||||||||||||||||||||||||||||||||
| 关于我们 | 代理申请 | 付款方式 | 客户管理 | 友情链接 | 网站导航 |
| 版权所有 2008 三易网络(洛阳)科技开发有限公司 服务热线:0379-63921200 63265368(洛阳) 0371-63653120(郑州) QQ在线客服: 总部地址:纱厂南路41号中泰新城泰华苑12楼B座 郑州分公司地址:金水区圣菲城 |