public class ZipUtils {
private static final Logger logger = Logger.getLogger(ZipUtils.class);
/**
* 下载zip文件流
*
* @param filePath
* @param response
* @throws Exception
*/
public static void downloadFileZip(String filePath,HttpServletResponse response) throws Exception{
ServletOutputStream outputStream = null;
FileInputStream zipfile = null;
try {
//下载zip文件
String fileSimpleName = filePath.substring(filePath.lastIndexOf("//")+1).split("//.")[0];
outputStream = response.getOutputStream();
response.setContentType("application/zip");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=/""
+ URLEncoder.encode(fileSimpleName, "UTF-8") + ".zip" + "/"");
byte[] content = new byte[1024];
zipfile = new FileInputStream(filePath);
while (zipfile.read(content) != -1) {
outputStream.write(content);
}
outputStream.flush();
outputStream.close();
zipfile.close();
}catch(Exception e) {
if(outputStream != null) outputStream.close();
if(zipfile != null) zipfile.close();
e.printStackTrace();
logger.error("文件压缩下载失败");
}
}
/**
* 对directory目录下的文件压缩,保存为指定的文件zipFile
*
* @param directory
* @param zipFile
*/
public static void zip(String directory, String zipFile) {
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
fileZip(zos, new File(directory));
} catch (Exception e) {
e.printStackTrace();
logger.error("压缩文件出错");
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 把指定文件解压缩到指定目录,支持多级目录解压
* @param zipFile 压缩文件
* @param directory 目标目录
* @throws Exception
*/
public static void unzip(String zipFile, String directory) {
if (null == directory || "".equals(directory)) { // 处理传进来的目标目录为空
directory = zipFile.substring(0, zipFile.lastIndexOf("."));
}
ZipInputStream zis = null;
try {
zis = new ZipInputStream(new FileInputStream(zipFile));
File f = new File(directory);
f.mkdirs();
fileUnZip(zis, f);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zis != null) {
try {
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 解压缩文件
*
* @param zis 压缩文件输入流
* @param file 解压后保存的文件目录
* @throws Exception
*/
private static void fileUnZip(ZipInputStream zis, File file)
throws Exception {
ZipEntry zip = zis.getNextEntry();
if (zip == null)
return;
String name = zip.getName(); // 得到压缩文件中文件名称
File f = new File(file.getAbsolutePath() + File.separator + name);
if (zip.isDirectory()) {
f.mkdirs();
fileUnZip(zis, file);
} else {
f.createNewFile();
BufferedOutputStream bfos = null;
try {
bfos = new BufferedOutputStream(new FileOutputStream(f));
byte b[] = new byte[2048];
int aa = 0;
while ((aa = zis.read(b)) != -1) {
bfos.write(b, 0, aa);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bfos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
fileUnZip(zis, file); // 解压下一个条目
}
}
/**
* 压缩文件
*
* @param zos
* @param file
* @throws Exception
*/
private static void fileZip(ZipOutputStream zos, File file) throws Exception {
if (file.isFile()) {
zos.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] bb = new byte[2048];
int aa = 0;
while ((aa = fis.read(bb)) != -1) {
zos.write(bb, 0, aa);
}
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else {
directoryZip(zos, file, "");
}
}
/**
* 压缩目录文件
* @param out
* @param f
* @param base
* @throws Exception
*/
private static void directoryZip(ZipOutputStream out, File f, String base)
throws Exception {
// 如果传入的是目录
if (f.isDirectory()) {
File[] fl = f.listFiles();
// 创建压缩的子目录
out.putNextEntry(new ZipEntry(base + "/"));
if (base.length() == 0) {
base = "";
} else {
base = base + "/";
}
for (int i = 0; i < fl.length; i++) {
directoryZip(out, fl[i], base + fl[i].getName());
}
} else {
// 把压缩文件加入zip中
out.putNextEntry(new ZipEntry(base));
FileInputStream in = null;
try {
in = new FileInputStream(f);
byte[] bb = new byte[2048];
int aa = 0;
while ((aa = in.read(bb)) != -1) {
out.write(bb, 0, aa);
}
} catch (Exception e) {
throw e;
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public static void main(){
}
}