【问题标题】:Unzip folder gives FileNotFound Exception in android解压缩文件夹在 android 中给出 FileNotFound 异常
【发布时间】:2020-04-15 14:20:29
【问题描述】:

我正在尝试将 zip 文件夹解压缩到文件夹,它可以工作并且它解压缩了一些文件夹,但是当它使用 index.html 时会出现这样的错误,我想将文件解压缩到文件夹但出现错误,我该如何解决这个问题有问题吗?

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.kocsistem.pixageoneandroid/files/Contents/widgetContent/ab34f7fe018a45f0a43d3139d6986b84/index.html (No such file or directory)
        at java.io.FileOutputStream.open0(Native Method)
W/System.err:     at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:180)
        at com.kocsistem.pixageoneandroid.utils.Utils.unpackZip(Utils.java:454)
        at 

我的解压缩功能,它可以工作,但对于 index.html,它给出了上述错误

public static void unpackZip(String src, String dest, String folderName){

    final int BUFFER_SIZE = 4096;

    BufferedOutputStream bufferedOutputStream = null;
    FileInputStream fileInputStream;
    try {
        fileInputStream = new FileInputStream(src);
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
        ZipEntry zipEntry;

        while ((zipEntry = zipInputStream.getNextEntry()) != null){

            String zipEntryName = zipEntry.getName();

            String name = dest.substring(dest.lastIndexOf("/")-1);

            File FileName = new File(dest);
            if (!FileName.isDirectory()) {
                try {
                    if (FileName.mkdir()) {
                    } else {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            File file = new File(dest+ "/" + folderName +"/" +zipEntryName);

            if (file.exists()){

            } else {
                if(zipEntry.isDirectory()){
                    file.mkdirs();
                }else{
                    byte buffer[] = new byte[BUFFER_SIZE];
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
                    int count;

                    while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
                        bufferedOutputStream.write(buffer, 0, count);
                    }

                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
                }
            }
        }
        zipInputStream.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: android unzip


    【解决方案1】:

    build.gradle

    zip4j implementation 'com.github.joielechong:zip4jandroid:1.0.1'

    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
    
        }
    }
    

    代码

    String source = destFilePath + "/" + fileNameList.get(i);
    String target = destFilePath.getAbsolutePath();
    
        try {
               ZipFile zipFile = new ZipFile(source);
               zipFile.extractAll(target);
             } catch (ZipException e) {
               e.printStackTrace();
               }
    

    【讨论】:

    • 谢谢,它可以工作并且不会给出 FileNotFound 异常
    【解决方案2】:

    尝试使用以下代码:

    1.创建第一个 zip 文件

                    File FPath = new File(folder.getAbsolutePath() + File.separator + folderName);
                    if (FPath.exists()) FPath.delete();
                    String mPath = folder.getAbsolutePath() + File.separator + folderName+ ".zip";
    
    
    
                    FileOutputStream fos = new FileOutputStream(mPath);
                    InputStream is = cn.getInputStream();
                    int bytesRead = 0;
                    byte[] buf = new byte[8096];
                    long total = 0;
                    int progress;
                    int count = i;
                    while ((bytesRead = is.read(buf)) != -1) {
                        total += bytesRead;
                        fos.write(buf, 0, bytesRead);
                    }
                    if (is != null) is.close();
                    if (fos != null) {
                        fos.flush();
                        fos.close();
                    }
    

    2.创建文件时

     unpackZip(folder.getAbsolutePath() + File.separator, folderName);
    

    3.你的功能

     private boolean unpackZip(String path, String folderName) {
    
        InputStream is;
        ZipInputStream zis;
        try {
            String filename;
            is = new FileInputStream(path + folderName+ ".zip");
            zis = new ZipInputStream(new BufferedInputStream(is));
            ZipEntry ze;
            byte[] buffer = new byte[1024];
            int count;
    
            File refolder = new File(path + folderName);
            if (!refolder.exists())
                refolder.mkdirs();
    
    
            while ((ze = zis.getNextEntry()) != null) {
                filename = ze.getName();
                if (ze.isDirectory()) {
                    File fmd = new File(refolder.getAbsolutePath() + "/" + filename);
                    fmd.mkdirs();
                    continue;
                }
    
                FileOutputStream fout = new FileOutputStream(refolder.getAbsolutePath() + "/" + filename);
                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }
    
                fout.close();
                zis.closeEntry();
            }
    
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            File mFF = new File(path + folderName+ ".zip");
            mFF.delete();
        }
    
        return true;
    }
    

    【讨论】:

    • if (!refolder.exists()) if (!refolder.mkdirs()) { Toast( ... sorry could not create directory... ); return;}
    • File fmd = new File(refolder.getAbsolutePath() + "/" + filename); 更好:File fmd = new File(refolder, filename);
    • is = new FileInputStream(path + folderName+ ".zip"); 错误。应该是is = new FileInputStream(path);,正如您在原始帖子中看到的那样。
    • ContextWrapper cw = new ContextWrapper(context);文件夹 = cw.getDir("名称", Context.MODE_PRIVATE);
    • @blackapps 抱歉,请立即查看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 2013-03-09
    • 2010-09-05
    • 2011-11-10
    相关资源
    最近更新 更多