package xin.yangshuai.mybatisgeneratorweb.util;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* ZipUtils
*
* @author shuai
* @date 2021/7/30
*/
public class ZipUtils {
public static void main(String[] args) throws Exception {
File sourceFile = new File("D:/test/testFile");
fileToZip(sourceFile);
}
/**
* sourceFile一定要是文件夹
* 默认会在同目录下生成zip文件
*
* @param sourceFilePath
* @throws Exception
*/
public static void fileToZip(String sourceFilePath) throws Exception {
fileToZip(new File(sourceFilePath));
}
/**
* sourceFile一定要是文件夹
* 默认会在同目录下生成zip文件
*
* @param sourceFile
* @throws Exception
*/
public static void fileToZip(File sourceFile) throws Exception {
if (!sourceFile.exists()) {
throw new RuntimeException("不存在");
}
if (!sourceFile.isDirectory()) {
throw new RuntimeException("不是文件夹");
}
//zip文件生成位置
File zipFile = new File(sourceFile.getAbsolutePath() + ".zip");
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
fileToZip(zos, sourceFile, "");
zos.close();
fos.close();
}
private static void fileToZip(ZipOutputStream zos, File sourceFile, String path) throws Exception {
System.out.println(sourceFile.getAbsolutePath());
//如果是文件夹只创建zip实体即可,如果是文件,创建zip实体后还要读取文件内容并写入
if (sourceFile.isDirectory()) {
path = path + sourceFile.getName() + "/";
ZipEntry zipEntry = new ZipEntry(path);
zos.putNextEntry(zipEntry);
for (File file : sourceFile.listFiles()) {
fileToZip(zos, file, path);
}
} else {
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(path + sourceFile.getName());
zos.putNextEntry(zipEntry);
byte[] bufs = new byte[1024 * 10];
//读取待压缩的文件并写进压缩包里
FileInputStream fis = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
bis.close();
fis.close();
}
}
}
将指定的压缩包作为流发送给浏览器文章来源:https://www.toymoban.com/news/detail-560976.html
fileToZip(new File("file-management\\src\\main\\resources\\fileTemplate\\重点排放指标检测"));
OutputStream out = res.getOutputStream();
fileName = "file-management\\src\\main\\resources\\fileTemplate\\重点排放指标检测.zip";
byte[] data = getByte(fileName);
res.reset();
res.addHeader("Content-Length", ""+data.length);
res.setContentType("application/zip;charset=UTF-8");
res.setHeader("Content-Disposition","attachment;filename=重点排放指标检测");
IOUtils.write(data, out);
out.flush();
out.close();
下面粘贴一些没有用的东西,是我自已业务的逻辑,,主要逻辑是根据传入文件不同 判断将压缩包发送给浏览器 还是将xls模板发送给浏览器文章来源地址https://www.toymoban.com/news/detail-560976.html
public void download(HttpServletResponse res, RequestVo vo) {
String name = vo.getName();
try{
InputStream inputStream=null;
String fileName=null;
if (name.equals("重点排放指标检测")){
fileToZip(new File("file-management\\src\\main\\resources\\fileTemplate\\重点排放指标检测"));
OutputStream out = res.getOutputStream();
fileName = "file-management\\src\\main\\resources\\fileTemplate\\重点排放指标检测.zip";
byte[] data = getByte(fileName);
res.reset();
res.addHeader("Content-Length", ""+data.length);
res.setContentType("application/zip;charset=UTF-8");
res.setHeader("Content-Disposition","attachment;filename=重点排放指标检测");
IOUtils.write(data, out);
out.flush();
out.close();
}else{
ClassPathResource classPathResource = new ClassPathResource("fileTemplate/"+name+".xls");
inputStream =classPathResource.getInputStream();
fileName = name+".xls";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len ;
byte[] buffer = new byte[1024]; //缓冲区
while((len = inputStream.read(buffer))!=-1) { //将接受的数据写入缓冲区数组buffer
baos.write(buffer,0,len); //将缓冲区buffer写入byte数组输出流`
}
inputStream.close();
baos.close();
byte[] bytes = baos.toByteArray();
res.setCharacterEncoding("UTF-8");
// attachment是以附件的形式下载,inline是浏览器打开
res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"utf-8"));
res.setContentType("text/plain;UTF-8");
// 把二进制流放入到响应体中
ServletOutputStream os = res.getOutputStream();
os.write(bytes);
os.flush();
os.close();
}
}catch (Exception e){
throw new RuntimeException("error");
}
}
到了这里,关于java将指定文件夹按照文件目录压缩为zip压缩包的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!