解压文件时,可以采用多线程的方式,下面上代码: 创建类
@Slf4j
public class FileOperation {
private static Executor executor = Executors.newFixedThreadPool(20);
public static File newFile(String fileUrl) {
File file = new File(fileUrl);
return file;
}
删除文件:
public static boolean deleteFile(File file) {
if (file == null) {
return false;
}
if (!file.exists()) {
return false;
}
if (file.isFile()) {
return file.delete();
} else {
for (File newfile : file.listFiles()) {
deleteFile(newfile);
}
}
return file.delete();
}
public static boolean deleteFile(String fileUrl) {
File file = newFile(fileUrl);
return deleteFile(file);
}
public static boolean deleteFile(String fileUrl) {
File file = newFile(fileUrl);
return deleteFile(file);
}
创建目录
public static boolean mkdir(File file) {
if (file == null) {
return false;
}
if (file.exists()) {
return true;
}
return file.mkdirs();
}
public static boolean mkdir(String fileUrl) {
if (fileUrl == null) {
return false;
}
File file = newFile(fileUrl);
if (file.exists()) {
return true;
}
return file.mkdirs();
}
拷贝文件
public static void copyFile(FileInputStream fileInputStream, FileOutputStream fileOutputStream) throws IOException {
try {
byte[] buf = new byte[4096];
int len = fileInputStream.read(buf);
while (len != -1) {
fileOutputStream.write(buf, 0, len);
len = fileInputStream.read(buf);
}
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void copyFile(File src, File dest) throws IOException {
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest);
copyFile(in, out);
}
public static void copyFile(String srcUrl, String destUrl) throws IOException {
if (srcUrl == null || destUrl == null) {
return;
}
File srcFile = newFile(srcUrl);
File descFile = newFile(destUrl);
copyFile(srcFile, descFile);
}
解压zip文件
public static List<String> unzip(File sourceFile, String destDirPath) {
ZipFile zipFile = null;
Set<String> set = new HashSet<String>();
List<String> fileEntryNameList = new ArrayList<>();
try {
zipFile = new ZipFile(sourceFile, Charset.forName("GBK"));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String[] nameStrArr = entry.getName().split("/");
String nameStr = "/";
for (int i = 0; i < nameStrArr.length; i++) {
if (!"".equals(nameStrArr[i])) {
nameStr = nameStr + "/" + nameStrArr[i];
set.add(nameStr);
}
}
log.info("解压" + entry.getName());
String zipPath = "/" + entry.getName();
fileEntryNameList.add(zipPath);
if (entry.isDirectory()) {
String dirPath = destDirPath + File.separator + entry.getName();
File dir = FileOperation.newFile(dirPath);
dir.mkdir();
} else {
File targetFile = new File(destDirPath + "/" + entry.getName());
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
InputStream is = null;
FileOutputStream fos = null;
try {
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[2048];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (Exception e) {
if (fos != null) {
try {
fos.close();
} catch (Exception e1) {
log.error("关闭流失败:" + e1);
}
}
if (is != null) {
try {
is.close();
} catch (Exception e2) {
log.error("关闭流失败:" + e2);
}
}
}
}
}
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
for (String zipPath : fileEntryNameList) {
executor.execute(new Runnable() {
@Override
public void run() {
if (FileUtil.isImageFile(FileUtil.getFileExtendName(zipPath))) {
File file = new File(destDirPath + zipPath);
File minFile = new File(destDirPath + FileUtil.getFileNameNotExtend(zipPath) + "_min." + FileUtil.getFileExtendName(zipPath));
try {
ImageOperation.thumbnailsImage(file, minFile, 300);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
List<String> res = new ArrayList<>(set);
return res;
}
其中ImageOperation.thumbnailsImage(file, minFile, 300);方法如下:
import net.coobird.thumbnailator.Thumbnails;
import java.io.File;
import java.io.IOException;
public class ImageOperation {
public static void thumbnailsImage(File inFile, File outFile, int imageSize) throws IOException {
Thumbnails.of(inFile).size(imageSize, imageSize)
.toFile(outFile);
}
}
上面判断是否是图片文件的FileUtil.isImageFile方法:
public class FileUtil {
public static final String[] IMG_FILE = {"bmp", "jpg", "png", "tif", "gif", "jpeg"};
public static final String[] DOC_FILE = {"doc", "docx", "ppt", "pptx", "xls", "xlsx", "txt", "hlp", "wps", "rtf", "html", "pdf"};
public static final String[] VIDEO_FILE = {"avi", "mp4", "mpg", "mov", "swf"};
public static final String[] MUSIC_FILE = {"wav", "aif", "au", "mp3", "ram", "wma", "mmf", "amr", "aac", "flac"};
public static final int IMAGE_TYPE = 1;
public static final int DOC_TYPE = 2;
public static final int VIDEO_TYPE = 3;
public static final int MUSIC_TYPE = 4;
public static final int OTHER_TYPE = 5;
public static final int SHARE_FILE = 6;
public static final int RECYCLE_FILE = 7;
public static String getFileExtendName(String fileName) {
if (fileName.lastIndexOf(".") == -1) {
return "";
}
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
public static boolean isImageFile(String extendName) {
for (int i = 0; i < IMG_FILE.length; i++) {
if (extendName.equalsIgnoreCase(IMG_FILE[i])) {
return true;
}
}
return false;
}
}
|