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

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

Java代码  
[java]  view plain copy print ?
  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.     public void zip(String src, String dest, List filter) throws Exception {     
  24.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(dest));     
  25.         File srcFile = new File(src);     
  26.         zip(out,srcFile,"",filter);     
  27.         out.close();     
  28.     }     
  29.          
  30.          
  31.     public void zip(ZipOutputStream out, File srcFile, String base, List filter) throws Exception {     
  32.         if(srcFile.exists()==false) {     
  33.             throw new Exception("压缩目录不存在!");     
  34.         }     
  35.              
  36.         if(srcFile.isDirectory()) {     
  37.             File[] files = srcFile.listFiles();     
  38.             base = base.length() == 0 ? "" : base + "/";     
  39.             if (isExist(base, filter)) {     
  40.                 out.putNextEntry(new ZipEntry(base));     
  41.             }     
  42.             for(int i=0; i<files.length; i++) {     
  43.                 zip(out,files[i],base + files[i].getName(),filter);     
  44.             }     
  45.         } else {     
  46.             if (isExist(base, filter)) {     
  47.                 base = base.length() == 0 ? srcFile.getName() : base ;     
  48.                 ZipEntry zipEntry = new ZipEntry(base);     
  49.                 zipEntry.setComment(comment);     
  50.                 out.putNextEntry(zipEntry);     
  51.                 FileInputStream in = new FileInputStream(srcFile);     
  52.                 int length = 0;     
  53.                 byte[] b = new byte[1024];     
  54.                 while((length=in.read(b,0,1024))!=-1) {     
  55.                     out.write(b,0,length);     
  56.                 }     
  57.                 in.close();     
  58.             }     
  59.         }     
  60.     }     
  61.          
  62.          
  63.     public boolean isExist(String base, List list) {     
  64.         if (list != null && !list.isEmpty()) {     
  65.             for (int i = 0; i < list.size(); i++) {     
  66.                 if (base.indexOf((String) list.get(i)) >= 0) {     
  67.                     return true;     
  68.                 }     
  69.             }     
  70.         }     
  71.         return false;     
  72.     }     
  73.          
  74.          
  75.     public void unZip(String srcFile,String dest,boolean deleteFile)  throws Exception {     
  76.         File file = new File(srcFile);     
  77.         if(!file.exists()) {     
  78.             throw new Exception("解压文件不存在!");     
  79.         }     
  80.         ZipFile zipFile = new ZipFile(file,"GB2312");     
  81.         Enumeration e = zipFile.getEntries();     
  82.         while(e.hasMoreElements()) {     
  83.             ZipEntry zipEntry = (ZipEntry)e.nextElement();     
  84.             if(zipEntry.isDirectory()) {     
  85.                 String name = zipEntry.getName();     
  86.                 name = name.substring(0,name.length()-1);     
  87.                 File f = new File(dest + name);     
  88.                 f.mkdirs();     
  89.             } else {     
  90.                 File f = new File(dest + zipEntry.getName());     
  91.                 f.getParentFile().mkdirs();     
  92.                 f.createNewFile();     
  93.                 InputStream is = zipFile.getInputStream(zipEntry);     
  94.                 FileOutputStream fos = new FileOutputStream(f);     
  95.                 int length = 0;     
  96.                 byte[] b = new byte[1024];     
  97.                 while((length=is.read(b, 01024))!=-1) {     
  98.                     fos.write(b, 0, length);     
  99.                 }     
  100.                 is.close();     
  101.                 fos.close();     
  102.             }     
  103.         }     
  104.              
  105.         if (zipFile != null) {     
  106.             zipFile.close();     
  107.         }     
  108.              
  109.         if(deleteFile) {     
  110.             file.deleteOnExit();     
  111.         }     
  112.     }     
  113.          
  114.          
  115.     public static String getZipComment(String srcFile) {     
  116.         String comment = "";     
  117.         try {     
  118.             ZipFile zipFile = new ZipFile(srcFile);     
  119.             Enumeration e = zipFile.getEntries();     
  120.      
  121.             while (e.hasMoreElements()) {     
  122.                 ZipEntry ze = (ZipEntry) e.nextElement();     
  123.      
  124.                 comment = ze.getComment();     
  125.                 if (comment != null && !comment.equals("")     
  126.                         && !comment.equals("null")) {     
  127.                     break;     
  128.                 }     
  129.             }     
  130.      
  131.             zipFile.close();     
  132.         } catch (Exception e) {     
  133.             System.out.println("获取zip文件注释信息失败:" + e.getMessage());     
  134.         }     
  135.      
  136.         return comment;     
  137.     }     
  138.      
  139.     public static void main(String[] args) throws Exception {     
  140.         long begin = System.currentTimeMillis();     
  141.         ZipUtil zu = new ZipUtil();     
  142.         List<String> filter = new ArrayList<String>();     
  143.         filter.add("3RDPARTY");     
  144.         filter.add("BANNER.GIF");     
  145.         zu.setComment("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");     
  146.         zu.zip("C:/VALUEADD""c:/hh.zip",filter);     
  147.         System.out.println(ZipUtil.getZipComment("c:/hh.zip"));     
  148.         //new ZipUtil().unZip("c:/tt.zip", "c:/mmmmmmmmmmmmmmmmmmm/", true);     
  149.         //File f = new File("c:/hh.zip");     
  150.         //f.deleteOnExit();     
  151.         long end = System.currentTimeMillis();     
  152.         System.out.println(end-begin);     
  153.     }     
  154. }     
  155.     
  156.   
  157.   
  158.   
  159.   
  160. 如果不需要上面的filter,可以添加fileter为空list或者用下面的程序:   
  161.   
  162.   
  163. 不带filter的zip压缩:   
  164. //   
  165. // * @author WeiMiao   
  166. // * @param out: ZipOutputStream   
  167. // * @param srcFile: 要压缩的目录   
  168. // * @param base: 根路径   
  169. // * @throws Exception   
  170. //   
  171. public static void zip(ZipOutputStream out, File srcFile, String base) throws Exception {   
  172.     if (!srcFile.exists()) {   
  173.         throw new Exception("压缩目录不存在!");   
  174.     }   
  175.     if (srcFile.isDirectory()) {   
  176.         File[] files = srcFile.listFiles();   
  177.         base = base.length() == 0 ? "" : base + "/";   
  178.         if (base.length() > 0) {   
  179.             out.putNextEntry(new ZipEntry(base));   
  180.         }   
  181.         for (int i = 0; i < files.length; i++) {   
  182.             zip(out, files[i], base + files[i].getName());   
  183.         }   
  184.     } else {   
  185.         base = base.length() == 0 ? srcFile.getName() : base;   
  186.         out.putNextEntry(new ZipEntry(base));   
  187.         FileInputStream fis = new FileInputStream(srcFile);   
  188.         int length = 0;   
  189.         byte[] b = new byte[BUFFER];   
  190.         while ((length = fis.read(b, 0, BUFFER)) != -1) {   
  191.             out.write(b, 0, length);   
  192.         }   
  193.         fis.close();   
  194.     }   
  195. }   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值