首页 > 其他 > 详细

文件压缩和解压缩工具类

时间:2016-03-20 17:24:54      阅读:87      评论:0      收藏:0      [点我收藏+]
  1 package com.apache.io;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStream;
  9 import java.io.OutputStream;
 10 import java.nio.charset.Charset;
 11 import java.nio.charset.StandardCharsets;
 12 import java.util.Enumeration;
 13 import java.util.zip.ZipEntry;
 14 import java.util.zip.ZipException;
 15 import java.util.zip.ZipFile;
 16 import java.util.zip.ZipOutputStream;
 17 
 18 /**
 19  * 
 20  * @author LiuJun
 21  *
 22  */
 23 public class ZipUtil {
 24 
 25     private static Charset CHARSET = StandardCharsets.UTF_8;
 26     private static String PATH = "/";
 27 
 28     /****************************************文件压缩 ****************************************/
 29     public static void zip(File file, String path) throws IOException {
 30         try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
 31                 new File(path)), CHARSET)) {
 32             compress(zos, file, "");
 33         }
 34     }
 35 
 36     private static void compress(ZipOutputStream os, File file, String basePath)
 37             throws IOException {
 38         if (file.isDirectory()) {
 39             compressDir(os, file, basePath + file.getName() + PATH);
 40         } else {
 41             compressFile(os, file, basePath);
 42         }
 43 
 44     }
 45 
 46     private static void compressFile(ZipOutputStream os, File file,
 47             String basePath) throws IOException {
 48         ZipEntry zEntry = new ZipEntry(basePath + file.getName());
 49         try (FileInputStream fs = new FileInputStream(file);) {
 50             os.putNextEntry(zEntry);
 51             write(os, fs);
 52             os.closeEntry();
 53         }
 54     }
 55 
 56     private static void compressDir(ZipOutputStream os, File file,
 57             String basePath) throws IOException {
 58         File[] files = file.listFiles();
 59         if (files.length < 1) {
 60             ZipEntry zEntry = new ZipEntry((basePath));
 61             os.putNextEntry(zEntry);
 62             os.closeEntry();
 63         } else {
 64             for (File f : files) {
 65                 compress(os, f, basePath);
 66             }
 67         }
 68 
 69     }
 70 
 71     private static void write(OutputStream os, InputStream is)
 72             throws IOException {
 73         int len = 0;
 74         byte[] b = new byte[1024 * 8];
 75         while ((len = is.read(b)) != -1) {
 76             os.write(b, 0, len);
 77         }
 78     }
 79 
 80     public static void zip(String file, String path) throws IOException {
 81         zip(new File(file), path);
 82     }
 83 
 84     /****************************************文件解压 ****************************************/
 85 
 86     public static void unzip(String file) throws IOException {
 87         unzip(file, new File(file).getParent());
 88     }
 89 
 90     public static void unzip(File file) throws IOException {
 91         unzip(file, file.getParent());
 92     }
 93 
 94     public static void unzip(String file, String path) throws IOException {
 95         unzip(new File(file), path);
 96     }
 97 
 98     public static void unzip(File f, String path) throws IOException {
 99         unZipDir(f, path, "", false);
100     }
101 
102     private static void decompress(File f, String path, ZipEntry temp,
103             ZipFile zipFile) throws IOException {
104         if (temp.isDirectory()) {
105             deprocessDir(f, path, temp);
106         } else {
107             depreocessFile(path, temp, zipFile);
108         }
109     }
110 
111     private static void depreocessFile(String path, ZipEntry temp,
112             ZipFile zipFile) throws IOException {
113         String name = temp.getName();
114         File file = new File(path + PATH + name);
115         if (!file.getParentFile().exists()) {
116             file.getParentFile().mkdirs();
117         }
118 
119         try (FileOutputStream fos = new FileOutputStream(file);
120                 InputStream is = zipFile.getInputStream(temp);) {
121             write(fos, is);
122         }
123     }
124 
125     private static void deprocessDir(File f, String path, ZipEntry temp) {
126         File f1 = new File(path + PATH + temp.getName());
127         f1.mkdirs();
128     }
129 
130     /****************************************解压单一文件****************************************/
131     public static void unZipSingle(File file, String path, String[] fileNames)
132             throws IOException {
133         ZipFile zipFile = new ZipFile(file, CHARSET);
134         Enumeration<? extends ZipEntry> entries = zipFile.entries();
135         ZipEntry zEntry = null;
136         String name;
137         while (entries.hasMoreElements()) {
138             zEntry = entries.nextElement();
139             name = zEntry.getName();
140             for (String fileName : fileNames) {
141                 if (name.indexOf(fileName) != -1) {
142                     try (InputStream is = zipFile.getInputStream(zEntry);
143                             FileOutputStream fos = new FileOutputStream(path
144                                     + PATH + getFileName(name))) {
145                         write(fos, is);
146                     }
147                 }
148             }
149         }
150         zipFile.close();
151     }
152 
153     private static String getFileName(String name) {
154         return name.substring(name.lastIndexOf(PATH) + 1);
155     }
156 
157     public static void unZipSingle(String zName, String[] fileNames)
158             throws IOException {
159         File file = new File(zName);
160         unZipSingle(file, file.getParent(), fileNames);
161     }
162     
163     public static void unZipSingle(String zName,String fileName) throws IOException{
164         unZipSingle(zName,new String[]{fileName});
165     }
166 
167     public static void unZipSingle(File file,String fileName) throws IOException{
168         unZipSingle(file,new String[]{fileName});
169     }
170     
171     public static void unZipSingle(File file, String[] fileNames)
172             throws IOException {
173         unZipSingle(file, file.getParent(), fileNames);
174     }
175 
176     public static void unZipSingle(File file, String path, String fileNames)
177             throws IOException {
178         unZipSingle(file, path, new String[] { fileNames });
179     }
180 
181     public static void unZipSingle(String file, String path, String fileNames)
182             throws IOException {
183         unZipSingle(new File(file), path, new String[] { fileNames });
184     }
185 
186     public static void unZipSingleDir(File file, String path, String fileDir)
187             throws IOException {
188         unZipDir(file, path, fileDir, true);
189     }
190     
191     /****************************************解压单一目录****************************************/
192     private static void unZipDir(File file, String path, String fileDir,
193             boolean b) throws IOException {
194         ZipFile zipFile = new ZipFile(file, CHARSET);
195         Enumeration<? extends ZipEntry> entries = zipFile.entries();
196         ZipEntry temp;
197         while (entries.hasMoreElements()) {
198             temp = entries.nextElement();
199             if (b && temp.getName().indexOf(fileDir) < 0) {
200                 continue;
201             }
202             decompress(file, path, temp, zipFile);
203         }
204         zipFile.close();
205     }
206 
207     public static void unZipSingleDir(String file, String path, String fileDir)
208             throws IOException {
209         unZipSingleDir(new File(file), path, fileDir);
210     }
211 
212     public static void unZipSingleDir(String fileName, String fileDir)
213             throws IOException {
214         File file = new File(fileName);
215         unZipSingleDir(file, file.getParent(), fileDir);
216     }
217 
218     public static void unZipSingleDir(File file, String fileDir)
219             throws IOException {
220         unZipSingleDir(file, file.getParent(), fileDir);
221     }
222 
223     /****************************************添加文件到压缩包****************************************/
224     /*
225      * 1.支持添加多个文件到单一目录
226      * 2.支持添加多个文件到对应目录
227      */
228     /**************************************************************************************/
229     public static void zipAddFile(String file, String[] filePath,String zPath) throws IOException {
230            zipAddFile(new File(file),filePath,new String[]{zPath});
231     }
232     
233     public static void zipAddFile(File file, String[] filePath,String zPath) throws IOException {
234         zipAddFile(file,filePath,new String[]{zPath});
235     }
236 
237     public static void zipAddFile(String file, String filePath,String zPath) throws IOException {
238         zipAddFile(new File(file),new String[]{filePath},new String[]{zPath});
239     }
240     
241     public static void zipAddFile(File file, String filePath,String zPath) throws IOException {
242         zipAddFile(file,new String[]{filePath},new String[]{zPath});
243     }
244     
245     public static void zipAddFile(String file,String[] filePath,String zPath[]) throws IOException{
246         zipAddFile(new File(file),filePath,zPath);
247     }
248     
249     public static void zipAddFile(File file, String[] filePath, String[] zPath)
250             throws IOException {
251 
252         File temp = File.createTempFile(file.getName(), null);
253         if (temp.delete() && file.renameTo(temp)) {
254             addFile(file, temp, filePath, zPath);
255         }
256     }
257 
258     private static void addFile(File file, File temp, String[] filePath,
259             String[] zPath) throws IOException {
260         boolean falg = zPath.length == 1;
261         if(!falg && filePath.length > zPath.length){
262             throw new IOException();
263         }
264         try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file),CHARSET);
265                 ZipFile zFile = new ZipFile(temp);){
266             Enumeration<? extends ZipEntry> entries = zFile.entries();
267             ZipEntry zipEntry;
268             String zName;
269             label:while(entries.hasMoreElements()){
270                 zipEntry = entries.nextElement();
271                 zName = zipEntry.getName();
272                 for(int i = 0;i<filePath.length;i++){
273                     File tarFile = new File(filePath[i]);
274                     if(zName.endsWith(zPath[falg? 0 :i]+PATH+tarFile.getName())){
275                         continue label;
276                     }
277                 }
278                 
279                 zos.putNextEntry(zipEntry);
280                 InputStream is = zFile.getInputStream(zipEntry);
281                 write(zos, is);
282                 zos.closeEntry();
283             }
284             
285             for(int i = 0;i<filePath.length;i++){
286                 compress(zos,new File(filePath[i]),zPath[falg? 0 :i]+PATH);
287             }
288         } catch (Exception e) {
289             throw e;
290         }
291     }
292 
293 }

 

文件压缩和解压缩工具类

原文:http://www.cnblogs.com/liujun2020/p/5297996.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!