@Slf4j
@Component
public class FileUtils {
/**
* 文件夹复制到指定的文件夹
*/
@SneakyThrows
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 {
Files.copy(file.toPath(), inFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
}
/**
* 删除文件或文件夹(包括文件夹下的文件)
*/
@SneakyThrows
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)
*/
@SneakyThrows
public static void copyFile(File source, File target) {
if (!target.exists()) {
boolean newFile = target.createNewFile();
}
try (FileInputStream fileInputStream = new FileInputStream(source);
FileOutputStream fileOutputStream = new FileOutputStream(target)) {
int len;
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文件夹压缩成 zip 包
* @param source 要压缩的文件夹
* @param target 压缩后存放路径
*/
public static String zipDir(File source, File target) {
try {
if (!target.exists()) {
boolean mkdirs = target.mkdirs();
}
File targetFile = new File(target, source.getName() + ".zip");
FileOutputStream fos = new FileOutputStream(targetFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(fos);
zipSource(source, source.getName(), zipOutputStream);
zipOutputStream.close();
fos.close();
return targetFile.getCanonicalPath();
} catch (Exception e) {
log.error("压缩文件夹失败:{}", ExceptionUtil.stacktraceToString(e));
return "";
}
}
// 将目标文件夹中的一个个文件进行压缩,保留其目录结构
private static void zipSource(File source, String name, ZipOutputStream zipOutputStream) throws IOException {
if (source.isDirectory()) {
// 继续遍历
for (File file : Objects.requireNonNull(source.listFiles())) {
zipSource(file, name + "/" + file.getName(), zipOutputStream);
}
} else {
FileInputStream fileInputStream = new FileInputStream(source);
zipOutputStream.putNextEntry(new ZipEntry(name));
int len;
byte[] bytes = new byte[4096];
while ((len = fileInputStream.read(bytes)) != -1) {
zipOutputStream.write(bytes, 0, len);
}
zipOutputStream.closeEntry();
fileInputStream.close();
}
}
public static void main(String[] args) {
File source = new File("C:\\Users\\16372\\Documents\\笔记");
File target = new File("C:\\Users\\16372");
// copyDir(source, target);
// removeFile(target);
//copyDir(new File("C:\\Users\\16372\\Documents\\干饭人.pdf"),new File("C:\\Users\\16372"));
String s = zipDir(source, target);
System.out.println(s);
}
}
10-22
881
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)