一、背景
最近在做一个项目,里面有一个一次性下载多张证书的接口,我需要将多张证书返回给前端,然后我就想到是否可以把证书文件压缩到一个zip文件,讨论之后就采用了我的想法。之后就想着怎么才能方便的写入呢?实现如下:
二、代码实现
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
private static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
/**
* 获取指定路径下的文件,如果没有就直接创建一个
* @param path 文件夹名称,可空
* @param fileName 文件名,非空
* @return 返回指定w文件
* @throws IOException 读取异常
*/
public static File getFile(String path, String fileName) throws IOException {
File file;
if (path != null && !path.equals("")) {
file = new File(path, fileName);
} else {
file = new File(fileName);
}
if (!file.exists()) {
file.createNewFile();
}
return file;
}
/**
* 获取文件输出流
* @param file 文件
* @return 文件输出流
* @throws FileNotFoundException 文件未找到异常
*/
public static FileOutputStream getFileStream(File file) throws FileNotFoundException {
return new FileOutputStream(file);
}
/**
* 将文件夹中的文件打包到zip文件中
* @param folder 待压缩文件夹
* @param zipPath zip存放路径
* @param zipName zip文件名
* @return zip文件
* @throws IOException 输出异常
*/
public static File getZipFile(String folder,String zipPath,String zipName) throws IOException {
File file = new File(folder);
if(!file.exists()){
return null;
}
File[] files = file.listFiles();
List<File> fileList = Arrays.asList(files);
return getZipFile(fileList,zipPath,zipName);
}
/**
* 将批量文件加入到zip文件中
* @param fileList 文件列
* @param zipFilePath zip文件存放路径
* @param zipFileName zip文件名
* @return zip文件
* @throws IOException 输入输出异常
*/
public static File getZipFile(List<File> fileList, String zipFilePath, String zipFileName) throws IOException {
reentrantReadWriteLock.writeLock().lock();
File zipFile = getFile(zipFilePath, zipFileName);
try (
FileOutputStream outputStream = getFileStream(zipFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
){
int size = fileList.size();
fileList.forEach(file->{
try {
zipFile(file,zipOutputStream);
file.delete();
} catch (IOException e) {
System.out.println("文件处理异常");
}
});
zipOutputStream.flush();
outputStream.flush();
return zipFile;
} catch (Exception e) {
throw new IOException(e);
} finally {
reentrantReadWriteLock.writeLock().unlock();
}
}
/**
* 将文件写入zip
* @param file 需要写入到zip文件的
* @param zipOutputStream
* @throws IOException
*/
private static void zipFile(File file, ZipOutputStream zipOutputStream) throws IOException {
if (file.exists() && file.isFile()) {
try (
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
){
ZipEntry entry = new ZipEntry(file.getName());
zipOutputStream.putNextEntry(entry);
long streamTotal = 0L;
streamTotal = (long)bis.available();
int streamNum = (int)Math.floor((double)(streamTotal / 10485760L));
int leaveByte = (int)(streamTotal % 10485760L);
byte[] buffer;
if (streamNum > 0) {
for(int i = 0; i < streamNum; ++i) {
buffer = new byte[10485760];
bis.read(buffer, 0, 10485760);
zipOutputStream.write(buffer, 0, 10485760);
}
}
buffer = new byte[leaveByte];
bis.read(buffer, 0, leaveByte);
zipOutputStream.write(buffer, 0, leaveByte);
zipOutputStream.closeEntry();
}catch (Exception e){
throw new IOException(e);
}
}
}
public static void main(String[] args) throws IOException {
File file = new File("F:\\2345Downloads\\综述\\Desktop\\颈动脉");
File[] files = file.listFiles();
List<File> files1 = Arrays.asList(files);
getZipFile(files1,"F:\\2345Downloads\\综述\\Desktop\\颈动脉","test.zip");
}
}
上述方法有一个缺陷,不能打包文件,不能直接将文件夹下的文件按照原来的路径存放到zip文件中。
三、执行结果
来源:CSDN
作者:笑不语
链接:https://blog.csdn.net/weixin_43935907/article/details/103605586