package org.springblade.common.utils;
|
|
import java.io.*;
|
import java.nio.channels.FileChannel;
|
import java.util.List;
|
import net.lingala.zip4j.core.ZipFile;
|
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
|
public class FileUtil {
|
|
public static void clearFiles(String workspaceRootPath) {
|
File file = new File(workspaceRootPath);
|
deleteFile(file);
|
}
|
|
public static void deleteFile(File file) {
|
if (file.exists()) {
|
if (file.isDirectory()) {
|
File[] files = file.listFiles();
|
for (int i = 0; i < files.length; i++) {
|
deleteFile(files[i]);
|
}
|
}
|
}
|
file.delete();
|
}
|
|
public static void fileWrite(String str, String fileNamePath) throws IOException {
|
FileWriter writer = null;
|
try {
|
File file = new File(fileNamePath);
|
if (!file.getParentFile().exists()) {
|
file.getParentFile().mkdirs();
|
file.createNewFile();
|
}
|
writer = new FileWriter(file, true);
|
writer.write(str + System.getProperty("line.separator"));
|
|
} catch (IOException e) {
|
} finally {
|
if (writer != null) {
|
writer.close();
|
}
|
}
|
}
|
|
|
public static File mkFile(String fileName) {
|
File f = new File(fileName);
|
try {
|
if (f.exists()) {
|
f.delete();
|
}
|
f.createNewFile();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return f;
|
}
|
|
|
public static void copyDirAndFile(String oldPath, String newPath) throws IOException {
|
if (!(new File(newPath)).exists()) {
|
(new File(newPath)).mkdir();
|
}
|
File file = new File(oldPath);
|
//file name list
|
String[] filePaths = file.list();
|
for (String filePath : filePaths) {
|
String oldFullPath = oldPath + file.separator + filePath;
|
String newFullPath = newPath + file.separator + filePath;
|
File oldFile = new File(oldFullPath);
|
File newFile = new File(newFullPath);
|
if (oldFile.isDirectory()) {
|
copyDirAndFile(oldFullPath, newFullPath);
|
} else if (oldFile.isFile()) {
|
copyFile(oldFile, newFile);
|
}
|
}
|
}
|
|
public static void copyFile(File source, File dest) throws IOException {
|
FileChannel inputChannel = null;
|
FileChannel outputChannel = null;
|
try {
|
inputChannel = new FileInputStream(source).getChannel();
|
outputChannel = new FileOutputStream(dest).getChannel();
|
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
|
} finally {
|
inputChannel.close();
|
outputChannel.close();
|
}
|
}
|
|
/**
|
*
|
* 解压zip 包
|
* @author panchaoyuan
|
* @param srcFile Unzipped file
|
* @param destDirPath Unzipped destination folder
|
* @throws RuntimeException
|
* @throws IOException
|
*/
|
public static void unZip(MultipartFile srcFile, String destDirPath,String savePath) throws RuntimeException, IOException {
|
File file = null;
|
InputStream ins = srcFile.getInputStream();
|
String savaPaths = savePath+srcFile.getOriginalFilename();
|
System.out.println("savaPaths = " + savaPaths);
|
file=new File(savaPaths);
|
inputStreamToFile(ins, file);
|
|
if (!file.exists()) {
|
throw new RuntimeException(file.getPath() + ",file is not found");
|
}
|
ZipFile zipFile = null;
|
try {
|
zipFile = new ZipFile(file);
|
// zipFile.setFileNameCharset("utf-8");
|
zipFile.setFileNameCharset("gbk");
|
//解压到 destDirPath
|
zipFile.extractAll(destDirPath);
|
}catch(Exception e) {
|
throw new RuntimeException("unzip error from FileUtil", e);
|
}
|
}
|
|
|
/**
|
* 输入流转换为文件
|
* @author panchaoyuan
|
* @return
|
*/
|
private static void inputStreamToFile(InputStream ins, File file) {
|
try {
|
OutputStream os = new FileOutputStream(file);
|
int bytesRead = 0;
|
byte[] buffer = new byte[8192];
|
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
os.write(buffer, 0, bytesRead);
|
}
|
os.close();
|
ins.close();
|
}catch(Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 获取解压出来的图片
|
* @author arsn
|
* @return
|
*/
|
public List<MultipartFile> getSubFiles(String desFile,List<MultipartFile> fileList){
|
//取出图片数据遍历
|
File file = new File(desFile);
|
File[] files = file.listFiles();
|
for (File fileIndex : files) {
|
if (!fileIndex.exists()) {
|
throw new NullPointerException("Cannot find " + fileIndex);
|
} else if (fileIndex.isFile()) {
|
try {
|
//将 file 转换为 MultipartFile
|
File file1 = new File(desFile+File.separator + fileIndex.getName());
|
FileInputStream fileInputStream = new FileInputStream(file1);
|
MockMultipartFile mockMultipartFile = null;
|
try {
|
String name = fileIndex.getName();
|
mockMultipartFile = new MockMultipartFile(name,fileInputStream);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
fileList.add(mockMultipartFile);
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
}
|
} else {
|
if (fileIndex.isDirectory()) {
|
getSubFiles(fileIndex.getAbsolutePath(),fileList);
|
}
|
}
|
}
|
return fileList;
|
}
|
|
|
}
|