1.压缩文件夹下所有文件,按原目录输出
2.压缩文件list,List<File>,按照单个文件组成压缩包
代码如下文件,按照second 标注
package com.juanpi.service.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CustomFileUtil {
private static Logger logger = LoggerFactory.getLogger(CustomFileUtil.class);
/**
*
* @param inputFileName
* 输入一个文件夹(磁盘路径)
* @param zipFileName
* 输出一个压缩文件夹,打包后文件名字
* @throws Exception
*/
public static void zip(String inputFileName, String zipFileName) throws Exception {
zip(zipFileName, new File(inputFileName));
}
private static void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
} catch (Exception e) {
logger.error("compress file error" + e.getMessage(), e);
} finally {
if (out != null) {
out.closeEntry(); // Closes the current ZIP entry
out.close();
}
}
}
private static void zip(ZipOutputStream out, File f, String base) throws Exception {
FileInputStream in = null;
try {
if (f.isDirectory()) { // 判断是否为目录
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else { // 压缩目录中的所有文件
out.putNextEntry(new ZipEntry(base));
in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
}
} catch (Exception e) {
logger.error("compress file error" + e.getMessage(), e);
} finally {
if (in != null) {
in.close();
}
}
}
/**
* 下载文件
*
* @param file
* @param response
*/
public static void downloadFile(File file, HttpServletResponse response, boolean isDelete) {
BufferedInputStream fis = null;
OutputStream toClient = null;
try {
// 以流的形式下载文件。
fis = new BufferedInputStream(new FileInputStream(file.getPath()));
// 清空response
response.reset();
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
byte[] buffer = new byte[1024 * 1024];
int byteRead = 0;
while ((byteRead = fis.read(buffer)) != -1) {
toClient.write(buffer, 0, byteRead);
}
toClient.flush();
if (isDelete) {
file.delete(); // 是否将生成的服务器端文件删除
}
} catch (IOException ex) {
logger.error("compress file error" + ex.getMessage(), ex);
} finally {
try {
if (fis != null) {
fis.close();
}
if (toClient != null) {
toClient.close();
}
} catch (IOException e) {
logger.error("compress file error" + e.getMessage(), e);
}
}
}
/**
* @date 2016-10-18
* @author juanzi the second compress fold method begin
* @param path
* @param fileName
* 创建文件
*/
public static void createFile(String path, String fileName) {
File f = new File(path);
File file = new File(f, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
logger.error("zipFile createFile error:", e.getMessage(), e);
}
}
}
/**
* 压缩文件列表中的文件 the second compress fold method
*
* @param files
* @param outputStream
* @throws IOException
*/
public static void zipFile(List<File> files, ZipOutputStream outputStream) {
try {
int size = files.size();
// 压缩列表中的文件
for (int i = 0; i < size; i++) {
File file = (File) files.get(i);
if (file.exists()) {
zipFile(file, outputStream);
}
}
} catch (IOException e) {
logger.error("zipFile IOException error:", e.getMessage(), e);
} catch (ServletException e) {
logger.error("zipFile ServletException error:", e.getMessage(), e);
}
}
/**
* 将文件写入到zip文件中 the second compress fold method
*
* @param inputFile
* @param outputstream
* @throws Exception
*/
public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, ServletException {
FileInputStream inStream = null;
BufferedInputStream bInStream = null;
try {
if (inputFile.isFile()) {
inStream = new FileInputStream(inputFile);
bInStream = new BufferedInputStream(inStream);
ZipEntry entry = new ZipEntry(inputFile.getName());
outputstream.putNextEntry(entry);
final int MAX_BYTE = 2 * 1024 * 1024; // 最大的流为2M
long streamTotal = 0; // 接受流的容量
int streamNum = 0; // 流需要分开的数量
int leaveByte = 0; // 文件剩下的字符数
byte[] inOutbyte; // byte数组接受文件的数据
streamTotal = bInStream.available(); // 通过available方法取得流的最大字符数
streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分开的数量
leaveByte = (int) streamTotal % MAX_BYTE; // 分开文件之后,剩余的数量
if (streamNum > 0) {
for (int j = 0; j < streamNum; ++j) {
inOutbyte = new byte[MAX_BYTE];// 读入流,保存在byte数组
bInStream.read(inOutbyte, 0, MAX_BYTE);
outputstream.write(inOutbyte, 0, MAX_BYTE); // 写出流
}
}
inOutbyte = new byte[leaveByte];// 写出剩下的流数据
bInStream.read(inOutbyte, 0, leaveByte);
outputstream.write(inOutbyte);
outputstream.closeEntry(); // Closes the current ZIP entry
}
} catch (IOException e) {
logger.error("zipFile IOException error:", e.getMessage(), e);
} finally {
if (bInStream != null) {
bInStream.close(); // 关闭
}
if (inStream != null) {
inStream.close();
}
}
}
}