zip条目将目录显示为文件

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

标签: java file io

嗨我试图提取一个zip文件 并且zip文件包含以下内容

zipfile.zip:

  • zip文件/ text1.txt
  • zip文件/ text2.txt
  • zip文件/分贝/模式/的text.txt
  • zip文件/伪影/ tex.txt

这是我的代码

File file = new File("/home/solomon/originar.zip");
        try {
            InputStream ios = new FileInputStream(file);
            ZipInputStream zis = new ZipInputStream(ios);
            File outputfolder = new File(file.getParent()+File.separator+"EXTRACT");
            if(!outputfolder.exists()){
                outputfolder.mkdirs();
            }
            ZipEntry ZENTRY;
            FileOutputStream fos=null;
            while((ZENTRY=zis.getNextEntry())!=null){
                File file1 = new File(outputfolder.getAbsolutePath()+File.separator+ZENTRY.getName());
                new File(file1.getParent()).mkdirs();
                System.out.println("filename"+file1.isDirectory());
                if(!file1.isDirectory()){
                    fos = new FileOutputStream(file1);
                    int len;
                    while((len=zis.read())>-1){
                        fos.write(len);
                    }
                }




            }
            fos.close(); 
            zis.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }
已成功提取

file1.txtfile2.txt,但当我在text.txt文件夹内阅读db/schema时,由于db显示为文件而非夹。怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

请参阅内联评论

// remove the EXTRACT string from the output folder path.I think there is no extract directory 

File outputfolder = new File(file.getParent()+File.separator+"EXTRACT");
.
.
.
   while((ZENTRY=zis.getNextEntry())!=null){
  // remove the outputfolder.getAbsolutePath()
  File file1 = new File(outputfolder+File.separator+ZENTRY.getName());
            new File(file1.getParent()).mkdirs();
            System.out.println("filename::"+ file1.getName()+"::"+file1.isDirectory());
            if(!file1.isDirectory()){
                fos = new FileOutputStream(file1);
                int len;
                while((len=zis.read())>-1){

                    fos.write(len);
                }
                System.out.println("file name on read::"+ file1);
            }