Unzip文件夹在android中提供FileNotFound异常

问题描述 投票:2回答:2

我正在尝试将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
2个回答
0
投票

尝试使用以下代码:

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;
}

0
投票

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();
           }
© www.soinside.com 2019 - 2024. All rights reserved.