Java实现Zip压缩与解压(解决中文乱码问题)

解决中文压缩与解压问题 
使用的是org.apache.tools.zip包下面的相关类 
下面是自己写的类,可以设置和获取Zip文件的注释信息 

Java代码  收藏代码

  1. import java.io.File;  

  2. import java.io.FileInputStream;  

  3. import java.io.FileOutputStream;  

  4. import java.io.InputStream;  

  5. import java.util.ArrayList;  

  6. import java.util.Enumeration;  

  7. import java.util.List;  

  8.   

  9. import org.apache.tools.zip.ZipEntry;  

  10. import org.apache.tools.zip.ZipFile;  

  11. import org.apache.tools.zip.ZipOutputStream;  

  12.   

  13.   

  14. public class ZipUtil {  

  15.       

  16.     private String comment = "";  

  17.       

  18.     public void setComment(String comment) {  

  19.         this.comment = comment;  

  20.     }  

  21.   

  22.     /** 

  23.      * @param src:要压缩的目录 

  24.      * @param dest:压缩文件存档 

  25.      * @throws Exception 

  26.      */  

  27.     public void zip(String src, String dest, List filter) throws Exception {  

  28.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(dest));  

  29.         File srcFile = new File(src);  

  30.         zip(out,srcFile,"",filter);  

  31.         out.close();  

  32.     }  

  33.       

  34.     /** 

  35.      * @param out 

  36.      * @param srcFile 

  37.      * @param base:根路径 

  38.      * @param filter:过滤 

  39.      * @throws Exception 

  40.      */  

  41.     public void zip(ZipOutputStream out, File srcFile, String base, List filter) throws Exception {  

  42.         if(srcFile.exists()==false) {  

  43.             throw new Exception("压缩目录不存在!");  

  44.         }  

  45.           

  46.         if(srcFile.isDirectory()) {  

  47.             File[] files = srcFile.listFiles();  

  48.             base = base.length() == 0 ? "" : base + "/";  

  49.             if (isExist(base, filter)) {  

  50.                 out.putNextEntry(new ZipEntry(base));  

  51.             }  

  52.             for(int i=0; i<files.length; i++) {  

  53.                 zip(out,files[i],base + files[i].getName(),filter);  

  54.             }  

  55.         } else {  

  56.             if (isExist(base, filter)) {  

  57.                 base = base.length() == 0 ? srcFile.getName() : base ;  

  58.                 ZipEntry zipEntry = new ZipEntry(base);  

  59.                 zipEntry.setComment(comment);  

  60.                 out.putNextEntry(zipEntry);  

  61.                 FileInputStream in = new FileInputStream(srcFile);  

  62.                 int length = 0;  

  63.                 byte[] b = new byte[1024];  

  64.                 while((length=in.read(b,0,1024))!=-1) {  

  65.                     out.write(b,0,length);  

  66.                 }  

  67.                 in.close();  

  68.             }  

  69.         }  

  70.     }  

  71.       

  72.     /** 

  73.      * 过滤出要压缩的文件夹 

  74.      * @param base 

  75.      * @param list 

  76.      * @return 

  77.      */  

  78.     public boolean isExist(String base, List list) {  

  79.         if (list != null && !list.isEmpty()) {  

  80.             for (int i = 0; i < list.size(); i++) {  

  81.                 if (base.indexOf((String) list.get(i)) >= 0) {  

  82.                     return true;  

  83.                 }  

  84.             }  

  85.         }  

  86.         return false;  

  87.     }  

  88.       

  89.     /** 

  90.      * @param srcFile:压缩文件路径 

  91.      * @param dest:解压到的目录 

  92.      * @param deleteFile:解压完成后是否删除文件 

  93.      * @throws Exception 

  94.      */  

  95.     public void unZip(String srcFile,String dest,boolean deleteFile)  throws Exception {  

  96.         File file = new File(srcFile);  

  97.         if(!file.exists()) {  

  98.             throw new Exception("解压文件不存在!");  

  99.         }  

  100.         ZipFile zipFile = new ZipFile(file);  

  101.         Enumeration e = zipFile.getEntries();  

  102.         while(e.hasMoreElements()) {  

  103.             ZipEntry zipEntry = (ZipEntry)e.nextElement();  

  104.             if(zipEntry.isDirectory()) {  

  105.                 String name = zipEntry.getName();  

  106.                 name = name.substring(0,name.length()-1);  

  107.                 File f = new File(dest + name);  

  108.                 f.mkdirs();  

  109.             } else {  

  110.                 File f = new File(dest + zipEntry.getName());  

  111.                 f.getParentFile().mkdirs();  

  112.                 f.createNewFile();  

  113.                 InputStream is = zipFile.getInputStream(zipEntry);  

  114.                 FileOutputStream fos = new FileOutputStream(f);  

  115.                 int length = 0;  

  116.                 byte[] b = new byte[1024];  

  117.                 while((length=is.read(b, 01024))!=-1) {  

  118.                     fos.write(b, 0, length);  

  119.                 }  

  120.                 is.close();  

  121.                 fos.close();  

  122.             }  

  123.         }  

  124.           

  125.         if (zipFile != null) {  

  126.             zipFile.close();  

  127.         }  

  128.           

  129.         if(deleteFile) {  

  130.             file.deleteOnExit();  

  131.         }  

  132.     }  

  133.       

  134.     /** 

  135.      * 获取Zip文件的注释信息 

  136.      * @param srcFile 

  137.      * @return 

  138.      */  

  139.     public static String getZipComment(String srcFile) {  

  140.         String comment = "";  

  141.         try {  

  142.             ZipFile zipFile = new ZipFile(srcFile);  

  143.             Enumeration e = zipFile.getEntries();  

  144.   

  145.             while (e.hasMoreElements()) {  

  146.                 ZipEntry ze = (ZipEntry) e.nextElement();  

  147.   

  148.                 comment = ze.getComment();  

  149.                 if (comment != null && !comment.equals("")  

  150.                         && !comment.equals("null")) {  

  151.                     break;  

  152.                 }  

  153.             }  

  154.   

  155.             zipFile.close();  

  156.         } catch (Exception e) {  

  157.             System.out.println("获取zip文件注释信息失败:" + e.getMessage());  

  158.         }  

  159.   

  160.         return comment;  

  161.     }  

  162.   

  163.     public static void main(String[] args) throws Exception {  

  164.         long begin = System.currentTimeMillis();  

  165.         ZipUtil zu = new ZipUtil();  

  166.         List<String> filter = new ArrayList<String>();  

  167.         filter.add("3RDPARTY");  

  168.         filter.add("BANNER.GIF");  

  169.         zu.setComment("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");  

  170.         zu.zip("C:/VALUEADD""c:/hh.zip",filter);  

  171.         System.out.println(ZipUtil.getZipComment("c:/hh.zip"));  

  172.         //new ZipUtil().unZip("c:/tt.zip", "c:/mmmmmmmmmmmmmmmmmmm/", true);  

  173.         //File f = new File("c:/hh.zip");  

  174.         //f.deleteOnExit();  

  175.         long end = System.currentTimeMillis();  

  176.         System.out.println(end-begin);  

  177.     }  

  178. }  


转载于:https://my.oschina.net/sexgirl/blog/347467

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值