package com.xcong.farmer.cms.common.utils;
|
|
import java.io.*;
|
import java.util.Enumeration;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipFile;
|
import java.util.zip.ZipInputStream;
|
|
/**
|
* @author wzy
|
* @date 2022-07-04
|
**/
|
public class FileUtils {
|
|
public static String path(String path) {
|
if (!path.endsWith("/")) {
|
return path + "/";
|
}
|
|
return path;
|
}
|
|
public static String path(String path, String fileName) {
|
File file = new File(path);
|
if (!file.isDirectory()){
|
return "";
|
}
|
|
String dir = path(path);
|
return dir + fileName;
|
}
|
|
public static void zipUpload(String zipPath, String templateDir) throws IOException {
|
FileInputStream fis = new FileInputStream(zipPath);
|
ZipInputStream zipIs = new ZipInputStream(fis);
|
|
ZipEntry ze = null;
|
try {
|
while ((ze = zipIs.getNextEntry()) != null) {
|
File zfile = new File(path(templateDir, ze.getName()));
|
if (ze.isDirectory()) {
|
if (!zfile.exists()) {
|
zfile.mkdirs();
|
}
|
zipIs.closeEntry();
|
} else {
|
byte[] data = new byte[1024];
|
FileOutputStream fos = new FileOutputStream(zfile);
|
int i = 0;
|
|
while ((i = zipIs.read(data)) != -1) {
|
fos.write(data, 0, i);
|
}
|
zipIs.closeEntry();
|
fos.close();
|
}
|
}
|
|
fis.close();
|
zipIs.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
public static void upload(File file, String dir) {
|
if (file.isDirectory()) {
|
File[] files = file.listFiles();
|
if (files == null) {
|
return;
|
}
|
|
for (File dirFile : files) {
|
upload(dirFile, path(dir, dirFile.getName()));
|
}
|
} else {
|
outputFile(file, dir);
|
}
|
}
|
|
public static void outputFile(File file, String outputDir) {
|
try {
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
|
int count;
|
int buffer = 1024;
|
byte[] dataByte = new byte[buffer];
|
|
FileOutputStream out = new FileOutputStream(path(outputDir, file.getName()));
|
|
BufferedOutputStream bos = new BufferedOutputStream(out, buffer);
|
while((count = bis.read(dataByte, 0, buffer)) != -1) {
|
bos.write(dataByte, 0 ,count);
|
}
|
|
bos.flush();
|
bos.close();
|
bis.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|