为什么我的解压缩类不会创建目录?

时间:2013-01-07 11:37:18

标签: android zip directory unzip zipfile

我有一个解压缩文件的类,当zip里面没有任何文件夹时,它可以正常工作。如果确实如此,则只会抛出错误。

她的课程:

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

这是我的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 个答案:

答案 0 :(得分:0)

我修复了这个重写代码的问题。我真的不知道发生了什么,因为它的代码相同。但现在它工作得很好。