http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4415733
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4244499
一个由来已久的bug,但是sun对此似乎并不急于修改,可苦了我们这帮靠java吃饭的人.
幸好还有更多乐于奉献的人,幸好我们也还有很多open source.
TrueZip正好可以帮我们解决这个问题,网址是https://truezip.dev.java.net/
以下就是我用于测试的一段代码:
[CODE]
import de.schlichtherle.io.File;
import de.schlichtherle.io.FileOutputStream;
import de.schlichtherle.util.zip.ZipFile;
import de.schlichtherle.util.zip.ZipEntry;
import java.io.*;
import java.util.Enumeration;
import junit.framework.TestCase;
public class ZipTest extends TestCase {
public void testTrueZip() throws Exception {
// 因为是个法国项目,文件名中有法文特殊字符,所以用ISO-8859-1
ZipFile zipfile = new ZipFile("D:/TEMP/ISL_2_02.zip", "ISO-8859-1");
Enumeration enum_entry = zipfile.entries();
// 用于去除ZipEntry中文件夹路径的regexp. 比如foler/file.txt => file.txt
String pattern = "^([//S_//s])*/";
int buffer_size = 1024 * 4;
while (enum_entry.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enum_entry.nextElement();
System.out.println("D:/TEMP/EXT/" + entry.getName().replaceAll(pattern, ""));
BufferedInputStream fis = new BufferedInputStream( zipfile.getInputStream(entry) );
FileOutputStream fos = new FileOutputStream(new File("D:/TEMP/EXT/" + entry.getName().replaceAll(pattern, "")));
byte[] b = new byte[buffer_size];
int read = 0;
while ( (read = fis.read(b)) != -1 ) {
fos.write(b, 0, read);
}
fos.close();
fis.close();
}
}
}
[CODE]
还有一个要注意的就是类的引入(import) 采用的是drop-in replacements,所以象File这些类实际上是使用的de.schlichtherle.io包下面的类,而不是java.io下面的类.