Zip提取失败

时间:2011-12-10 16:18:40

标签: android zip compression

我需要将zip文件从我的资源文件夹解压缩到SD卡,以便完成安装。

由于zip大约10MB,我把它作为mp3文件放在assets文件夹中,然后将存档复制到SD卡(md5总和是相同的)。

然后就是提取它的问题:我在stackoverflow上尝试了zip文件的每一个解压缩方法,但它们都没有工作。

我正在使用的那个部分工作 - 有时它会提取所有文件,但大多数时候它会在中间崩溃(给我一个非常长的调试输出)。

我附上了代码和部分输出。

有人可以帮忙吗?它不必是一个zip文件,任何其他归档格式如tar也可以。 / edit:我在服务中运行此代码,这可能是个问题吗?

提取代码:

private final static int CHUNK_SIZE = 32 * 1024;

public static void unzipFile( String zipFile, String directory) 
    throws IOException
{ 
    byte[] _fileIOBuffer = new byte[CHUNK_SIZE];
    ZipInputStream in = null; 
    FileOutputStream os = null; 
    try 
    {
        in = new ZipInputStream (new FileInputStream(new File(zipFile))); 
        ZipEntry entry = null; 
        while ((entry = in.getNextEntry ())!= null) 
        { 
            String entryName = entry.getName();                 
            if (entry.isDirectory ()) { 
                File file = new File (directory, entryName); 
                file.mkdirs(); 
            } 
            else { 
                File file = new File(directory, entryName);
                if (file.exists()){
                    file.delete();  // I don't know how to append, so delete it always
                }
                os = new FileOutputStream (file); 

                int bytesRead = 0; 
                while ((bytesRead = in.read (_fileIOBuffer))!= -1) {
                    os.write(_fileIOBuffer, 0, bytesRead);
                    // progress procedure
                }
                os.close();
            }
        }
    }
    catch (FileNotFoundException e) {        
        Log.v("unzip", e.getMessage());
    } 
    catch (IOException e) {
        Log.v("unzip", e.getMessage());
    } 
    finally{
        if (in != null ){
            in.close();
        }
        if (os != null ){
            os.close();
        }
    }
}

某些日志输出:

W/dalvikvm(23094): "Binder Thread #1" prio=5 tid=5 NATIVE
W/dalvikvm(23094):   | group="main" sCount=1 dsCount=0 s=N obj=0x46e7a300 self=0x12aba8
W/dalvikvm(23094):   | sysTid=23098 nice=0 sched=3/0 cgrp=unknown handle=1369000
W/dalvikvm(23094):   | schedstat=( 806168347 48580865164 5171 )
W/dalvikvm(23094):   at dalvik.system.NativeStart.run(Native Method)
W/dalvikvm(23094): 
W/dalvikvm(23094): "Compiler" daemon prio=5 tid=4 VMWAIT
W/dalvikvm(23094):   | group="system" sCount=1 dsCount=0 s=N obj=0x46e732a0 self=0x125638
W/dalvikvm(23094):   | sysTid=23097 nice=0 sched=3/0 cgrp=unknown handle=1225832
W/dalvikvm(23094):   | schedstat=( 864668103 47259131847 4450 )
W/dalvikvm(23094):   at dalvik.system.NativeStart.run(Native Method)
W/dalvikvm(23094): 
W/dalvikvm(23094): "Signal Catcher" daemon prio=5 tid=3 RUNNABLE
W/dalvikvm(23094):   | group="system" sCount=0 dsCount=0 s=N obj=0x46e731e8 self=0x12b278
W/dalvikvm(23094):   | sysTid=23096 nice=0 sched=3/0 cgrp=unknown handle=1308736
W/dalvikvm(23094):   | schedstat=( 113256650 45104360665 3544 )
W/dalvikvm(23094):   at dalvik.system.NativeStart.run(Native Method)
W/dalvikvm(23094): 
W/dalvikvm(23094): "HeapWorker" daemon prio=5 tid=2 VMWAIT
W/dalvikvm(23094):   | group="system" sCount=1 dsCount=0 s=N obj=0x448f6f78 self=0x149d40
W/dalvikvm(23094):   | sysTid=23095 nice=0 sched=3/0 cgrp=unknown handle=1304104
W/dalvikvm(23094):   | schedstat=( 1423918773 45794713359 3888 )
W/dalvikvm(23094):   at dalvik.system.NativeStart.run(Native Method)
W/dalvikvm(23094): 
W/dalvikvm(28514): 
W/dalvikvm(28514): 
W/dalvikvm(28514): ----- pid 28514 at 2011-12-10 17:08:17 -----
W/dalvikvm(28514): Cmd line: com.google.android.apps.maps:NetworkLocationService
W/dalvikvm(28514): 
W/dalvikvm(28514): DALVIK THREADS:
W/dalvikvm(28514): "main" prio=5 tid=1 WAIT
W/dalvikvm(28514):   | group="main" sCount=1 dsCount=0 s=N obj=0x4001d938 self=0xcd98
W/dalvikvm(28514):   | sysTid=28514 nice=0 sched=3/0 cgrp=unknown handle=-1345017808
W/dalvikvm(28514):   | schedstat=( 1203913753 1177626459 364 )
W/dalvikvm(28514):   at java.lang.Object.wait(Native Method)
W/dalvikvm(28514):   - waiting on <0x46faf6c8> (a android.os.MessageQueue)
W/dalvikvm(28514):   at java.lang.Object.wait(Object.java:288)
W/dalvikvm(28514):   at android.os.MessageQueue.next(MessageQueue.java:146)
W/dalvikvm(28514):   at android.os.Looper.loop(Looper.java:110)
W/dalvikvm(28514):   at android.app.ActivityThread.main(ActivityThread.java:4627)
W/dalvikvm(28514):   at java.lang.reflect.Method.invokeNative(Native Method)
W/dalvikvm(28514):   at java.lang.reflect.Method.invoke(Method.java:521)
W/dalvikvm(28514):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
W/dalvikvm(28514):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/dalvikvm(28514):   at dalvik.system.NativeStart.main(Native Method)
W/dalvikvm(28514): 
W/dalvikvm(28514): "RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@47054bc0" daemon prio=5 tid=16 WAIT
W/dalvikvm(28514):   | group="main" sCount=1 dsCount=0 s=N obj=0x47036980 self=0x242a70
W/dalvikvm(28514):   | sysTid=28535 nice=0 sched=3/0 cgrp=unknown handle=1227616
W/dalvikvm(28514):   | schedstat=( 206874 20758542 2 )
W/dalvikvm(28514):   at java.lang.Object.wait(Native Method)
W/dalvikvm(28514):   - waiting on <0x46fd3838> (a java.lang.ref.ReferenceQueue)
W/dalvikvm(28514):   at java.lang.Object.wait(Object.java:326)
W/dalvikvm(28514):   at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:107)
W/dalvikvm(28514):   at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:80)
W/dalvikvm(28514):   at org.apache.http.impl.conn.tsccm.RefQueueWorker.run(RefQueueWorker.java:102)
W/dalvikvm(28514):   at java.lang.Thread.run(Thread.java:1096)
W/dalvikvm(28514): 
W/dalvikvm(28514): "NetworkLocationCallbackRunner" prio=5 tid=15 WAIT
W/dalvikvm(28514):   | group="main" sCount=1 dsCount=0 s=N obj=0x470429d8 self=0x13c4c8
W/dalvikvm(28514):   | sysTid=28534 nice=-4 sched=3/0 cgrp=unknown handle=1248160
W/dalvikvm(28514):   | schedstat=( 568270822 541402506 640 )
W/dalvikvm(28514):   at java.lang.Object.wait(Native Method)
W/dalvikvm(28514):   - waiting on <0x47112b88> (a android.os.MessageQueue)
W/dalvikvm(28514):   at java.lang.Object.wait(Object.java:288)
W/dalvikvm(28514):   at android.os.MessageQueue.next(MessageQueue.java:146)
W/dalvikvm(28514):   at android.os.Looper.loop(Looper.java:110)
W/dalvikvm(28514):   at com.google.android.location.os.real.i.run((null):-1)
W/dalvikvm(28514):   at java.lang.Thread.run(Thread.java:1096)
W/dalvikvm(28514): 
W/dalvikvm(28514): "Thread-16" daemon prio=1 tid=14 WAIT
W/dalvikvm(28514):   | group="main" sCount=1 dsCount=0 s=N obj=0x470706a0 self=0x243c38
W/dalvikvm(28514):   | sysTid=28533 nice=19 sched=3/0 cgrp=unknown handle=2374056
W/dalvikvm(28514):   | schedstat=( 343958 16096459 3 )
W/dalvikvm(28514):   at java.lang.Object.wait(Native Method)
W/dalvikvm(28514):   - waiting on <0x470709c8> (a java.lang.VMThread)
W/dalvikvm(28514):   at java.lang.Thread.parkFor(Thread.java:1535)
W/dalvikvm(28514):   at java.lang.LangAccessImpl.parkFor(LangAccessImpl.java:48)
W/dalvikvm(28514):   at sun.misc.Unsafe.park(Unsafe.java:317)
W/dalvikvm(28514):   at java.util.concurrent.locks.LockSupport.park(LockSupport.java:131)
W/dalvikvm(28514):   at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1996)
W/dalvikvm(28514):   at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:359)
W/dalvikvm(28514):   at b.a.run((null):-1)

1 个答案:

答案 0 :(得分:2)

in.read(byte[])未在ZipInputStream上定义,而是在父类FilterInputStream上定义。我认为它会混淆ZipInputStream的入口边界。

而是尝试使用in.read(_fileIOBuffer, 0, _fileIOBuffer.length)