大家在用java做zip压缩解压缩时,java提供了原生的zip类
import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream;
但是实际应用中是,我们有两个问题
1.中文路径压缩解压缩问题,我网上找了找方案,都是要改源类库,如果有不用的请赐教
2.是我在实际应用中发现的,
unziping resultFile\resultFile1.txt
d:\resultFile\resultFile1.txt
f:d:\resultFile\resultFile1.txt
java.util.zip.ZipException: invalid entry compressed size (expected 4294967295 but got 7 bytes)
at java.util.zip.ZipInputStream.readEnd(ZipInputStream.java:391)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:156)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:105)
at TestZip.unzip(TestZip.java:55)
at TestZip.main(TestZip.java:93)
原因不明,但我估计和压缩的版本有关,zip来源是我同学用c#压缩的东西,试了很多种方法基于java原生zip的方法都不行,后来尝试了commons-compress-1.1.jar,apache的开源类库(话说很多java原生的类库比起apache的工具类都差好多),解压缩实现如下,是改了网上某人的,他的方法有点问题
public static void decompress(String srcFile, String destPath) {
try {
File outFile = new File(destPath);
if (!outFile.exists()) {
outFile.mkdirs();
}
ZipFile zipFile = new ZipFile(srcFile);
Enumeration<?> en = zipFile.getEntries();
ZipArchiveEntry zipEntry = null;
while (en.hasMoreElements()) {
zipEntry = (ZipArchiveEntry) en.nextElement();
if (zipEntry.isDirectory()) {
// mkdir directory
String dirName = zipEntry.getName();
dirName = dirName.substring(0, dirName.length() - 1);
File f = new File(outFile.getPath() + "/" + dirName);
f.mkdirs();
}
else {
// unzip file
File f = new File(outFile.getPath() + "/" + zipEntry.getName());
if(!f.getParentFile().exists()){
f.getParentFile().mkdirs();
}
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
OutputStream out = new FileOutputStream(f);
IOUtils.copy(in, out);
out.close();
in.close(); } }
zipFile.close(); }
catch (Exception e)
{ e.printStackTrace();
}
}
红字是我加的,使用无非是
decompress("d:\\resultFile.zip","e:\\1",);
附件是我找的commons-compress-1.1.jar