关于java.util.zip的问题,主要是ZipEntry中getSize()返回值为-1。(附测试源码)

kingwing 2004-07-29 01:10:52
我写了很简单的zip和unzip程序,但是由zip程序打包好的文件在unzip的时候发现所有ZipEntry的size都是-1(用WinRAR打的zip包不会出现问题)。
我在zip程序中已经为每个ZipEntry都setSize了

之后发现setCRC校验,把mehtod改成STORED之后,size的问题解决了,但是在字频程序中会出现下面的异常:
java.util.zip.ZipException: invalid entry crc-32 (expected 0x0 but got 0xea200eb7)
我看了JDK的src,可能的原因是ZipOutputStream的crc值和ZipEntry的不符,晕。。。

实在没有解决的方法了
各位有遇到过类似的问题么?帮帮忙,多谢了

或者有第三方的zip包吗?Apache的common里面有个项目是相关的,但是还没有发布版本,特别是zip这块儿,只有ZipOutputStream,还没有ZipInputStream的实现

简单测试的源码我发在下一贴中
...全文
1062 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingwing 2004-07-29
  • 打赏
  • 举报
回复
To rocshaw:
http://community.csdn.net/Expert/TopicView1.asp?id=3222720
这个链接和本文看似无关啊

To Gwyongcheng:
打包肯定成功了,没有抛出任何异常,用WinRAR和WinZip都可以正常打开
孙亖 2004-07-29
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/TopicView1.asp?id=3222720
Gwyongcheng 2004-07-29
  • 打赏
  • 举报
回复
是不是打包没成功,用winrar打开有文件吗?
kingwing 2004-07-29
  • 打赏
  • 举报
回复
忙了一天了也没搞定

up
kingwing 2004-07-29
  • 打赏
  • 举报
回复
// Unzip Test

import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class SimpleUnzipTest {

public static void main(String[] args) {
String zipFile = new String("E:\\test.zip");
getExtractFileLength(zipFile);
}

static void getExtractFileLength(String zipFilePath) {
try {
FileInputStream inputstream = new FileInputStream(zipFilePath);
ZipInputStream zipinputstreamForLength = new ZipInputStream(inputstream);
ZipEntry zipentryForLength = null;

while ((zipentryForLength = zipinputstreamForLength.getNextEntry()) != null) {
System.out.println(zipentryForLength.getSize());
}
zipinputstreamForLength.close();
inputstream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
kingwing 2004-07-29
  • 打赏
  • 举报
回复
// Zip Test
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class SimpleZipTest {

public static void main(String[] args) {
try {
File zipFile = new File("E:\\test.zip");
File beZippedFile = new File("E:\\forTest.dat");
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
zipFile(beZippedFile, zos);
zos.flush();
zos.close();
System.out.println("Finish...");
} catch (Exception ex) {
ex.printStackTrace();
}
}
static void zipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
FileInputStream fis = new FileInputStream(file);
ZipEntry anEntry = new ZipEntry(file.getName());
anEntry.setSize(fis.available());
zos.putNextEntry(anEntry);
while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}
fis.close();
}
}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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