一个ZIP压缩包解压的代码,使用了ANT.JAR包解决了解压过程中的中文问题
由于解压后,中文目录名的前一半是用ISO5586-1编码的 后一半是用UTF-8的
所以用一半的编码转换难以实现
java 代码
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- public class FileUnit {
- public void unzipFile(String zipFileName) throws Exception {
- try {
- File f = new File(zipFileName);
- ZipFile zipFile = new ZipFile(zipFileName);
- if((!f.exists()) && (f.length() <= 0)) {
- throw new Exception("要解压的文件不存在!");
- }
- String strPath, gbkPath, strtemp;
- File tempFile = new File(f.getParent());
- strPath = tempFile.getAbsolutePath();
- java.util.Enumeration e = zipFile.getEntries();
- while(e.hasMoreElements()) {
- org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
- gbkPath=zipEnt.getName();
- if(zipEnt.isDirectory()) {
- strtemp = strPath + "/" + gbkPath;
- File dir = new File(strtemp);
- dir.mkdirs();
- continue;
- } else {
- //读写文件
- InputStream is = zipFile.getInputStream(zipEnt);
- BufferedInputStream bis = new BufferedInputStream(is);
- gbkPath=zipEnt.getName();
- strtemp = strPath + "/" + gbkPath;
- //建目录
- String strsubdir = gbkPath;
- for(int i = 0; i < strsubdir.length(); i++) {
- if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
- String temp = strPath + "/"
- + strsubdir.substring(0, i);
- File subdir = new File(temp);
- if(!subdir.exists())
- subdir.mkdir();
- }
- }
- FileOutputStream fos = new FileOutputStream(strtemp);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- int c;
- while((c = bis.read()) != -1) {
- bos.write((byte) c);
- }
- bos.close();
- fos.close();
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- throw e;
- }
- }
- }