java解压缩zip文件时文件内中文部分乱码

xaqk008 xaqk008 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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)));

while ((zipEntry = zipIn.getNextEntry()) != null)
{
file = new File(mDestPath + zipEntry.getName());
if (zipEntry.isDirectory())
{
file.mkdirs();
} else
{
// 如果指定文件的目录不存在,则创建之.
File parent = file.getParentFile();
if (!parent.exists())
{
parent.mkdirs();
}
fileOut = new FileOutputStream(file);
while ((readedBytes = zipIn.read(buf)) > 0)
{
fileOut.write(buf, 0, readedBytes);
}
fileOut.close();
}
zipIn.closeEntry();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

// public static void unZip(String zipfile, String destDir)
// {
// byte b[] = new byte [1024];
// int length;
// ZipFile zipFile;
// try {
//
// zipFile = new ZipFile( new File(zipfile,"GBK"));
//
// Enumeration<?> enumeration = zipFile.getEntries();
// ZipEntry zipEntry = null ;
// while (enumeration.hasMoreElements())
// {
// zipEntry = (ZipEntry) enumeration.nextElement();
// File loadFile = new File(destDir + zipEntry.getName());
//
//
//
// if (zipEntry.isDirectory()) {
//
// // 这段都可以不要,因为每次都貌似从最底层开始遍历的
//
// loadFile.mkdirs();
//
// } else {
//
// if (!loadFile.getParentFile().exists())
//
// loadFile.getParentFile().mkdirs();
//
//
//
// OutputStream outputStream = new FileOutputStream(loadFile);
//
// InputStream inputStream = zipFile.getInputStream(zipEntry);
//
//
//
// while ((length = inputStream.read(b)) > 0)
//
// outputStream.write(b, 0, length);
//
//
//
// }
//
// }
//
// System. out .println( " 文件解压成功 " );
//
// } catch (IOException e) {
//
// // TODO Auto-generated catch block
//
// e.printStackTrace();
//
// }
//
//
//
// }







}
...全文
评论 14
收藏 已收藏
分享
返回顶部
xaqk008
Java SE 62,621 成员 307,251 内容