ant zip 0kb

利用ant包进行减压的时候,会发现0kb的文件  用inputStream.read()方法的时候会报错,而inputStream.available()返回的是1,所以目前还没有判断方法,只有将异常抛出,否则后面的程序也无法进行。如果有高手知道解决方法,可以给我留言

 @SuppressWarnings("unchecked")
public static void unZip(String zipfile, String destDir) {
    destDir = destDir.endsWith( "/" ) ? destDir : destDir + "/" ;
        byte b[] = new byte [1024];
        int length;
        ZipFile zipFile;
        try {
            zipFile = new ZipFile( new File(zipfile));
            Enumeration enumeration = zipFile.getEntries();
            ZipEntry zipEntry = null ;
            while (enumeration.hasMoreElements()) {
               zipEntry = (ZipEntry) enumeration.nextElement();
               File loadFile = new File(destDir + zipEntry.getName());
                   if (!loadFile.getParentFile().exists()){
                      loadFile.getParentFile().mkdirs();
                   }
                   OutputStream outputStream = new FileOutputStream(loadFile);
                   InputStream inputStream = zipFile.getInputStream(zipEntry);
                   try{
                  while ((length = inputStream.read(b)) > 0)
                      outputStream.write(b, 0, length);
                  }catch(ZipException e){
                  if(e.getMessage().equals("invalid stored block lengths")){
                     System.out.println(loadFile+"为空文件"+zipEntry.getCompressedSize()); 
                  }
                   }
            }
            System.out.println("解压成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }

     }

下面这个方法代码看起来更简洁,但是没有地方对0kb文件的判断。

public static void unzip1(String sourceZip, String destDir)  
            throws Exception {  
        try {  
            Project p = new Project();  
            Expand e = new Expand();  
            e.setProject(p);  
            e.setSrc(new File(sourceZip));  
            e.setOverwrite(false);  
            e.setDest(new File(destDir));  
            /*  
             * ant下的zip工具默认压缩编码为UTF-8编码, 而winRAR软件压缩是用的windows默认的GBK或者GB2312编码  
             * 所以解压缩时要制定编码格式  
             */  
            e.setEncoding("gbk");  
            e.execute();  
        } catch (Exception e) {  
            throw e;  
        }  
    } 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值