无法使用java解压缩文件但能够使用7zip解压缩它

时间:2016-04-08 00:12:03

标签: java zip unzip 7zip zipinputstream

我正在尝试使用java解压缩文件,但以下代码不会进入while循环,因为' ze'一片空白。但是我可以使用7zip应用程序解压缩相同的文件。有人能让我知道为什么会这么开心吗?

尝试{

        //get the zip file content
        ZipInputStream zis = 
            new ZipInputStream(new FileInputStream(zipFile));
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while(ze!=null){

           String fileName = ze.getName();
           File newFile = new File(outputFolder + File.separator + fileName);

           System.out.println("file unzip : "+ newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);             

            int len;
            while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
            }

            fos.close();   
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

        System.out.println("Done");

    }catch(IOException ex){
       ex.printStackTrace(); 
    }

1 个答案:

答案 0 :(得分:1)

这里的javadoc:https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html表示如果没有更多条目,getNextEntry()将返回null。 检查您的zip文件是否实际包含文件或是否为空。

我尝试使用包含文件的zip代码并且它正确运行。我尝试使用空的zip文件,并且对于空文件,ze为null,因此它没有进入while循环。

相关问题