JAVA中无法处理带有特殊字符文件名的zip包的问题解决

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下面的类.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值