Commit 17e0e77b by zhongmh

优化文件删除算法

parent 356677e7
......@@ -31,7 +31,7 @@ public class FileCleanTask {
for (File file : files) {
if (System.currentTimeMillis() - file.lastModified() > month) {
logger.warn("开始清理过期文件[{}] 创建时间[{}]",file,DateFormatUtils.format(new Date(file.lastModified()),"yyyy-MM-dd HH:mm:ss"));
systemFile.cleanFile(file);
systemFile.cleanFile(file.getName());
}
}
}
......
......@@ -79,16 +79,15 @@ public class SystemFile {
/**
* 解压文件
*
* @param fileName
* @param filename
* @param inputStream
*/
public void unzip(String fileName, InputStream inputStream) throws IOException {
public void unzip(String filename, InputStream inputStream) throws IOException {
// ArchiveEntry archiveEntry = null;
File unzipFile = new File(getWwwDir(), fileName);//临时解压目录
File unzipFile = new File(getWwwDir(), filename);//临时解压目录
logger.info("解压目录:{}", unzipFile);
if (unzipFile.exists()) {
File currentFile = new File(unzipFile.getPath());
cleanFile(currentFile);
cleanFile(filename);
}
unzipFile.mkdirs();
SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(IOUtils.toByteArray(inputStream));
......@@ -110,26 +109,36 @@ public class SystemFile {
zipFile.close();
}
public void cleanFile(File currentFile) {
public void cleanFile(String filename) {
File currentFile = new File(wwwDir, filename);
if (!currentFile.isDirectory()) {
currentFile.deleteOnExit();
}
Stack<File> fileStack = new Stack<File>();
fileStack.push(currentFile);
do {
File[] listFiles = currentFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
fileStack.pop().delete();
} else {
currentFile = fileStack.peek();
for (File file : listFiles) {
if (file.isDirectory()) {
fileStack.push(file);
} else {
file.delete();
while (fileStack.size() > 0 && (currentFile = fileStack.pop()) != null) {
//如果是文件夹,继续寻找文件夹下的东西
if (currentFile.isDirectory()) {
File[] listFiles = currentFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
// 说明是空文件夹直接删除
currentFile.deleteOnExit();
} else{
fileStack.push(currentFile);
for (File file : listFiles) {
if (file.isDirectory() && file.listFiles().length > 0) {
//将找到的文件夹压入栈中
fileStack.push(file);
} else {
//如果是文件直接删除
file.delete();
}
}
}
} else {
currentFile.deleteOnExit();
}
} while (fileStack.size() > 0);
}
}
}
......@@ -2,6 +2,8 @@ package cn.yunmaozj.controller;
import cn.yunmaozj.common.SystemFile;
import cn.yunmaozj.security.Developer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
......@@ -22,6 +24,7 @@ import java.util.*;
@Controller
public class IndexController {
private final Logger logger = LoggerFactory.getLogger(IndexController.class);
@Autowired
private SystemFile systemFile;
......@@ -54,8 +57,8 @@ public class IndexController {
@ResponseBody
@GetMapping("del/{project}")
public String del(@PathVariable("project") String project) throws IOException {
File file = new File(systemFile.getWwwDir(), project);
systemFile.cleanFile(file);
logger.info("当前正在删除项目:{}",project);
systemFile.cleanFile(project);
return index();
}
}
package cn.boliyuan.security;
import cn.yunmaozj.common.SystemFile;
import org.junit.Test;
import java.io.File;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-10-28 11:44
*/
public class SystemFileTest {
public void cleanFile(){
SystemFile systemFile = new SystemFile("E:\\tmp");
systemFile.cleanFile("logs");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment