二话不说,代码奉上:
1 import java.io.*; 2 import java.util.*; 3 import org.apache.tools.zip.*; 4 5 public class AdapterZipFile { 6 String zipFileName = null;//zip文件的绝对路径 7 String outputDirectory =null;//zip文件解压缩后的存放路径 8 ArrayList list = new ArrayList();//文件解压缩后的文件列表绝对路径 9 10 // public AdapterZipFile(String zipFileName) { 11 // if(zipFileName == null){ 12 // return; 13 // } 14 // this.zipFileName = zipFileName.replace("/","\\"); 15 // unZipFile(); 16 // } 17 18 //初始化 19 public AdapterZipFile(String zipFileName, String outputDirectory){ //可以实现压缩文件中包含子文件夹的情况 20 if(zipFileName == null){ 21 return; 22 } 23 this.zipFileName = zipFileName; 24 if(outputDirectory != null){ 25 this.outputDirectory = outputDirectory; 26 }else{ 27 this.outputDirectory = zipFileName.substring(0, 28 zipFileName.replace('\\', '/').lastIndexOf("/")); 29 } 30 // unZipFile(); 31 } 32 33 //创建目录 34 private void createDirectory(String directory, String subDirectory) { 35 String dir[]; 36 File fl = new File(directory); 37 try { 38 if (subDirectory == "" && fl.exists() != true) 39 fl.mkdirs(); 40 else if (subDirectory != "") { 41 dir = subDirectory.replace("\\","/").split("/"); 42 for (int i = 0; i < dir.length; i++) { 43 File subFile = new File(directory + File.separator + dir[i]); 44 if (!subFile.exists()) 45 subFile.mkdir(); 46 directory += File.separator + dir[i]; 47 } 48 } 49 }catch (Exception ex) { 50 System.out.println(ex.getMessage()); 51 } 52 } 53 54 //解压缩文件 55 //如果outputDierctory为null,解压到zipFileName文件的当前目录下 56 public void unZipFile() { 57 File file = new File(zipFileName); 58 if (!file.exists()) { 59 return; 60 } 61 createDirectory(outputDirectory, ""); 62 63 try { 64 ZipFile zipFile = new ZipFile(zipFileName); 65 Enumeration e = zipFile.getEntries(); 66 ZipEntry zipEntry = null; 67 while (e.hasMoreElements()) { 68 zipEntry = (ZipEntry) e.nextElement(); 69 if (zipEntry.isDirectory()) { 70 String name = zipEntry.getName(); 71 name = name.substring(0, name.length() - 1); 72 File f = new File(outputDirectory + File.separator + name); 73 f.mkdir(); 74 } else { 75 String fileName = zipEntry.getName(); 76 fileName = fileName.replace('\\', '/'); 77 //文件在多层目录下 这时需要创建目录如:d1/d2/1.txt 78 if (fileName.indexOf("/") != -1) { 79 createDirectory(outputDirectory, fileName.substring(0, 80 fileName.lastIndexOf("/"))); 81 fileName = fileName.substring( 82 fileName.lastIndexOf("/") + 1, fileName 83 .length()); 84 } 85 86 list.add(outputDirectory + File.separator 87 + zipEntry.getName()); 88 File f = new File(outputDirectory + File.separator 89 + zipEntry.getName()); 90 91 f.createNewFile(); 92 InputStream in = zipFile.getInputStream(zipEntry); 93 FileOutputStream out = new FileOutputStream(f); 94 95 byte[] by = new byte[1024*1024*6]; 96 int c; 97 while ((c = in.read(by)) != -1) { 98 out.write(by, 0, c); 99 } 100 out.close(); 101 in.close(); 102 } 103 } 104 } catch (Exception ex) { 105 System.out.println(ex.getMessage()); 106 } 107 } 108 109 //返回zip文件解压撒后文件列表的绝对路径 110 public ArrayList GetZipFileList(){ 111 return list; 112 } 113 public static void main(String[] args) { 114 AdapterZipFile uzip=new AdapterZipFile("F:/lucene视频和示例.zip","F:/2013"); 115 long a=System.currentTimeMillis(); 116 uzip.unZipFile(); 117 long b=System.currentTimeMillis(); 118 System.out.println("文件解压耗时"+(b-a) +"毫秒"); 119 } 120 }
解压可以根据目标文件的大小,适时的调整缓存流的大小。刚刚测试了下把缓存流设置在1024还是最佳的,解压900MB左右的ZIP文件大概需要22S左右,当然这可能和你自己所用的电脑有一点关系,但是我自己也用了解压软件测试了下,大概在10s左右,这说明上面的代码还是有很大优化的空间的,希望看到这篇博文的同学,可以和我交流下!!!!!一起学习,一起进步!@!!!!