package com.example.demo.utils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.core.io.ResourceLoader;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author chenlong
* @create 2025-11-18 9:49
*/
public class FileHandleUtil {
/**
* contentType
*/
public static String contentType_octet = "application/octet-stream;charset=UTF-8";
public static String contentType_multipart = "multipart/form-data";
/**
* 将resources 目录下的指定文件复制到项目所在目录(jar包内复制到包外)
* @param fileName 文件名称,例如:XXX
* @param filePath 文件路径,例如:config
*/
public void copyJarToOut(String fileName, String filePath) {
String projectPath = System.getProperty("user.dir");
File file = new File(projectPath, fileName);
if (!file.exists()) {
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
throw new RuntimeException("copyJarToOut 创建文件失败:{}",e);
}
try (InputStream is = getClass().getClassLoader().getResourceAsStream(filePath + "/" + fileName);
FileOutputStream fop = new FileOutputStream(file)) {
if (is == null) {
throw new RuntimeException("FileHandleUtil.copyJarToOut:复制的文件不存在 " + fileName);
}
// 复制io流
FileHandleUtil.baseCopyIo(is, fop);
} catch (Exception e) {
throw new RuntimeException("FileHandleUtil.copyJarToOut异常 + " + fileName, e);
}
}
}
/**
* 将resources 目录下的指定文件复制到项目所在目录(jar包内复制到包外)
* @param fileName 文件名称,例如:XXX
* @param filePath 文件路径,例如:config
*/
public static void copyJarToOutStatic(String fileName, String filePath) {
String projectPath = System.getProperty("user.dir");
File file = new File(projectPath, fileName);
if (!file.exists()) {
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
throw new RuntimeException("copyJarToOut 创建文件失败:{}",e);
}
try (InputStream is = ResourceLoader.class.getResourceAsStream("/" + filePath + "/" + fileName);
FileOutputStream fop = new FileOutputStream(file)) {
if (is == null) {
throw new RuntimeException("FileHandleUtil.copyJarToOut:复制的文件不存在 " + fileName);
}
// 复制io流
FileHandleUtil.baseCopyIo(is, fop);
} catch (Exception e) {
throw new RuntimeException("FileHandleUtil.copyJarToOut异常 + " + fileName, e);
}
}
}
/**
* 文件夹压缩成 zip 包
*
* @param source 要压缩的文件夹,例如:D:\project\demo\src
* @param target 压缩后存放路径,例如:D:\
*/
public static String zipDir(File source, File target) {
if (!target.exists()) {
boolean mkdir = target.mkdirs();
}
// zip压缩包目标文件
File targetFile = new File(target, source.getName() + ".zip");
zipSource(source, source.getName(), targetFile);
try {
return targetFile.getCanonicalPath();
} catch (IOException e) {
throw new RuntimeException("getCanonicalPath 压缩文件失败:{}",e);
}
}
// 将目标文件夹中的一个个文件进行压缩,保留其目录结构
private static void zipSource(File source, String name, File targetFile) {
if (source.isDirectory()) {
// 继续遍历
for (File file : Objects.requireNonNull(source.listFiles())) {
zipSource(file, name + "/" + file.getName(), targetFile);
}
} else {
try (FileOutputStream fos = new FileOutputStream(targetFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(fos);
FileInputStream fileInputStream = new FileInputStream(source)) {
zipOutputStream.putNextEntry(new ZipEntry(name));
// 复制io流
FileHandleUtil.baseCopyIo(fileInputStream, zipOutputStream);
} catch (Exception e) {
throw new RuntimeException("zipSource 压缩文件失败:{}",e);
}
}
}
/**
* 文件夹复制到指定的文件夹
*/
public static void copyDir(File source, File target) {
if (!target.exists()) {
boolean mkdirs = target.mkdirs();
}
if (source.isDirectory()) {
File[] files = source.listFiles();
if (files != null) {
for (File file : files) {
File inFile = new File(target, file.getName());
if (file.isDirectory()) {
copyDir(file, inFile);
} else {
try {
Files.copy(file.toPath(), inFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
/**
* 删除文件或文件夹(包括文件夹下的文件)
*/
public static void removeFile(File target) {
if (target.exists()) {
File[] files = target.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
removeFile(file);
} else {
boolean delete = target.delete();
}
}
}
boolean delete = target.delete();
}
}
/**
* 文件复制(如果目标文件存在就会替换)
* source 和 target 都是全路径包括文件名(例如:C:\Users\干饭人.pdf)
*/
public static void copyFile(File source, File target) {
if (!target.exists()) {
try {
boolean newFile = target.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
try (FileInputStream fileInputStream = new FileInputStream(source);
FileOutputStream fileOutputStream = new FileOutputStream(target)) {
FileHandleUtil.baseCopyIo(fileInputStream, fileOutputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 下载文件
* @param downLoadFile 需要下载文件
* @param response HttpServletResponse
*/
public static void downloadFile(File downLoadFile, HttpServletResponse response) {
if (!downLoadFile.exists()) {
throw new RuntimeException("下载文件不存在");
}
try (InputStream is = new FileInputStream(downLoadFile);
OutputStream os = response.getOutputStream()) {
response.setContentType(FileHandleUtil.contentType_octet);
response.setHeader("Content-Disposition", "attachment;filename=" + downLoadFile.getName());
response.setContentLengthLong(downLoadFile.length());
FileHandleUtil.baseCopyIo(is, os);
} catch (Exception e) {
throw new RuntimeException("下载文件失败:{}", e);
}
}
/**
* 复制io
* @param source 数据源
* @param target 目标
*/
public static void baseCopyIo(InputStream source, OutputStream target) throws IOException {
byte[] buffer = new byte[8192];
int length;
while ((length = source.read(buffer)) != -1) {
target.write(buffer, 0, length);
}
target.flush();
}
public static void main(String[] args) throws IOException {
// FileHandleUtil fileHandleUtil = new FileHandleUtil();
// fileHandleUtil.copyJarToOut("excelToSQL.html", "static");
// FileHandleUtil.copyJarToOut02("excelToSQL.html", "static");
File file = new File("D:\\project\\demo\\src");
File target = new File("D:\\");
String s = FileHandleUtil.zipDir(file, target);
}
}