java zip解压中文乱码问题

这段代码可以解决中文zip和zip包中的文件也是中文的问题


import
java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.tools.zip.ZipFile; public class unzipandrar { /** * 解压zip格式的压缩文件到指定位置 * @param zipFileName 压缩文件 * @param extPlace 解压目录 * @throws Exception */ @SuppressWarnings("unchecked") public synchronized void unzip(String zipFileName, String extPlace){ try { (new File(extPlace)).mkdirs(); 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(extPlace); strPath = tempFile.getAbsolutePath(); java.util.Enumeration e = zipFile.getEntries(); while(e.hasMoreElements()){ org.apache.tools.zip.ZipEntry zipEnt = (org.apache.tools.zip.ZipEntry)e.nextElement(); gbkPath=zipEnt.getName(); if(zipEnt.isDirectory()){ strtemp = strPath + File.separator + 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 + File.separator + gbkPath; //建目录 String strsubdir = gbkPath; for(int i = 0; i < strsubdir.length(); i++) { if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) { String temp = strPath + File.separator + 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(); try { throw e; } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }

 

posted @ 2013-08-20 20:18  编程小爬虫  阅读(3295)  评论(1编辑  收藏  举报