Why my decompress class don't make directories

2019-09-14 23:48发布

问题:

I have a class to decompress files, it works fine when the zip doesn't have any folder inside. If it does then it just throw errors.

Hers my class:

public class Decompress
{
private String _zipFile;
private String _location;
ZipEntry ze = null;

public Decompress(String zipFile, String location)
{
    _zipFile = zipFile;
    _location = location;

    _dirChecker("");
}

public void unzip()
{
    try
    {
        FileInputStream fin = new FileInputStream(_zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        while ((ze = zin.getNextEntry()) != null)
        {
            Log.v("Decompress", "Unzipping " + ze.getName()); 

            if (ze.isDirectory())
            {
                _dirChecker(ze.getName());
            }
            else
            {
                FileOutputStream fout = new FileOutputStream(_location + ze.getName());
                for (int c = zin.read(); c != -1; c = zin.read())
                {
                    fout.write(c);
                }

                zin.closeEntry();
                fout.close();
            } 

        }
        zin.close();
    }
    catch (Exception e)
    {
        Log.e("Decompress", "unzip", e);
    }

}

private void _dirChecker(String dir)
{
    File f = new File(_location + dir);

    Log.d("Decompress", f.getAbsolutePath().toString());

    if (!f.isDirectory())
    }
        f.mkdirs();
    }
}

Here's my logcat:

01-07 07:33:49.941 D/Decompress(19375): /sdcard/test
01-07 07:33:49.941 V/Decompress(19375): Unzipping META-INF/
01-07 07:33:49.941 D/Decompress(19375): /sdcard/test/META-INF
01-07 07:33:49.941 V/Decompress(19375): Unzipping META-INF/CERT.RSA
01-07 07:33:49.949 E/Decompress(19375): unzip
01-07 07:33:49.949 E/Decompress(19375): java.io.FileNotFoundException: /sdcard/test/META-INF/CERT.RSA: open failed: ENOENT (No such file or directory)
01-07 07:33:49.949 E/Decompress(19375):     at libcore.io.IoBridge.open(IoBridge.java:416)
01-07 07:33:49.949 E/Decompress(19375):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-07 07:33:49.949 E/Decompress(19375):     at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
01-07 07:33:49.949 E/Decompress(19375):     at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
01-07 07:33:49.949 E/Decompress(19375):     at com.mycompany.myapp3.Decompress.unzip(Decompress.java)
01-07 07:33:49.949 E/Decompress(19375):     at com.mycompany.myapp3.MainActivity.onCreate(MainActivity.java)
01-07 07:33:49.949 E/Decompress(19375):     at android.app.Activity.performCreate(Activity.java:5108)
01-07 07:33:49.949 E/Decompress(19375):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-07 07:33:49.949 E/Decompress(19375):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2263)
01-07 07:33:49.949 E/Decompress(19375):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
01-07 07:33:49.949 E/Decompress(19375):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
01-07 07:33:49.949 E/Decompress(19375):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
01-07 07:33:49.949 E/Decompress(19375):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 07:33:49.949 E/Decompress(19375):     at android.os.Looper.loop(Looper.java:137)
01-07 07:33:49.949 E/Decompress(19375):     at android.app.ActivityThread.main(ActivityThread.java:5202)
01-07 07:33:49.949 E/Decompress(19375):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 07:33:49.949 E/Decompress(19375):     at java.lang.reflect.Method.invoke(Method.java:511)
01-07 07:33:49.949 E/Decompress(19375):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
01-07 07:33:49.949 E/Decompress(19375):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
01-07 07:33:49.949 E/Decompress(19375):     at dalvik.system.NativeStart.main(Native Method)
01-07 07:33:49.949 E/Decompress(19375): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-07 07:33:49.949 E/Decompress(19375):     at libcore.io.Posix.open(Native Method)
01-07 07:33:49.949 E/Decompress(19375):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-07 07:33:49.949 E/Decompress(19375):     at libcore.io.IoBridge.open(IoBridge.java:400)
01-07 07:33:49.949 E/Decompress(19375):     ... 19 more

回答1:

Well I fix this rewriting the code. I don't really know what happened becuase its the same code. But now its working great.