JAVA解压RAR和ZIP

本文提供了解压ZIP和RAR文件的Java实现方法,包括如何读取压缩文件、创建目标目录以及将压缩文件内容写入到指定路径。

/**
* @Title: unZipFile
* @Description: 解压ZIP文件
* @author LuRh
* @param @param zipFile
* @param @param descDir
* @param @return
* @param @throws IOException
* @return boolean
* @throws
*/
@SuppressWarnings("rawtypes")
public static boolean unZipFile(String filePath, String extractToPath) {
try {
File zipFile = new File(filePath);
File extractPath = new File(extractToPath);
if (!extractPath.exists()) {
extractPath.mkdirs();
}
ZipFile zip = new ZipFile(zipFile, "GBK");
for (Enumeration entries = zip.getEntries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (extractToPath + zipEntryName).replaceAll("\\*", "/");
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));

if (!file.exists()) {
file.mkdirs();
}

if (new File(outPath).isDirectory()) {
continue;
}
if (in != null) {
OutputStream out = new FileOutputStream(outPath);
byte[] buf = new byte[2048];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
} catch (IOException e) {
return false;
}
return true;
}

/**
* @Title: unRARFile
* @Description: 解压RAR文件
* @author LuRh
* @param @param sourceRar
* @param @param destDir
* @param @return
* @param @throws Exception
* @return boolean
* @throws
*/
public static boolean unRARFile(String filePath, String extractToPath) {
Archive archive = null;
FileOutputStream fileOutputStream = null;
try {
archive = new Archive(new File(filePath));
FileHeader fileHeader = archive.nextFileHeader();
while (fileHeader != null) {
if (!fileHeader.isDirectory()) {
String compressFileName = "";
String destFileName = "";
String destDirName = "";

if (fileHeader.isUnicode()) {
compressFileName = fileHeader.getFileNameW().trim();
} else {
compressFileName = fileHeader.getFileNameString().trim();
}

// 非windows系统
if (File.separator.equals("/")) {
destFileName = extractToPath + compressFileName.replaceAll("\\\\", "/");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
// windows系统
} else {
destFileName = extractToPath + compressFileName.replaceAll("/", "\\\\");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
}
File dir = new File(destDirName);

if (!dir.exists() || !dir.isDirectory()) {
dir.mkdirs();
}

fileOutputStream = new FileOutputStream(new File(destFileName));
archive.extractFile(fileHeader, fileOutputStream);
fileOutputStream.close();
fileOutputStream = null;
}
fileHeader = archive.nextFileHeader();
}
archive.close();
archive = null;
} catch (Exception e) {
return false;
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
fileOutputStream = null;
} catch (Exception e) {
e.printStackTrace();
}
}
if (archive != null) {
try {
archive.close();
archive = null;
} catch (Exception e) {
return false;
}
}
}
return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值