java解压文件
2016-08-07 22:09
183 查看
需求:从本地上传一个.zip的文件夹,文件夹中的文件是一些图片,解压到服务器的某个路径。
package com.it.test; import java.io.*; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /** * Created by lingmao on 2016/8/7. */ public class UnZipFile { public static void main(String[] args) throws IOException { /** * 解压文件 */ try { File zipFile = new File("C:/Users/lingmao/Desktop/photo/photo.zip"); String path = "e:/test/"; unZipFiles(zipFile, path); } catch (Exception e){ System.out.print(e.getMessage()); } } /** * 解压文件到指定目录 * @param zipFile * @param descDir * @author isea533 */ @SuppressWarnings("rawtypes") public static void unZipFiles(File zipFile,String descDir)throws IOException{ File pathFile = new File(descDir); if(!pathFile.exists()){ pathFile.mkdirs(); } // 如果文件的名字有中文,需要转码。 // Charset gbk = Charset.forName("GBK"); // ZipFile zip = new ZipFile(zipFile, gbk); ZipFile zip = new ZipFile(zipFile); for(Enumeration entries = zip.entries();entries.hasMoreElements();){ ZipEntry entry = (ZipEntry)entries.nextElement(); String zipEntryNamePath = entry.getName(); //去掉文件夹的名称 String zipEntryName=zipEntryNamePath.substring(zipEntryNamePath.indexOf("/")+1,zipEntryNamePath.length()); 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("******************解压完毕********************"); } }注意:经过这样的解压之后,原解压文件是删不掉的,Java虚拟机和压缩文件还有关系,要zipfile关掉才可以删掉,zipfile有个close方法。
相关文章推荐
- Java 编程之实现文件或目录的解压.
- 用java压缩及解压文件 AntZip
- Java解压zip文件
- java zip和rar压缩文件的解压
- 在解压gz文件时,如果直接用java.util.zip.GZIPInputStream来处理问题只能解压很少一部分内容,通过类MultiMemberGZIPInputStream 可以完全解压一个gz文件。
- [JAVA100例]040、压缩和解压文件(java100例)
- java 解压zip、rar文件资料收集
- java实现zip文件压缩,解压
- java中如何实现文件打包上传以及自动解压
- java解压缩zip文件,java创建zip文件,java压缩文件,java解压文件,用到ant.jar解决汉字乱码
- Java 编程之实现文件或目录的解压.
- 用java的ZipOutputStream类解压文件或文件夹中的文件
- java的解压压缩文件的程序
- java解压缩zip文件,java创建zip文件,java压缩文件,java解压文件,用到ant.jar解决汉字乱码
- java压缩与解压文件
- Java 编程之实现文件或目录的解压.
- 利用java的ZipOutputStream类解压文件或文件夹中的文件
- java 压缩、解压文件、文件夹。
- 利用java的ZipOutputStream类压缩文件,解压文件或文件夹中的文件
- java_文件解压