写在前面: 我是 「扬帆向海」,这个昵称来源于我的名字以及女朋友的名字。我热爱技术、热爱开源、热爱编程。
技术是开源的、知识是共享的
。
这博客是对自己学习的一点点总结及记录,如果您对 Java、算法 感兴趣,可以关注我的动态,我们一起学习。用知识改变命运,让我们的家人过上更好的生活
。
文章目录
- 一、以zip形式批量下载文件
- 二、压缩文件的方法
由于最近忙,两个多月没有写过文章了。最近在开发的时候使用到了从服务器把文件以压缩包的形式下载到本地,在此做一总结。有需要使用的话直接可以当作工具类来使用!
一、以zip形式批量下载文件
步骤:
① 通过response对象获取OutputStream流
② 设置文件ContentType类型,会判断下载文件类型
③ 设置文件头
④ 获取zip的输出流
⑤ 获取文件路径下的所有文件夹及文件
⑥ 获取此目录下的所有文件名与目录名
⑦ 循环读取文件路径集合,获取每一个文件的路径
⑧ 通过文件路径创建文件
⑨ 将每一个文件写入zip文件压缩包内,即进行打包
⑩ 最后刷新缓冲区
代码实现:
/**
* 以zip形式批量下载文件
*
* @param response
* @param downloadName
* @param filePath
*/
public static void MultiFileZipDownload(HttpServletResponse response, String downloadName, String filePath) {
OutputStream outputStream = null;
ZipOutputStream zos = null;
try {
// 通过response对象获取OutputStream流
outputStream = response.getOutputStream();
// 设置文件ContentType类型
response.setContentType("multipart/form-data");
// 设置文件头
response.setHeader("Content-Disposition", "attachment;fileName = " + URLEncoder.encode(downloadName, "UTF-8"));
// 获取zip的输出流
zos = new ZipOutputStream(outputStream);
// 获取文件路径下的所有文件夹及文件
File dirFile = new File(filePath);
// 获取此目录下的所有文件名与目录名
String[] fileList = dirFile.list();
// 循环读取文件路径集合,获取每一个文件的路径
if (fileList != null) {
for (String fp : fileList) {
File file = new File(filePath, fp); // 通过文件路径创建文件
zipFile(file, zos); // 将每一个文件写入zip文件压缩包内,即进行打包
// 刷新缓冲区
response.flushBuffer();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (zos != null) {
zos.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、压缩文件的方法
步骤:
① 首先判断文件是否存在
② 如果是文件
③ 创建输入流进行读取文件
④ 将文件写入zip压缩包内
⑤ 进行写入文件操作
⑥最后关闭输入、输出流
⑦ 如果是文件夹,则使用穷举的方法获取文件,写入zip压缩包内
⑧ 关闭输入、输出流
代码实现:
/**
* 压缩文件的方法
*
* @param inputFile
* @param zipoutputStream
*/
public static void zipFile(File inputFile, ZipOutputStream zipoutputStream) {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
if (inputFile.exists()) { // 判断文件是否存在
if (inputFile.isFile()) { // 如果是文件
// 创建输入流读取文件
fis = new FileInputStream(inputFile);
bis = new BufferedInputStream(fis);
// 将文件写入zip压缩包内
ZipEntry zip =
new ZipEntry(inputFile.getParentFile().getName() + File.separator + inputFile.getName()); //获取文件名
zipoutputStream.putNextEntry(zip);
// 进行写入文件
byte[] bytes = new byte[1024];
long fileLength = 0;
while (fileLength < inputFile.length()) {
int count = bis.read(bytes, 0, 1024);
fileLength += count;
zipoutputStream.write(bytes, 0, count);
}
// 关闭输入输出流
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
} else { // 如果是文件夹,则使用穷举的方法获取文件,写入zip
try {
zipoutputStream.putNextEntry(new ZipEntry(inputFile.getName() + File.separator));
File[] files = inputFile.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(files[i], zipoutputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
由于水平有限,本博客难免有不足,恳请各位大佬不吝赐教!