1、zip压缩
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
public static void compressToZip(String sourceFilePath, String zipFilePath, String zipFilename) {
File sourceFile = new File(sourceFilePath);
File zipPath = new File(zipFilePath);
if (!zipPath.exists()) {
zipPath.mkdirs();
}
File zipFile = new File(zipPath + File.separator + zipFilename);
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
writeZip(sourceFile, "", zos);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
public static void writeZip(File file, String parentPath, ZipOutputStream zos) {
if (file.isDirectory()) {
parentPath += file.getName() + File.separator;
File[] files = file.listFiles();
for (File f : files) {
writeZip(f, parentPath, zos);
}
} else {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
zos.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[1024 * 10];
while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
zos.write(buffer, 0, len);
zos.flush();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
}
}
2、文件处理
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils;
import com.ruoyi.common.utils.StringUtils;
public class FileUtils extends org.apache.commons.io.FileUtils
{
public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
public static void writeBytes(String filePath, OutputStream os) throws IOException
{
FileInputStream fis = null;
try
{
File file = new File(filePath);
if (!file.exists())
{
throw new FileNotFoundException(filePath);
}
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int length;
while ((length = fis.read(b)) > 0)
{
os.write(b, 0, length);
}
}
catch (IOException e)
{
throw e;
}
finally
{
if (os != null)
{
try
{
os.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
if (fis != null)
{
try
{
fis.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
public static boolean deleteFile(String filePath)
{
boolean flag = false;
File file = new File(filePath);
if (file.isFile() && file.exists())
{
file.delete();
flag = true;
}
return flag;
}
public static boolean isValidFilename(String filename)
{
return filename.matches(FILENAME_PATTERN);
}
public static boolean checkAllowDownload(String resource)
{
if (StringUtils.contains(resource, ".."))
{
return false;
}
if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource)))
{
return true;
}
return false;
}
public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
{
final String agent = request.getHeader("USER-AGENT");
String filename = fileName;
if (agent.contains("MSIE"))
{
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
}
else if (agent.contains("Firefox"))
{
filename = new String(fileName.getBytes(), "ISO8859-1");
}
else if (agent.contains("Chrome"))
{
filename = URLEncoder.encode(filename, "utf-8");
}
else
{
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
{
String percentEncodedFileName = percentEncode(realFileName);
StringBuilder contentDispositionValue = new StringBuilder();
contentDispositionValue.append("attachment; filename=")
.append(percentEncodedFileName)
.append(";")
.append("filename*=")
.append("utf-8''")
.append(percentEncodedFileName);
response.setHeader("Content-disposition", contentDispositionValue.toString());
}
public static String percentEncode(String s) throws UnsupportedEncodingException
{
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
return encode.replaceAll("\\+", "%20");
}
public static void fileMove(String from, String to) throws Exception {
try {
File dir = new File(from);
File[] files = dir.listFiles();
if (files == null)
return;
File moveDir = new File(to);
if (!moveDir.exists()) {
moveDir.mkdirs();
}
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
fileMove(files[i].getPath(), to + "\\" + files[i].getName());
}
File moveFile = new File(moveDir.getPath() + "\\"
+ files[i].getName());
files[i].renameTo(moveFile);
}
} catch (Exception e) {
throw e;
}
}
public static void copyDir(String fromDir,String toDir) throws IOException {
File dirSouce = new File(fromDir);
if (!dirSouce.isDirectory()) {
return;
}
File destDir = new File(toDir);
if(!destDir.exists()){
destDir.mkdir();
}
File[]files = dirSouce.listFiles();
for (File file : files) {
String strFrom = fromDir + File.separator + file.getName();
System.out.println(strFrom);
String strTo = toDir + File.separator + file.getName();
System.out.println(strTo);
if (file.isDirectory()) {
copyDir(strFrom,strTo);
}
if (file.isFile()) {
System.out.println("正在复制文件:"+file.getName());
copyFile(strFrom,strTo);
}
}
}
public static void copyFile(String fromFile,String toFile) throws IOException{
FileInputStream in = new FileInputStream(fromFile);
FileOutputStream out = new FileOutputStream(toFile);
byte[] bs = new byte[1*1024*1024];
int count = 0;
while((count = in.read(bs))!=-1){
out.write(bs,0,count);
}
in.close();
out.flush();
out.close();
}
}
3、