日期:2014-05-20  浏览次数:20866 次

java解压缩zip文件时文件内中文部分乱码
RT 网上说用org.apache.tools.zip可以解决,我用后没有效果(注释掉的部分)。代码如下,求指教

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.zip.ZipEntry;
import java.util.Enumeration;
import java.util.zip.ZipInputStream;


import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
 
//import org.apache.tools.zip.ZipEntry;
//import org.apache.tools.zip.ZipFile; 
//import org.apache.tools.zip.ZipOutputStream; 




public class UnRarUtil 
{
private static void unRar(String srcRar,String destDir) throws Exception
{
Archive a =null;
FileOutputStream fos = null;
try{
a = new Archive(new File(srcRar));
FileHeader fh = a.nextFileHeader();
while(fh != null)
{
if(!fh.isDirectory())
{
//根据不同的操作系统取得相应的destFileName,destDirNamme
String compressFileName = fh.getFileNameString().trim();
String destFileName = "";
String destDirNamme = "";
//非windows操作系统
if(File.separator.equals("/"))
{
destFileName = destDir + compressFileName.replaceAll("\\\\", "/");
destDirNamme = destFileName.substring(0, destFileName.lastIndexOf("/"));
}
else//windows系统
{
destFileName = destDir + compressFileName.replaceAll("/", "\\\\");
destDirNamme = destFileName.substring(0, destFileName.lastIndexOf("\\"));
}

File dir = new File(destDirNamme);
if(!dir.exists() || !dir.isDirectory())
{
dir.mkdirs();
}

fos = new FileOutputStream(new File(destFileName));
a.extractFile(fh, fos);
fos.close();
fos = null;
}
fh = a.nextFileHeader();
}
a.close();
a = null;
}
catch(Exception e){
throw e;
}
finally{
if(fos != null)
{
try{
fos.close();
fos = null;
}
catch(Exception e){
e.printStackTrace();
}
}
if(a != null)
{
try{
a.close();
a = null;
}
catch(Exception e){
e.printStackTrace();
}
}
}
}

public static void doCompress(String srcFile,String destDir) throws Exception
{
//保证文件夹路径最后是"\"或者"/"
char lastChar = destDir.charAt(destDir.length()-1);
if(lastChar != '\\' && lastChar != '/')
{
destDir += File.separator;
}
//根据类型进行相应的解压
String type = srcFile.substring(srcFile.lastIndexOf(".")+1);
if(type.endsWith("zip"))
{
unZip(srcFile,destDir);
}
else if(type.endsWith("rar"))
{
UnRarUtil.unRar(srcFile, destDir);
}
else
{
throw new Exception("只支持zip和rar格式的压缩包!");
}
}

public static void unZip(String unZipfileName, String mDestPath) {
  if (!mDestPath.endsWith("/")) 
  {
  mDestPath = mDestPath + "/";
  }
  FileOutputStream fileOut = null;
  ZipInputStream zipIn = null;
  ZipEntry zipEntry = null;
  File file = null;
  int readedBytes = 0;
  byte buf[] = new byte[4096];
  try {
  zipIn = new ZipInputStream(new BufferedInputStream(new FileInputStream(unZipfileName)));