一、压缩文件
String filePath = RuoYiConfig.getUploadPath()+GraphicsConstant.SAVE_IMAGE_PATH;
String fileName = Seq.getId(Seq.uploadSeqType)+".zip";
String url = filePath+fileName;
File zipFile = new File(url);
ZipMultiFileUtil.zipFiles(fileList.stream().toArray(File[]::new),zipFile);
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
*压缩工具类
*/
public class ZipMultiFileUtil {
/**
* 压缩文件
* @param srcFiles
* @param zipFile
*/
public static void zipFiles(File[] srcFiles, File zipFile) {
try{
if (srcFiles.length != 0){
//判断压缩后的文件存在不,不存在则创建
if (!zipFile.exists()){
zipFile.createNewFile();
}else{
zipFile.delete();
zipFile.createNewFile();
}
//创建FileInputStream对象
FileInputStream fileInputStream = null;
//实例化FileOutputStream对象
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
//实例化ZipOutputStream对象
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
//创建ZpEntry对象
ZipEntry zipEntry = null;
//遍历源文件数组
for (int i = 0;i <srcFiles.length;i++){
//将源文件数组中的当前文件读入FileInputStream流中
fileInputStream = new FileInputStream(srcFiles[i]);
//实例化ZipEntry对象,源文件数组中的当前文件
zipEntry = new ZipEntry(srcFiles[i].getName());
zipOutputStream.putNextEntry(zipEntry);
//该变量记录每次真正读的字节个数
int len;
//定义每次读取的字节数组
byte[] buffer = new byte[1024];
while ((len = fileInputStream.read(buffer)) > 0){
zipOutputStream.write(buffer,0,len);
}
}
zipOutputStream.closeEntry();
zipOutputStream.close();
fileInputStream.close();
fileOutputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
二、解压文件
import com.github.junrar.Archive;
import com.github.junrar.UnrarCallback;
import com.github.junrar.rarfile.FileHeader;
import com.imagegeneration.common.config.RuoYiConfig;
import com.imagegeneration.common.utils.uuid.Seq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.util.List;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
* 解压文件
*/
@Slf4j
public class UnZipUtils {
public static final String PREFIX_PATH = "/opt/application/zip/";
/**
* 获取随机数
* @return
*/
private static int getRandom(){
Random random = new Random();
return random.nextInt(10000);
}
/**
* 复制压缩文件到本地
*/
public static File copyZipFile(InputStream inputStream,String fileName) throws IOException {
StringBuffer sb = new StringBuffer();
long timeMillis = System.currentTimeMillis();
String randomPath = sb.append(PREFIX_PATH).append(timeMillis).append("_")
.append(getRandom()).toString();
File zipFile = new File(randomPath+File.separator+fileName);
FileUtils.copyInputStreamToFile(inputStream,zipFile);
return zipFile;
}
/**
* 删除压缩文件
*/
public static void deleteZipFile(File zipFile){
if (zipFile.exists()){
zipFile.delete();
}
}
/**
* 解压7z文件
*/
public static File un7z(String filePath) throws IOException {
File srcFile = new File(filePath);
String parentPath = RuoYiConfig.getUnZipFilePath()+"\\"+ Seq.getId(Seq.uploadSeqType);
SevenZFile zIn = new SevenZFile(srcFile);
SevenZArchiveEntry entry;
while ((entry = zIn.getNextEntry()) != null){
if (!entry.isDirectory()){
String path = entry.getName();
log.info("path="+path);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len = -1;
byte[] buf = new byte[1024];
while ((len = zIn.read(buf)) != -1){
out.write(buf,0,len);
}
InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
String[] pathArray = path.split("/");
String fileName = pathArray[pathArray.length-1];
log.info(fileName);
String newFileName = "";
if (entry.isDirectory()){
newFileName = parentPath+"\\"+path+"\\"+fileName;
}else{
newFileName = parentPath+"\\"+path;
}
File destFile = new File(newFileName);
FileUtils.copyInputStreamToFile(inputStream,destFile);
inputStream.close();
}
}
zIn.close();
return new File(parentPath);
}
/**
* 解压rar文件
*/
public static File unrar(UnrarCallback callback,String filePath) throws Exception {
File file = new File(filePath);
String parentPath = RuoYiConfig.getUnZipFilePath()+"\\"+ Seq.getId(Seq.uploadSeqType);
Archive archive = new Archive(file,callback);
if (archive.isEncrypted()){
throw new Exception("File IS ENCRYPTED!");
}
List<FileHeader> files = archive.getFileHeaders();
for (FileHeader fh:files){
if (fh.isEncrypted()){
throw new Exception("File is encrypted!");
}
if (!fh.isDirectory()){
String path = fh.getFileNameString();
log.info("path="+path);
InputStream inputStream = archive.getInputStream(fh);
String[] pathArray = path.split("\\\\");
String fileName = pathArray[pathArray.length-1];
log.info(fileName);
String newFileName = "";
if (fh.isDirectory()){
newFileName = parentPath+"\\"+path+"\\"+fileName;
}else{
newFileName = parentPath+"\\"+path;
}
File destFile = new File(newFileName);
FileUtils.copyInputStreamToFile(inputStream,destFile);
inputStream.close();
}
}
return new File(parentPath);
}
/**
* 解压zip文件
*/
public static File unzip(String filePath){
File file = new File(filePath);
String parentPath = RuoYiConfig.getUnZipFilePath()+"\\"+
Seq.getId(Seq.uploadSeqType);
log.info("parentPath:"+parentPath);
ZipFile zipFile = null;
ZipInputStream zin = null;
FileInputStream fis = null;
try{
zipFile = new ZipFile(file);
fis = new FileInputStream(file);
zin = new ZipInputStream(fis);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null){
String path = ze.getName();
log.info("path="+path);
if (!ze.isDirectory()){
InputStream inputStream = zipFile.getInputStream(ze);
String[] pathArray = path.split("/");
String fileName = pathArray[pathArray.length-1];
log.info(fileName);
String newFileName = "";
if (ze.isDirectory()){
newFileName = parentPath+"\\"+path+"\\"+fileName;
}else{
newFileName = parentPath+"\\"+path;
}
File destFile = new File(newFileName);
FileUtils.copyInputStreamToFile(inputStream,destFile);
inputStream.close();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if (zin != null){
zin.closeEntry();
zin.close();
}
if (fis != null){
fis.close();
}
if (zipFile != null){
zipFile.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
return new File(parentPath);
}
}
三、指定文件夹及子文件删除
/**
* 删除文件夹及子文件
* @param file
* @return
*/
public static Boolean deleteFile(File file){
if (file == null || !file.exists()){
return false;
}
File[] files = file.listFiles();
if (files != null){
for (File f:files){
if (f.isDirectory()){
deleteFile(f);
}else{
f.delete();
}
}
}
file.delete();
return true;
}
四、指定文件夹下的子文件的查找。
/**
* 获取文件下的文本文件
* @param file
* @param fileList
* @return
*/
public static List<File> getFileData(File file, List<File> fileList) {
if (file.isDirectory() && Objects.requireNonNull(file.listFiles()).length > 0){
for (File file1 : Objects.requireNonNull(file.listFiles())){
if (file1.isDirectory() && Objects.requireNonNull(file1.listFiles()).length > 0){
getFileData(file1,fileList);
}else if (file1.isFile()){
fileList.add(file1);
}
}
}else if (file.isFile()){
fileList.add(file);
}
return fileList;
}