嗨我试图提取一个zip文件 并且zip文件包含以下内容
zipfile.zip:
这是我的代码
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.txt
和file2.txt
,但当我在text.txt
文件夹内阅读db/schema
时,由于db
显示为文件而非夹。怎么解决这个问题?
答案 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);
}