简介: 一、文件压缩,是很有必要的,我们在进行文件,传输过程中,很多时候都是,都是单个文件单个文件发送接收,但是当数据量特别大,或者文件数量比较多的时候,这个时候就可以考虑文件压缩。 二、优势:文件压缩过后,只需要进行一次文件的传输就可以了。
一、文件压缩,是很有必要的,我们在进行文件,传输过程中,很多时候都是,都是单个文件单个文件发送接收,但是当数据量特别大,或者文件数量比较多的时候,这个时候就可以考虑文件压缩。
二、优势:文件压缩过后,只需要进行一次文件的传输就可以了。减少频繁发送的问题。缺点:文件大小会变大,如果传输过程中断了,风险较大。
package com.global.boss.utils.io;
import org.apache.commons.io.IOUtils;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Iterator;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 文件工具类
*
* @author star
*/
public class FileUtil {
private final static String WINDOWS_TEMP_DIR = "C:/temp";
private final static String LINUX_TEMP_DIR = "/var/tmp";
/**
* 创建随机文件夹
*
* @return 返回文件夹信息
*/
public static String createDir(String filePath) {
long currentTimeMillis = System.currentTimeMillis();
filePath = filePath + "/" + currentTimeMillis + "-" + UUID.randomUUID() + "-temp";
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
return filePath;
}
/**
* 删除文件夹
* @param dirPath 文件夹路径
*/
public static void deleteDir(String dirPath) {
File file = new File(dirPath);
if (file.exists()) {
if (file.isDirectory()) {
String[] files = file.list();
for (String path : files) {
String currPath = dirPath + "\\" + path;
File deleteFile = new File(currPath);
if (deleteFile.isDirectory()) {
deleteDir(currPath);
}
deleteFile.delete();
}
file.delete();
} else {
file.delete();
}
}
}
public static byte[] file2byte(File tradeFile) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 获取临时目录文件夹
*
* @return
*/
public static String getTempDir() {
if (isWindows()) {
return getWindowsOsTempDir();
} else {
return getLinuxOsTempDir();
}
}
private static String getWindowsOsTempDir() {
return WINDOWS_TEMP_DIR;
}
private static String getLinuxOsTempDir() {
return LINUX_TEMP_DIR;
}
public static boolean isWindows() {
String osName = System.getProperty("os.name");
return StringUtil.hasText(osName) && !"LINUX".equalsIgnoreCase(osName);
}
/**
* 提供给用户使用的基本压缩类
*
* @param srcPath 被压缩文件夹路径
* @param outPath 输出路径
* @throws IOException
*/
public static void compressFile(String srcPath, String outPath) throws IOException {
//读取源文件
File srcFile = new File(srcPath);
//判断输出路径是否正确
File outFile = new File(outPath);
//如果只是路劲加入对应的压缩名称
if (outFile.isDirectory()) {
//用"/"作文判断标准
if (outPath.endsWith(File.separator)) {
outPath += srcFile.getName().split("\\.")[0] + ".zip";
} else {
outPath += File.separator + srcFile.getName().split("\\.")[0] + ".zip";
}
}
//读取文件流
FileOutputStream fileOutputStream = new FileOutputStream(outPath);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
//压缩文件
compressFile(srcFile, srcFile.getName(), zipOutputStream);
//关闭流
zipOutputStream.close();
fileOutputStream.close();
}
/**
* 迭代方式进行文件压缩
*
* @param file
* @param fileName
* @param outputStream
* @throws IOException
*/
private static void compressFile(File file, String fileName, final ZipOutputStream outputStream) throws IOException {
//如果是目录
if (file.isDirectory()) {
//创建文件夹
outputStream.putNextEntry(new ZipEntry(fileName + "/"));
//迭代判断,并且加入对应文件路径
File[] files = file.listFiles();
Iterator<File> iterator = Arrays.asList(files).iterator();
while (iterator.hasNext()) {
File f = iterator.next();
compressFile(f, fileName + "/" + f.getName(), outputStream);
}
} else {
//创建文件
outputStream.putNextEntry(new ZipEntry(fileName));
//读取文件并写出
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] bytes = new byte[1024];
int n;
while ((n = bufferedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, n);
}
//关闭流
fileInputStream.close();
bufferedInputStream.close();
}
}
}