用zip对单个和多个文件进行压缩

 1 /**
 2      * 多个文件的ZIP压缩  (压缩的是destPath该路径下所有的文件)
 3      * @param files   待压缩的文件列表 例如,F:apache-tomcat-6.0.44-VIPcenterlogsweb_err.log
 4      * @param zipfile  压缩后的文件名称 例如,C:\TomcatLog.zip
 5      * @param destPath 某个路径名    例如,F:apache-tomcat-6.0.44-VIPcenterlogs
 6      * @return
 7      * @throws Exception
 8      */
 9     public static boolean zipFiles(List<String> files, String zipfile,String destPath){  
10         boolean bf = true;  
11   
12         // 根据文件路径构造一个文件实例  
13         File ff = new File(zipfile);  
14         // 判断目前文件是否存在,如果不存在,则新建一个  
15         if (!ff.exists()) {  
16             try {
17                 ff.createNewFile();
18             } catch (IOException e) {
19                 e.printStackTrace();
20             }  
21         }  
22         // 根据文件路径构造一个文件输出流  
23         FileOutputStream out=null;
24         ZipOutputStream zipOut=null;
25         try {
26             out = new FileOutputStream(zipfile);
27             // 传入文件输出流对象,创建ZIP数据输出流对象  
28             zipOut = new ZipOutputStream(out);  
29             // 循环待压缩的文件列表  
30             for (int i = 0; i < files.size(); i++) {  
31                 File f = new File(files.get(i));  
32                 if (!f.exists()) {  
33                     bf = false;  
34                 }  
35                 FileInputStream in =null;
36                 try {  
37                     // 创建文件输入流对象  
38                     in = new FileInputStream(files.get(i));  
39                     // 得到压缩后压缩文件里面的路径名称,例如,bbb
equest.log.9
40                     String zipDestPath=f.getAbsolutePath().substring((destPath+"\").length(), f.getAbsolutePath().length());
41                     // 创建指向压缩原始文件的入口  
42                     ZipEntry entry = new ZipEntry(zipDestPath);
43                     zipOut.putNextEntry(entry);  
44                     // 向压缩文件中输出数据  
45                     int nNumber = 0;  
46                     byte[] buffer = new byte[1024];  
47                     while ((nNumber = in.read(buffer)) != -1) {  
48                         zipOut.write(buffer, 0, nNumber);  
49                     }  
50                     
51                     // 关闭创建的流对象  
52                     
53                 } catch (IOException e) {  
54                     e.printStackTrace();  
55                     bf = false;  
56                 }  finally{
57                     if(in!=null){
58                         try{
59                             in.close();  
60                         }catch(IOException ex){
61                             ex.printStackTrace();  
62                         }
63                     }
64                 }
65             }
66             
67         } catch (FileNotFoundException e1) {
68             e1.printStackTrace();
69         }finally{
70             try {
71                 if(null != zipOut) {
72                 zipOut.close();
73                 }
74                 if(null != out) {
75                 out.close(); 
76                 }
77             } catch (IOException e) {
78                 e.printStackTrace();
79             }  
80         }
81         return bf;  
82     }
原文地址:https://www.cnblogs.com/xxj-bigshow/p/7890747.html