解压rar 需要导入
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>0.7</version>
</dependency>
import com.github.junrar.Archive;
import com.github.junrar.exception.RarException;
import com.github.junrar.exception.RarException.RarExceptionType;
import com.github.junrar.rarfile.FileHeader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by lcc on 2017/9/8.
*/
public class RarUtil {
public static boolean unrar(String rarFileName, String outFilePath) throws Exception {
boolean flag = false;
try {
Archive archive = new Archive(new File(rarFileName));
if (archive == null) {
throw new FileNotFoundException(rarFileName + " NOT FOUND!");
}
if (archive.isEncrypted()) {
throw new Exception(rarFileName + " IS ENCRYPTED!");
}
List<FileHeader> files = archive.getFileHeaders();
for (FileHeader fh : files) {
if (fh.isEncrypted()) {
throw new Exception(rarFileName + " IS ENCRYPTED!");
}
String fileName = "";
if (fh.isUnicode()) {
fileName = fh.getFileNameW().trim();
} else {
fileName = fh.getFileNameString().trim();
}
if (fileName != null && fileName.trim().length() > 0) {
String saveFileName = outFilePath + "/" + fileName;
File saveFile = new File(saveFileName);
File parent = saveFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (saveFile.isDirectory()) {
continue;
}
if (!saveFile.exists()) {
saveFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(saveFile);
try {
archive.extractFile(fh, fos);
fos.flush();
fos.close();
} catch (RarException e) {
if (e.getType().equals(RarExceptionType.notImplementedYet)) {
}
} finally {
}
}
}
flag = true;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return flag;
}
解压zip需要导入
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.0</version>
</dependency>
import com.qingxing.ManagerComplex.exception.BadParameterException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import java.io.*;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
/**
* Created by lcc on 2017/9/8.
*/
public class ZipUtil {
private static byte[] ZIP_HEADER_1 = new byte[]{80, 75, 3, 4};
private static byte[] ZIP_HEADER_2 = new byte[]{80, 75, 5, 6};
/**
* 判断文件是否为一个压缩文件
*
* @param file
* @return
*/
public static boolean isArchiveFile(File file) {
if (file == null) {
return false;
}
if (file.isDirectory()) {
return false;
}
boolean isArchive = false;
InputStream input = null;
try {
input = new FileInputStream(file);
byte[] buffer = new byte[4];
int length = input.read(buffer, 0, 4);
if (length == 4) {
isArchive = (Arrays.equals(ZIP_HEADER_1, buffer)) || (Arrays.equals(ZIP_HEADER_2, buffer));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
}
}
return isArchive;
}
public static void zipToFile(String sourceFile, String toFolder) throws Exception {
String toDisk = toFolder;// 接收解压后的存放路径
org.apache.tools.zip.ZipFile zfile = new ZipFile(sourceFile, "GB2312");// 连接待解压文件
Enumeration zList = zfile.getEntries();// 得到zip包里的所有元素
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
// log.info("打开zip文件里的文件夹:"+ ze.getName());
continue;
}
OutputStream outputStream = null;
InputStream inputStream = null;
try {
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
String fileUrl = getRealFileName(toDisk, ze.getName()).getName();
outputStream = new BufferedOutputStream(new FileOutputStream(getRealFileName(toDisk, ze.getName())));
inputStream = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, readLen);
}
inputStream.close();
outputStream.close();
} catch (Exception e) {
// log.info("解压失败:"+e.toString());
throw new IOException("解压失败:" + e.toString());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
inputStream = null;
outputStream = null;
}
}
zfile.close();
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param zippath 指定根目录
* @param absFileName 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
private static File getRealFileName(String zippath, String absFileName) {
// log.info("文件名:"+absFileName);
String[] dirs = absFileName.split("/", absFileName.length());
File ret = new File(zippath);// 创建文件对象
if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
ret = new File(ret, dirs[i]);
}
}
if (!ret.exists()) {// 检测文件是否存在
ret.mkdirs();// 创建此抽象路径名指定的目录
}
ret = new File(ret, dirs[dirs.length - 1]);// 根据 ret 抽象路径名和 child
// 路径名字符串创建一个新 File 实例
return ret;
}
/**
* 遍历指定路径下的所有文件夹及文件
*
* @param dir
*/
public static List<String> getFiles(File dir, List<String> list) {
//如果当前文件或目录存在
if (dir.exists()) {
if (dir.isDirectory()) {
//获取该目录下的所有文件和目录组成的File数组
File[] files = dir.listFiles();
//递归遍历每一个子文件
for (File file : files) {
getFiles(file, list);
}
}
//如果是文件,则打印该文件路径及名称
else {
if (dir.getPath().endsWith("xml")) {
list.add(dir.getPath());
}
}
} else {
throw new BadParameterException("该文件不存在");
}
return list;
}
RarUtil.unrar("D:/upload/zip/tupian.rar","D:/upload");
ZipUtil.zipToFile("D:/upload/zip/tupian", "D:/upload");
List<String> list = new ArrayList<>();
list= ZipUtil.getFiles(new File("D:/upload/zip/tupian"),list);
System.out.println();