packagecom.tzh.hw.utils;
importorg.apache.tools.zip.ZipEntry;
importorg.apache.tools.zip.ZipOutputStream;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.io.*;
importjava.util.ArrayList;
importjava.util.List;
/***文件压缩* @author xing.Li* @date 2020/08/05*/public classFileZipUtils{
/***把文件打成压缩包并保存在本地硬盘* @paramsrcFiles原路径(里面是string类型的路径集合)* @paramzipPathzip路径*/public static voidZipFiles(ListsrcFiles, StringzipPath) {
byte[] buf= new byte[4096];
ZipOutputStreamout = null;
try{
//创建zip输出流out = newZipOutputStream(newFileOutputStream(zipPath));
//循环将源文件列表添加到zip文件中for(inti = 0; i < srcFiles.size(); i++) {
File file= newFile((String) srcFiles.get(i));
FileInputStream in= newFileInputStream(file);
String fileName= file.getName();
//将文件名作为zip的Entry存入zip文件中out.putNextEntry(newZipEntry(fileName));
intlen;
while( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
} catch(IOExceptione) {
e.printStackTrace();
} finally{
if(null!= out) {
try{
out.close();
out = null;
} catch(IOExceptione) {
e.printStackTrace();
}
}
}
}
/*** zip文件*把文件列表打成压缩包并输出到客户端浏览器中** @paramrequest请求* @paramresponse响应* @paramsrcFiles原路径(里面是string类型的路径集合)* @paramzipNameaip的名字*/public static voidZipFiles(HttpServletRequestrequest, HttpServletResponseresponse, ListsrcFiles, StringzipName) {
byte[] buf= new byte[4096];
try{
//--设置成这样可以不用保存在本地,再输出,通过response流输出,直接输出到客户端浏览器中。ZipOutputStream out= newZipOutputStream(response.getOutputStream());
// Compress the filesif(request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
zipName = newString(zipName.getBytes("GB2312"),"ISO-8859-1");
} else{
//对文件名进行编码处理中文问题zipName = java.net.URLEncoder.encode(zipName, "UTF-8");
zipName = newString(zipName.getBytes("UTF-8"), "GBK");
}
//重点 不同类型的文件对应不同的MIME类型---setContentTyperesponse.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/zip; application/octet-stream");
// inline在浏览器中直接显示,不提示用户下载// attachment弹出对话框,提示用户进行下载保存本地//默认为inline方式response.setHeader("Content-Disposition", "attachment;filename="+ zipName);
for(inti = 0; i < srcFiles.size(); i++) {
File file= newFile((String) srcFiles.get(i));
FileInputStream in= newFileInputStream(file);
// Add ZIP entry to output stream.String fileName= file.getName();
out.putNextEntry(newZipEntry(fileName));
// Transfer bytes from the file to the ZIP fileintlen;
while( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entryout.closeEntry();
in.close();
}
// Complete the ZIP fileout.close();
} catch(IOExceptione) {
e.printStackTrace();
}
}
/***把文件目录打成压缩包并输出到客户端浏览器中* @paramrequest请求* @paramresponse响应* @paramsourcePath原路径(里面是string类型的路径集合)* @paramzipName下载zip文件名称*/public static voidcreateZip(HttpServletRequestrequest, HttpServletResponseresponse, StringsourcePath, StringzipName) {
FileOutputStreamfos = null;
ZipOutputStreamout = null;
try{
//--设置成这样可以不用保存在本地,再输出,通过response流输出,直接输出到客户端浏览器中。out = newZipOutputStream(response.getOutputStream());
//此处修改字节码方式。out.setEncoding("gbk");
if(request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
zipName = newString(zipName.getBytes("GB2312"),"ISO-8859-1");
} else{
//对文件名进行编码处理中文问题zipName = java.net.URLEncoder.encode(zipName, "UTF-8");
zipName = newString(zipName.getBytes("UTF-8"), "GBK");
}
//重点 不同类型的文件对应不同的MIME类型---setContentTyperesponse.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");
//默认为inline方式response.setHeader("Content-Disposition", "attachment;filename="+ zipName);
writeZip(newFile(sourcePath),"",out);
out.flush();
out.close();
} catch(IOExceptione) {
e.printStackTrace();
} finally{
if(out != null) {
try{
out.close();
out = null;
} catch(IOExceptione) {
e.printStackTrace();
}
}
if(fos != null) {
try{
fos.close();
fos = null;
} catch(IOExceptione) {
e.printStackTrace();
}
}
}
}
/***创建zip文件* @paramfile文件或者目录* @paramparentPath父路径(默认为"")* @paramzosZipOutputStream*/private static voidwriteZip(Filefile, StringparentPath, ZipOutputStreamzos) {
if(file.exists()){
//处理文件夹if(file.isDirectory()){
parentPath += file.getName() + File.separator;
File[] files=file.listFiles();
if(files.length!= 0) {
for(File f:files){
writeZip(f, parentPath, zos);
}
} else{//空目录则创建当前目录try{
zos.putNextEntry(newZipEntry(parentPath));
} catch(IOExceptione) {
e.printStackTrace();
}
}
} else{
FileInputStreamfis=null;
try{
fis=newFileInputStream(file);
ZipEntry ze= newZipEntry(parentPath + file.getName());
zos.putNextEntry(ze);
byte[] content=new byte[1024];
intlen;
while((len=fis.read(content))!=-1){
zos.write(content,0,len);
zos.flush();
}
} catch(FileNotFoundExceptione) {
System.out.println("创建ZIP文件失败");
} catch(IOExceptione) {
System.out.println("创建ZIP文件失败");
}finally{
try{
if(fis!=null){
fis.close();
}
}catch(IOExceptione){
System.out.println("创建ZIP文件失败");
}
}
}
}
}
public static voidmain(String[] args) {
//测试文件列表List srcFiles= newArrayList();
srcFiles.add("C:\\Users\\hw\\Desktop\\hdvdatasrv.db");
ZipFiles(srcFiles, "C:\\Users\\hw\\Desktop\\cszip.zip");
}
}
拜拜了,最后祝看文章的小伙伴大富大贵 多财多亿 人早生贵子新婚快乐......