ant zip 0kb

来源:互联网 发布:跟踪软件 编辑:程序博客网 时间:2024/04/30 12:41

利用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;  
        }  
    } 

原创粉丝点击