在android中解压时发生java.io.FileNotFoundException

标签 java android java-io

我正在尝试解压缩 zip 文件。该文件包含其他几个文件。我使用的方法是:

private void unzipFile(String inputFileName,String outputDirName){
    int dataCount=0;
    byte[] dataArr=null;
    File inputZipFile=null,targetDir=null,unzipFile=null;
    ZipInputStream zipIPStream=null;
    ZipEntry zipEntry=null;
    BufferedOutputStream bfrOPStream=null;
    FileOutputStream unzipOPStream=null;
    try{
        inputZipFile=new File(inputFileName);
        targetDir=new File(outputDirName);
        zipIPStream=new ZipInputStream(new BufferedInputStream(new FileInputStream(inputZipFile)));
        while((zipEntry=zipIPStream.getNextEntry())!=null){
            dataArr=new byte[2048];
            // zipEntry=zipIPStream.getNextEntry();
            System.out.println("ZipEntry:"+zipEntry.getName());
            unzipFile=new File(targetDir.getAbsolutePath()+"/"+zipEntry.getName());
            System.out.println("Checking the path:"+unzipFile.getPath());
            if(!unzipFile.getParentFile().exists()){
                unzipFile.getParentFile().mkdirs();
            }
            unzipOPStream=new FileOutputStream(targetDir.getPath()+"/"+zipEntry.getName());
            bfrOPStream=new BufferedOutputStream(unzipOPStream,2048);
            while((dataCount=zipIPStream.read(dataArr,0,2048))!=-1){
             unzipOPStream.write(dataArr,0,dataCount);
            }
            unzipOPStream.flush();
            unzipOPStream.close();
        }
    }catch(IOException ioe){
        ioe.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(zipIPStream!=null){
            try{
                zipIPStream.close();
            }catch(Exception e){

            }
        }
    }
}

该方法抛出以下错误

10-22 13:33:09.896: W/System.err(446): java.io.FileNotFoundException: /mnt/sdcard/iR/testbook/Caterpillar-Insect-Animal-Macro-HD-600x375.jpg (Not a directory)

最佳答案

请参阅下面的链接下载并解压zip文件,它将解决您的问题。

用于下载 Zip 文件:-

Download Zip File

对于提取 Zip 文件:-

Extract Zip File

请参阅下面的链接,了解解决此问题的更多信息。

Download and Extract Zip File in Android

并且 zip 文件只能使用 winrar 软件制作,否则会出现上述错误。

关于在android中解压时发生java.io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13010646/

相关文章:

java - Struts2:如何在 webapp 之外存储图像并将其路径保存到数据库?

java - java.io.Buffer* 流与普通流有何不同?

java - Object 是抽象类的父类(super class)吗?

java - 即使从递归返回后,LinkedList 仍保留值

java - 如何从谷歌游戏控制台获取 IAP SKU 代码?

java - 双向约束 View 有什么好处吗?

android - 查找用户位置

java - TomEE/openEJB 和 log4j - 忽略的属性覆盖属性 ... log4.configurationFile?

android - 在隐藏 View 之前等待动画完成

java - 用于外部存储文件的通用图像加载器