日期:2014-05-20  浏览次数:20839 次

java Zip解压
给位大大好,现在小弟在Java的Zip解压上有个问题,请给为大侠帮下忙。
  现在有个需求要把上传的文件打成Zip包后,把Zip包以BLOB的数据形式存在数据库中,然后在下载的时候在把BLOB的数据
换成Zip包,在解压这个Zip包,把里面的文件下载下来。
  现在的问题是我把上传的文件打成Zip后,不做BOLB转化,直接加压没有问题。但是把Zip==》BOLB====》Zip后,解压就出现异常。java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at com.chengruisoft.bwb.TestZip.CreateZipOrReadZip.testReadZip(CreateZipOrReadZip.java:69)
at com.chengruisoft.bwb.TestZip.TestZip.main(TestZip.java:28)
请问各位大大,这是为什么?
我用的Zip工具是Java自带的(java.util.zip.*)

压缩方法:
public static void testCreateZip(String outDir) throws Exception {
List fileList = getSubFiles(new File(outDir));
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
outDir + ".zip"));

ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
String parhString = getAbsFileName(outDir, f);
System.out.println(parhString);
ze = new ZipEntry(parhString);
ze.setSize(f.length());
ze.setTime(f.lastModified());

zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(f));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
}
zos.close();
}

解压方法:
  public static void testReadZip(String outDir , String filename) throws Exception {
ZipFile zfile = new ZipFile(filename);
Enumeration zList = zfile.entries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
System.out.println("目录: " + ze.getName() + " ..");
continue;
}

OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(outDir, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zfile.close();
}



------解决方案--------------------
BLOB-->ZIP文件这一步骤确定没有问题么?
弄个很小的Zip文件,看一下原始的和从Blob转换出来的两个文件长度有没有变化,内部信息有没有被改写
------解决方案--------------------
应该是Zip==》BOLB====》Zip这步的问题,和解压、压缩没有关系吧..........
------解决方案--------------------
必须解压是也是用程序压缩的才行!用压缩工具压缩的,解压会报错!