package com.dsfa.bi.dp.web.fun.tools;
import com.dsfa.platform.starter.session.kit.DateKit;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* @author Hunag Zicai
* @date 2022/8/10 18:00
* @desc
**/
@Slf4j
public class DPFileTools {
public static void main(String[] args) throws IOException {
//DPFileTools.stringContentToFile("code> print('hello world!')", "test.py", "F:\\temp\\df\\dgdfg\\dgfdsfg", false);
//String folder = DPFileTools.addFolder("F:\\temp", "python");
//DPFileTools.stringContentToFile("sidfhkasfhkasf","xxx.py",folder,false);
//DPFileTools.deleteFolder("F:\\temp");
//DPFileTools.unzip("F:\\temp\\python.zip", "F:\\temp\\generate");
}
/**
* 将绝对路径下的压缩包文件解压到指定路径,如果之前存在文件夹,则覆盖
*
* @param filePath 文件绝对路径
* @param toPath 指定路径
*/
public static void unzip(String filePath, String toPath) throws IOException {
File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
throw new RuntimeException(filePath + "文件不存在");
}
ZipFile zip = new ZipFile(file, Charset.forName("GBK"));
File pathFile = new File(toPath );
if (!pathFile.exists()) {
pathFile.mkdirs();
}
for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (toPath + File.separator + zipEntryName).replaceAll("\\*", File.separator);
// 判断解压目标路径是否存在,不存在则创建文件路径
File toPathFile = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if (!toPathFile.exists()) {
toPathFile.mkdirs();
}
// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) {
continue;
}
FileOutputStream out = new FileOutputStream(outPath);
byte[] bytes = new byte[1024];
int len;
while ((len = in.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
in.close();
out.close();
}
log.debug("\n解压完成\n");
}
/**
* 删除指定路径的文件夹及其子文件
*
* @param path 指定路径
*/
public static void deleteFolder(String path) {
File file = new File(path);
if (!file.exists()) {
return;
} else if (!file.isDirectory()) {
//非目录即文件,直接删除
file.delete();
return;
}
//递归删除子文件
String[] tempList = file.list();
File tempFile = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
tempFile = new File(path + tempList[i]);
} else {
tempFile = new File(path + File.separator + tempList[i]);
}
if (tempFile.isFile()) {
tempFile.delete();
}
if (tempFile.isDirectory()) {
deleteFolder(path + File.separator + tempList[i]);//再删除空文件夹
}
}
file.delete();
}
/**
* 在指定路径生成一个文件夹
*
* @param path 指定路径
* @param folderName 要生成的文件夹名称
*/
public static String addFolder(String path, String folderName) {
String folderPath = path + File.separator + folderName;
File file = new File(folderPath);
if (file.exists()) {
log.debug("该路径已存在:{}", folderPath);
} else {
file.mkdirs();
}
return folderPath;
}
/**
* 将string内容保存成文件到指定路径
*
* @param content 字符内容
* @param fileName 生成的文件名
* @param path 保存的路径
* @param append 如果路径存在同名文件内容是否追加到后面
*/
public static void stringContentToFile(String content, String fileName, String path, boolean append) throws IOException {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
StringBuilder code = new StringBuilder();
code.append("###### Path:" + path + "\n");
code.append("###### Date:" + DateKit.getNowTime() + "\n\n");
code.append(content);
code.append("\n\n###############################################################################\n");
FileWriter fileWriter = null;
if (append) {
fileWriter = new FileWriter(path + File.separator + fileName, true);
} else {
fileWriter = new FileWriter(path + File.separator + fileName);
}
fileWriter.write(code.toString());
fileWriter.flush();
fileWriter.close();
}
}