现在项目中遇到一个java解压zip文件的问题,测试那边拿了个下载不完全的zip来测试,在程序中对这个zip解压时卡死在zins.closeEntry();这一步
有没什么方法可以判断zip文件是否损坏除了MD5校验的方法??

解决方案 »

  1.   

    解压下载不完全的zip不会报IOException吗?
      

  2.   


    不会报调试时直接卡死zins.closeEntry();这一步
      

  3.   

    我这里会报这个错
    07-22 10:37:15.934: WARN/System.err(1277): java.io.EOFException
    07-22 10:37:15.974: WARN/System.err(1277):     at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:245)
    07-22 10:37:15.993: WARN/System.err(1277):     at com.test1.test.unZip(test.java:220)
    07-22 10:37:15.993: WARN/System.err(1277):     at com.test1.test.onCreate(test.java:149)
    07-22 10:37:15.993: WARN/System.err(1277):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    07-22 10:37:15.993: WARN/System.err(1277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2233)
    07-22 10:37:15.993: WARN/System.err(1277):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
    07-22 10:37:15.993: WARN/System.err(1277):     at android.app.ActivityThread.access$1800(ActivityThread.java:114)
    07-22 10:37:15.993: WARN/System.err(1277):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1694)
    07-22 10:37:15.993: WARN/System.err(1277):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-22 10:37:15.993: WARN/System.err(1277):     at android.os.Looper.loop(Looper.java:123)
    07-22 10:37:15.993: WARN/System.err(1277):     at android.app.ActivityThread.main(ActivityThread.java:3972)
    07-22 10:37:15.993: WARN/System.err(1277):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-22 10:37:15.993: WARN/System.err(1277):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-22 10:37:15.993: WARN/System.err(1277):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    07-22 10:37:15.993: WARN/System.err(1277):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:543)
    07-22 10:37:15.993: WARN/System.err(1277):     at dalvik.system.NativeStart.main(Native Method)
      

  4.   


    我也试了在SE工程下来解压那个ZIP包,也是报EOF异常java.io.EOFException: Unexpected end of ZLIB input stream
    at java.util.zip.InflaterInputStream.fill(Unknown Source)
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at java.util.zip.ZipInputStream.read(Unknown Source)
    at java.io.FilterInputStream.read(Unknown Source)
    at ZipUtils.Ectract(ZipUtils.java:39)
    at ZipUtils.main(ZipUtils.java:61)不过在android下就不会抛出这个异常是不是android的底层实现和SE上不一样??
      

  5.   

    我用的是以下代码import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;public class ZipUtils {
    /**
     * @param args
     */
    public static int iCompressLevel; // 压缩比 取值范围为0~9
    public static boolean bOverWrite; // 是否覆盖同名文件 取值范围为True和False
    private static ArrayList allFiles = new ArrayList();
    public static String sErrorMessage; public static ArrayList Ectract(String sZipPathFile, String sDestPath) {
    ArrayList allFileName = new ArrayList();
    try {
    // 先指定压缩档的位置和档名,建立FileInputStream对象
    FileInputStream fins = new FileInputStream(sZipPathFile);
    // 将fins传入ZipInputStream中
    ZipInputStream zins = new ZipInputStream(fins);
    ZipEntry ze = null;
    byte ch[] = new byte[256];
    while ((ze = zins.getNextEntry()) != null) {
    File zfile = new File(sDestPath + ze.getName());
    File fpath = new File(zfile.getParentFile().getPath());
    if (ze.isDirectory()) {
    if (!zfile.exists())
    zfile.mkdirs();
    zins.closeEntry();
    } else {
    if (!fpath.exists())
    fpath.mkdirs();
    FileOutputStream fouts = new FileOutputStream(zfile);
    int i;
    allFileName.add(zfile.getAbsolutePath());
    while ((i = zins.read(ch)) != -1)
    fouts.write(ch, 0, i);
    zins.closeEntry();
    fouts.close();
    }
    }
    fins.close();
    zins.close();
    sErrorMessage = "OK";
    } catch (Exception e) {
    e.printStackTrace();
    System.err.println("Extract error:" + e.getMessage());
    sErrorMessage = e.getMessage();
    }
    allFiles.clear();
    return allFileName;
    }}import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;public class AndroidUnZipTest extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button btnTest = (Button)this.findViewById(R.id.btn_test);
            btnTest.setOnClickListener(new OnClickListener(){ @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    ZipUtils.Ectract("/sdcard/123.zip", "/sdcard/22/");
    }
            
            });
        }
    }在java工程中用ZipUtils类来解压会抛EOF异常,但是在android工程中解压同一个zip文件就卡死了,也不抛异常帮忙看下ZipUtils的解压方法有错吗?谢谢
      

  6.   

    我也是这么写的
    异常的地方是getNextEntry()这个函数,不是closeEntry()
    public static byte[] unZip(byte[] data) {
      byte[] b = null;
      try {
       ByteArrayInputStream bis = new ByteArrayInputStream(data);
       ZipInputStream zip = new ZipInputStream(bis);
       while (zip.getNextEntry() != null) {
       byte[] buf = new byte[1024];
       int num = -1;
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       while ((num = zip.read(buf, 0, buf.length)) != -1) {
       baos.write(buf, 0, num);
       }
       b = baos.toByteArray();
       baos.flush();
       baos.close(); 
       }
       zip.close();
       bis.close();
     } catch (Exception ex) {
     ex.printStackTrace();
     }
     return b;
    }        File file = new File("/data/data/com.test1/x.zip");
    FileInputStream fis = null;
    if(!file.exists())
    return;

    try {
    fis = new FileInputStream(file);
    int length = (int)file.length();

    byte[] xml = new byte[length];
    fis.read(xml, 0 , length);

    unZip(xml);
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if(fis != null)
    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
      

  7.   


    我用你的方法也是卡住
    在/data/anr/traces.txt中的日志是
    DALVIK THREADS:
    "main" prio=5 tid=3 RUNNABLE
      | group="main" sCount=1 dsCount=0 s=Y obj=0x4001b268 self=0xbd00
      | sysTid=2687 nice=0 sched=0/0 cgrp=default handle=-1344001384
      at java.util.zip.Inflater.needsInput(Inflater.java:~307)
      at java.util.zip.ZipInputStream.read(ZipInputStream.java:350)
      at java.util.zip.ZipInputStream.skip(ZipInputStream.java:386)
      at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:141)
      at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:213)
    这是在ZipInputStream.read卡住的吧(在java project中会报EOFException,不会卡住)
      

  8.   

    read 还是getNextEntry哦? /data/anr/traces.txt中的异常应该怎么看?  不清楚,你打个断点看看呗
      

  9.   


    按照你的方法是卡在baos.write(buf, 0, num);