JAVA压缩 解压缩zip 并解决linux下中文乱码

1:再压缩前,要设置linux模式, 需要使用第三方ant-1.6.5.jar
  如果是文件目录,则
ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator"));
zipEntry.setUnixMode(755);//解决linux乱码

如果是文件,则 
ZipEntry zipEntry=new ZipEntry(base);
zipEntry.setUnixMode(644);//解决linux乱码

然后在输出时强制设置编码: 
 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                zipFileName));
        out.setEncoding("GBK");//解决linux乱码

全部代码如下: 
  1 import java.io.File;
  2 import java.io.FileInputStream;
  3 import java.io.FileOutputStream;
  4 import java.io.OutputStream;
  5 
  6 import org.apache.tools.zip.ZipEntry;
  7 import org.apache.tools.zip.ZipOutputStream;
  8 
  9 import org.apache.tools.ant.Project;  
 10 
 11 import org.apache.tools.ant.taskdefs.Expand;
 12 
 13 /**
 14  * 文件夹压缩,支持win和linux
 15  * @author wlzhang
 16  *
 17  */
 18 public class ZipUtil
 19 {
 20     /**
 21      * @param inputFileName
 22      *            输入一个文件夹
 23      * @param zipFileName
 24      *            输出一个压缩文件夹,打包后文件名字
 25      * @throws Exception
 26      */
 27     public static OutputStream zip(String inputFileName, String zipFileName) throws Exception
 28     {
 29         return zip(zipFileName, new File(inputFileName));
 30     }
 31 
 32     private static OutputStream zip(String zipFileName, File inputFile) throws Exception
 33     {
 34         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
 35                 zipFileName));
 36         out.setEncoding("UTF-8");//解决linux乱码  根据linux系统的实际编码设置,可以使用命令 vi/etc/sysconfig/i18n 查看linux的系统编码
 37         zip(out, inputFile, "");
 38         out.close();
 39         return out;
 40     }
 41 
 42     private static void zip(ZipOutputStream out, File f, String base) throws Exception
 43     {
 44         if (f.isDirectory())
 45         { // 判断是否为目录
 46             File[] fl = f.listFiles();
 47             // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
 48 //            out.putNextEntry(new ZipEntry(base + "/"));
 49             ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator"));
 50             zipEntry.setUnixMode(755);//解决linux乱码
 51             out.putNextEntry(zipEntry);
 52 //            base = base.length() == 0 ? "" : base + "/";
 53             base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator");
 54             for (int i = 0; i < fl.length; i++)
 55             {
 56                 zip(out, fl[i], base + fl[i].getName());
 57             }
 58         }
 59         else
 60         { // 压缩目录中的所有文件
 61             // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
 62             ZipEntry zipEntry=new ZipEntry(base);
 63             zipEntry.setUnixMode(644);//解决linux乱码
 64             out.putNextEntry(zipEntry);
 65             FileInputStream in = new FileInputStream(f);
 66             int b;
 67             while ((b = in.read()) != -1)
 68             {
 69                 out.write(b);
 70             }
 71             in.close();
 72         }
 73     }
 74 
 75     private static void unzip(String sourceZip,String destDir) throws Exception{  
 76 
 77         try{  
 78 
 79             Project p = new Project();  
 80 
 81             Expand e = new Expand();  
 82 
 83             e.setProject(p);  
 84 
 85             e.setSrc(new File(sourceZip));  
 86 
 87             e.setOverwrite(false);  
 88 
 89             e.setDest(new File(destDir));  
 90 
 91             /* 
 92 
 93             ant下的zip工具默认压缩编码为UTF-8编码, 
 94 
 95             而winRAR软件压缩是用的windows默认的GBK或者GB2312编码 
 96 
 97             所以解压缩时要制定编码格式 
 98 
 99             */  
100 
101             e.setEncoding("UTF-8");  //根据linux系统的实际编码设置
102 
103             e.execute();  
104 
105         }catch(Exception e){  
106 
107             throw e;  
108 
109         }  
110 
111     }  
112 
113 
114 }

版权所有,转载请注明出处:http://www.oschina.net/code/snippet_250494_10312

转载于:https://www.cnblogs.com/wangrs/archive/2012/04/30/2476863.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值