1 packageorg.yu.units;2
3 importjava.io.Closeable;4 importjava.io.File;5 importjava.io.FileInputStream;6 importjava.io.FileOutputStream;7 importjava.io.IOException;8 importjava.io.InputStream;9 importjava.util.zip.ZipEntry;10 importjava.util.zip.ZipInputStream;11
12
13 /**
14 *@authorHai E-mail:256051@qq.com15 *@version创建时间:2017年10月20日 上午10:30:03 类说明16 */
17 /**
18 *@authorHH19 *20 */
21 public classzipFile {22
23 public static voidmain(String... args) {24 extractZipFile("e:\\xx\\nbproject.zip","e:\\xx", true);25 }26
27 public static boolean extractZipFile(String zipFilePath, String path, booleanoverwrite) {28 return extractZipFile(newFile(zipFilePath), path, overwrite);29 }30
31 public static boolean extractZipFile(File zipFilePath, String destDirectory, booleanoverwrite) {32 InputStream inputStream = null;33 ZipInputStream zipInputStream = null;34 boolean status = true;35
36 try{37 inputStream = newFileInputStream(zipFilePath);38
39 zipInputStream = newZipInputStream(inputStream);40 final byte[] data = new byte[1024];41
42 while (true) {43 ZipEntry zipEntry = null;44 FileOutputStream outputStream = null;45
46 try{47 zipEntry =zipInputStream.getNextEntry();48
49 if (zipEntry == null) {50 break;51 }52
53 finalString destination;54 if(destDirectory.endsWith(File.separator)) {55 destination = destDirectory +zipEntry.getName();56 } else{57 destination = destDirectory + File.separator +zipEntry.getName();58 }59
60 if (overwrite == false) {61 if(isFileOrDirectoryExist(destination)) {62 continue;63 }64 }65
66 if(zipEntry.isDirectory()) {67 createCompleteDirectoryHierarchyIfDoesNotExist(destination);68 } else{69 final File file = newFile(destination);70 //Ensure directory is there before we write the file.
71 createCompleteDirectoryHierarchyIfDoesNotExist(file.getParentFile());72
73 int size =zipInputStream.read(data);74
75 if (size > 0) {76 outputStream = newFileOutputStream(destination);77
78 do{79 outputStream.write(data, 0, size);80 size =zipInputStream.read(data);81 } while (size >= 0);82 }83 }84 } catch(IOException exp) {85 exp.printStackTrace();86 status = false;87 break;88 } finally{89 close(outputStream);90 closeEntry(zipInputStream);91 }92
93 } //while(true)
94 } catch(IOException exp) {95 exp.printStackTrace();96 status = false;97 } finally{98 close(zipInputStream);99 close(inputStream);100 }101 returnstatus;102 }103
104 public static booleancreateCompleteDirectoryHierarchyIfDoesNotExist(String directory) {105 return createCompleteDirectoryHierarchyIfDoesNotExist(newFile(directory));106 }107
108 private static booleancreateCompleteDirectoryHierarchyIfDoesNotExist(File f) {109 if (f == null)110 return true;111
112 if (false ==createCompleteDirectoryHierarchyIfDoesNotExist(f.getParentFile())) {113 return false;114 }115
116 final String path =f.getAbsolutePath();117
118 returncreateDirectoryIfDoesNotExist(path);119 }120
121 private static booleancreateDirectoryIfDoesNotExist(String directory) {122 java.io.File f = newjava.io.File(directory);123
124 if (f.exists() == false) {125 if(f.mkdir()) {126 return true;127 } else{128 return false;129 }130 }131
132 return true;133 }134
135 /**
136 * Performs close operation on Closeable stream, without the need of137 * writing cumbersome try...catch block.138 *139 *@paramcloseable The closeable stream.140 */
141 public static voidclose(Closeable closeable) {142 //Instead of returning boolean, we will just simply swallow any143 //exception silently. This is because this method will usually be144 //invoked within finally block. If we are having control statement145 //(return, break, continue) within finally block, a lot of surprise may146 //happen.147 // http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java
148 if (null !=closeable) {149 try{150 closeable.close();151 } catch(IOException ex) {152 ex.printStackTrace();153 }154 }155 }156
157 /**
158 * Performs close operation on ZIP input stream, without the need of159 * writing cumbersome try...catch block.160 *161 *@paramzipInputStream The ZIP input stream.162 */
163 public static voidcloseEntry(ZipInputStream zipInputStream) {164 //Instead of returning boolean, we will just simply swallow any165 //exception silently. This is because this method will usually be166 //invoked within finally block. If we are having control statement167 //(return, break, continue) within finally block, a lot of surprise may168 //happen.169 // http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java
170 if (null !=zipInputStream) {171 try{172 zipInputStream.closeEntry();173 } catch(IOException ex) {174 ex.printStackTrace();175 }176 }177 }178
179 public static booleanisFileOrDirectoryExist(String fileOrDirectory) {180 java.io.File f = newjava.io.File(fileOrDirectory);181 returnf.exists();182 }183 }