小弟从服务器端获取到了一个压缩文件,然后在客户端进行解压.
InputStream is=XXXXXXX;
ZipEntry entry;
ZipInputStream in = new ZipInputStream(is);
while ((entry = in.getNextEntry()) != null) 在执行到这一步时出现了异常:
java.util.zip.ZipException: unknown format (EXTSIG=80050)
08-31 14:01:23.760: WARN/System.err(24160): at java.util.zip.ZipInputStream.readAndVerifyDataDescriptor(ZipInputStream.java:176)
08-31 14:01:23.760: WARN/System.err(24160): at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:147)
08-31 14:01:23.760: WARN/System.err(24160): at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:200)
小弟多处搜索未找到原因,服务器的压缩文件从浏览器上可以成功下载和解压...欢迎研究和讨论!
估计源文件有错吧,或者源文件的压缩格式不被JAVA的API支持,换个简单的压缩文件试试呢。
你得这样
[code="java"]int len;
// 读取下载文件内容
while ((len = is.read(bt)) > 0) {
zout.write(bt, 0, len);
}
zout.closeEntry();
is.close();[/code]
确定文件是zip格式压缩的么,如果是rar的,这个程序解压不了吧
[quote]文件是用WinRAR压缩成ZIP格式的 [/quote]
你的ZIP包里文件多不?你可以试着只把一个文件(比如一个 TXT 文件)压缩成ZIP文件,再试试看
[quote]一个文件貌似没有问题... [/quote]
会不会你从服务器下载的源文件中还包含其格式的压缩文件啊?
刚偷空写了一个,可以解压多个文件,但不支持压缩文件里包含中文名称的文件
[code="java"]import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ZipUtil {
public static void decompression(File zipPath, File dpath) throws Exception {
if (zipPath == null) {
throw new IllegalArgumentException("zipFile cannot be null");
}
if (!zipPath.isFile() && zipPath.exists()) {
throw new Exception("指定路径是目录或文件不存在:" + zipPath.getAbsolutePath());
}
String zipFileName = zipPath.getName();
if (zipFileName.toLowerCase().endsWith(".zip")) {
int nameLength = zipFileName.length();
zipFileName = zipFileName.substring(0, nameLength - 4);
}
if (dpath == null) {
dpath = new File(zipFileName);
} else {
dpath = new File(dpath, zipFileName);
}
if (dpath.exists()) {
System.out.println("目录已经存在:" + dpath.getAbsolutePath());
} else {
dpath.mkdirs();
}
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(
zipPath));
try {
ZipEntry entry = null;
ZipFile zipFile = new ZipFile(zipPath);
byte[] cache = new byte[8096];
while ((entry = zipInputStream.getNextEntry()) != null) {
String fileName = entry.getName();
if (entry.isDirectory()) {
new File(dpath, fileName).mkdirs();
continue;
}
InputStream inputStream = zipFile.getInputStream(entry);
OutputStream ops = null;
try {
// 创建文件,写文件
File tmpFile = new File(dpath, fileName);
tmpFile.createNewFile();
ops = new FileOutputStream(tmpFile);
int length = -1;
while ((length = inputStream.read(cache)) != -1) {
ops.write(cache, 0, length);
ops.flush();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (ops != null) {
ops.close();
}
zipInputStream.closeEntry();
}
}
} finally {
zipInputStream.close();
}
}
}[/code]