java解压zip文件问题

Vyphn 2010-07-22 10:12:26
现在项目中遇到一个java解压zip文件的问题,测试那边拿了个下载不完全的zip来测试,在程序中对这个zip解压时卡死在zins.closeEntry();这一步。。。
有没什么方法可以判断zip文件是否损坏。。。除了MD5校验的方法??
...全文
1527 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang717 2011-08-24
  • 打赏
  • 举报
回复
Lz 你这个问题怎么解决的啊
Vyphn 2010-07-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 kenshintang1215 的回复:]

我也是这么写的
异常的地方是getNextEntry()这个函数,不是closeEntry()

Java code

public static byte[] unZip(byte[] data) {
byte[] b = null;
try {
ByteArrayInputStream bis = new ByteArra……
[/Quote]

我用你的方法也是卡住。。。
在/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,不会卡住。。。)
Vyphn 2010-07-23
  • 打赏
  • 举报
回复
还是没找到原因,不过用了另外一种解决方法:在下载时做控制,确保下载的包无缺失,结帖了,谢谢大家
Vyphn 2010-07-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 kenshintang1215 的回复:]

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

按照你的方法是卡在baos.write(buf, 0, num);
Hinagi 2010-07-23
  • 打赏
  • 举报
回复
read 还是getNextEntry哦? /data/anr/traces.txt中的异常应该怎么看? 不清楚,你打个断点看看呗
Vyphn 2010-07-23
  • 打赏
  • 举报
回复
自顶下。。
Hinagi 2010-07-22
  • 打赏
  • 举报
回复
我也是这么写的
异常的地方是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();
}
}


Vyphn 2010-07-22
  • 打赏
  • 举报
回复
我用的是以下代码

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的解压方法有错吗?谢谢
Hinagi 2010-07-22
  • 打赏
  • 举报
回复
我就是在Android的上试的啊
Vyphn 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 kenshintang1215 的回复:]

我这里会报这个错
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 ……
[/Quote]

我也试了在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上不一样??
Hinagi 2010-07-22
  • 打赏
  • 举报
回复
我这里会报这个错
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)
Hinagi 2010-07-22
  • 打赏
  • 举报
回复
这个还真没测试过,我也去找一个来试试,说不定我的代码也有这个问题...
vclongking 2010-07-22
  • 打赏
  • 举报
回复
关注学习一下
Vyphn 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kenshintang1215 的回复:]

解压下载不完全的zip不会报IOException吗?
[/Quote]

不会报。。。调试时直接卡死zins.closeEntry();这一步。。。
Hinagi 2010-07-22
  • 打赏
  • 举报
回复
解压下载不完全的zip不会报IOException吗?

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧