java解压读取rar⽂件
转⾃在此感谢作者:
<!-- 解压rar -->
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>3.0.0</version>
</dependency>
java实现解压(zip和rar⽂件)实例-解决中⽂乱码问题
1、实现说明:
在下⾯引⼊的包中可以看出:
1)压缩、解压zip是⽤到的apache 的zip包,需要在引⼊jar包(jar-ant.rar);
2)压缩、解压rar需要⽤到的是junrar,这⾥需要引⼊第三⽅jar包,我⽤到的是junrar-0.7.jar
下⾯的两个⽅法是实现解压⽂件的实例,压缩⽂件的实现其实也很简单,⼤家可以查查实现⽅式,在此我还是不关⼼压缩⽂件(相对来说⽤的少)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import ls.zip.ZipEntry;
import ls.zip.ZipFile;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
/**
*
* @author liuBf
* 类说明:解压⽂件公⽤类 *
*/
public class UnZipOrRarUtils {
/*** 这⾥⽤到了synchronized ,也就是防⽌出现并发问题 ***/
public static synchronized void untar(String tarFileName, String extPlace)
throws Exception {
unRarFile(tarFileName, extPlace);
}
public static synchronized void unzip(String zipFileName, String extPlace)
throws Exception {
unZipFiles(zipFileName, extPlace);
}
/**
* 解压zip格式的压缩⽂件到指定位置
*
* @param zipFileName
* 压缩⽂件
* @param extPlace
* 解压⽬录
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static boolean unZipFiles(String zipFileName, String extPlace)
throws Exception {
System.setProperty("ding",
try {
(new File(extPlace)).mkdirs();
File f = new File(zipFileName);
ZipFile zipFile = new ZipFile(zipFileName, "GBK"); // 处理中⽂⽂件名乱码的问题 if ((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的⽂件不存在!");
}
mkdirs方法String strPath, gbkPath, strtemp;
File tempFile = new File(extPlace);
strPath = AbsolutePath();
Enumeration<?> e = Entries();
while (e.hasMoreElements()) {
ZipEntry zipEnt = (ZipEntry) e.nextElement();
gbkPath = Name();
if (zipEnt.isDirectory()) {
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else { // 读写⽂件
InputStream is = InputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = Name();
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 (!ists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while ((c = ad()) != -1) {
bos.write((byte) c);
}
bos.close();
bos.close();
fos.close();
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据原始rar路径,解压到指定⽂件夹下.
*
* @param srcRarPath
* 原始rar路径
* @param dstDirectoryPath
* 解压到的⽂件夹
*/
public static void unRarFile(String srcRarPath, String dstDirectoryPath) {
if (!LowerCase().endsWith(".rar")) {
System.out.println("⾮rar⽂件!");
return;
}
File dstDiretory = new File(dstDirectoryPath);
if (!ists()) {// ⽬标⽬录不存在时,创建该⽂件夹
dstDiretory.mkdirs();
}
Archive a = null;
try {
a = new Archive(new File(srcRarPath));
if (a != null) {
// a.getMainHeader().print(); // 打印⽂件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
// 防⽌⽂件名中⽂乱码问题的处理
String fileName = fh.getFileNameW().isEmpty() ? fh
.getFileNameString() : fh.getFileNameW();
if (fh.isDirectory()) { // ⽂件夹
File fol = new File(dstDirectoryPath + File.separator
+ fileName);
fol.mkdirs();
} else { // ⽂件
File out = new File(dstDirectoryPath + File.separator
+ im());
try {
if (!ists()) {
if (!ParentFile().exists()) {// 相对路径可能多级,可能需要创建⽗⽬录. ParentFile().mkdirs();
}
}
FileOutputStream os = new FileOutputStream(out);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
ex.printStackTrace();
}
}
fh = a.nextFileHeader();
}
a.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String rarPath = "C:\\Users\\DELL\\Desktop\\sim\\1.rar";
File rarFile = new File(rarPath);
try {
UnZipAnRar.unRar(rarFile, "C:\\Users\\DELL\\Desktop\\");
} catch (Exception e) {
e.printStackTrace();
}
}
读取rar⽂件名称
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import java.io.*;
import java.util.Enumeration;
public class UnZipAnRar {
public static void unRar(File rarFile, String outDir) throws Exception {
File outFileDir = new File(outDir);
if (!ists()) {
boolean isMakDir = outFileDir.mkdirs();
if (isMakDir) {
System.out.println("创建压缩⽬录成功");
}
}
Archive archive = new Archive(new FileInputStream(rarFile));
FileHeader fileHeader = FileHeader();
while (fileHeader != null) {
String fileName = FileNameW().isEmpty() ? fileHeader .getFileNameString() : FileNameW();
System.out.println(fileName);
if (fileHeader.isDirectory()) {
fileHeader = FileHeader();
continue;
}
fileHeader = FileHeader();
}
archive.close();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论