importlombok.extern.slf4j.Slf4j;importorg.apache.poi.ss.formula.functions.T;importjavax.servlet.http.HttpServletResponse;import java.io.*;importjava.util.List;importjava.util.zip.ZipEntry;importjava.util.zip.ZipOutputStream;
@Slf4jpublic classZipUtils {/*** 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
*@paramsourceFilePath 待压缩的文件路径
*@paramzipFilePath 压缩后存放路径
*@paramfileName 压缩后文件的名称
*@return
*/
public static booleanfolderToZip(String sourceFilePath, String zipFilePath, String fileName) {boolean flag = false;
File sourceFile= newFile(sourceFilePath);
FileInputStream fis= null;
BufferedInputStream bis= null;
FileOutputStream fos= null;
ZipOutputStream zos= null;if (sourceFile.exists() == false) {
log.info("待压缩的文件目录:" + sourceFilePath + "不存在.");return false;
}else{try{
File zipFile= new File(zipFilePath + "/" + fileName + ".zip");if(zipFile.exists()) {
log.info(zipFilePath+ "目录下存在名字为:" + fileName + ".zip" + "打包文件.");return false;
}else{
File[] sourceFiles=sourceFile.listFiles();if (null == sourceFiles || sourceFiles.length < 1) {
log.error("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");return false;
}else{try{
fos= newFileOutputStream(zipFile);
}catch(FileNotFoundException e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if ( null !=fos ){
fos.close();
}
}try{
zos= new ZipOutputStream(newBufferedOutputStream(fos));
}catch(Exception e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if ( null !=zos ){
zos.close();
}
}byte[] bufs = new byte[1024 * 10];for (int i = 0; i < sourceFiles.length; i++) {//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = newZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);//读取待压缩的文件并写进压缩包里
try{
fis= newFileInputStream(sourceFiles[i]);
}catch(FileNotFoundException e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if ( null !=fis ){
fis.close();
}
}try{
bis= new BufferedInputStream(fis, 1024 * 10);
}catch(Exception e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if ( null !=bis ){
bis.close();
}
}int read = 0;while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs,0, read);
}
}
flag= true;
}
}
}catch(FileNotFoundException e) {
e.printStackTrace();throw newRuntimeException(e);
}catch(IOException e) {
e.printStackTrace();throw newRuntimeException(e);
}finally{//关闭流
try{if (null !=bis){
bis.close();
}if (null !=zos){
zos.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}returnflag;
}/*** 将sourceFilePath文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
*@paramsourceFilePath 待压缩的文件路径
*@paramzipFilePath 压缩后存放路径
*@paramfileName 压缩后文件的名称
*@return
*/
public static booleanfileToZip(String sourceFilePath, String zipFilePath, String fileName) {boolean flag = false;
File sourceFile= newFile(sourceFilePath);
FileInputStream fis= null;
BufferedInputStream bis= null;
FileOutputStream fos= null;
ZipOutputStream zos= null;if (sourceFile.exists() == false) {
log.info("待压缩的文件:" + sourceFilePath + "不存在.");return false;
}else{try{
File zipFile= new File(zipFilePath + "/" + fileName + ".zip");if(zipFile.exists()) {
log.info(zipFilePath+ "目录下存在名字为:" + fileName + ".zip" + "打包文件.");return false;
}else{try{
fos= newFileOutputStream(zipFile);
}catch(FileNotFoundException e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if (null !=fos ){
fos.close();
}
}try{
zos= new ZipOutputStream(newBufferedOutputStream(fos));
}catch(Exception e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if (null !=zos ){
zos.close();
}
}byte[] bufs = new byte[1024 * 10];//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = newZipEntry(sourceFile.getName());if ( zos != null){
zos.putNextEntry(zipEntry);
}//读取待压缩的文件并写进压缩包里
try{
fis= newFileInputStream(sourceFile);
}catch(FileNotFoundException e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if (null !=fis){
fis.close();
}
}try{
bis= new BufferedInputStream(fis, 1024 * 10);
}catch(Exception e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if (null !=bis){
bis.close();
}
}int read = 0;if ( bis != null){while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {if ( zos != null){
zos.write(bufs,0, read);
}
}
}
flag= true;
}
}catch(FileNotFoundException e) {
e.printStackTrace();throw newRuntimeException(e);
}catch(IOException e) {
e.printStackTrace();throw newRuntimeException(e);
}finally{//关闭流
try{if (null !=bis){
bis.close();
}if (null !=zos){
zos.close();
}
}catch(IOException e) {//e.printStackTrace();
log.error(e.getMessage());
}
}
}returnflag;
}/*** 将流的内容打包成fileName名称的zip文件,并存放到zipFilePath路径下
*@paramstreamfilename 待压缩的文件路径
*@paramzipFilePath 压缩后存放路径
*@paramfileName 压缩后文件的名称
*@return
*/
public static booleanstreamToZip(InputStream fis, String streamfilename, String zipFilePath, String fileName) {boolean flag = false;
BufferedInputStream bis= null;
FileOutputStream fos= null;
ZipOutputStream zos= null;try{
File zipFile= new File(zipFilePath + "/" + fileName + ".zip");if(zipFile.exists()) {
log.info(zipFilePath+ "目录下存在名字为:" + fileName + ".zip" + "打包文件.");return false;
}else{try{
fos= newFileOutputStream(zipFile);
}catch(FileNotFoundException e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if (null !=fos ){
fos.close();
}
}try{
zos= new ZipOutputStream(newBufferedOutputStream(fos));
}catch(Exception e) {//e.printStackTrace();
log.error(e.getMessage());
}finally{if (null !=zos ){
zos.close();
}
}byte[] bufs = new byte[1024 * 10];//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = newZipEntry(streamfilename);if ( zos != null){
zos.putNextEntry(zipEntry);
}//读取待压缩的文件并写进压缩包里
bis = new BufferedInputStream(fis, 1024 * 10);int read = 0;while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {if ( zos != null){
zos.write(bufs,0, read);
}
}
flag= true;
}if ( zos != null){
zos.close();
}
}catch(FileNotFoundException e) {
e.printStackTrace();throw newRuntimeException(e);
}catch(IOException e) {
e.printStackTrace();throw newRuntimeException(e);
}finally{//关闭流
try{if (null !=bis){
bis.close();
}if (null !=zos){
zos.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}returnflag;
}/*** 将流转成zip文件输出
*@paraminputstream 文件流
*@paramstreamfilename 流文件的名称
*@paramfileName zip包的名称
*@paramresponse
*@return
*/
public static booleanstreamToZipStream(InputStream inputstream, String streamfilename, String fileName,
HttpServletResponse response) {boolean flag = false;
BufferedInputStream bis= null;
FileOutputStream fos= null;
ZipOutputStream zos= null;
OutputStream out= null;try{
out=response.getOutputStream();
response.reset();
response.setHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
zos= newZipOutputStream(out);byte[] bufs = new byte[1024 * 10];//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = newZipEntry(streamfilename);
zos.putNextEntry(zipEntry);//读取待压缩的文件并写进压缩包里
bis = new BufferedInputStream(inputstream, 1024 * 10);int read = 0;while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs,0, read);
}
flag= true;
zos.close();
}catch(FileNotFoundException e) {
e.printStackTrace();throw newRuntimeException(e);
}catch(IOException e) {
e.printStackTrace();throw newRuntimeException(e);
}finally{//关闭流
try{if (null !=bis){
bis.close();
}if (null !=zos){
zos.close();
}if (null !=out){
out.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}returnflag;
}/*** 将多个流转成zip文件输出
*@paramlistStream 文件流实体类对象
*@paramfileName zip包的名称
*@paramresponse
*@return
*/
public static boolean listStreamToZipStream(ListlistStream, String fileName, HttpServletResponse response) {boolean flag = false;
BufferedInputStream bis= null;
FileOutputStream fos= null;
ZipOutputStream zos= null;
OutputStream out= null;try{
out=response.getOutputStream();
response.reset();
response.setHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
response.setHeader("Access-Control-Allow-Origin","*");
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
zos= newZipOutputStream(out);byte[] bufs = new byte[1024 * 10];for(ZipDto zipDto : listStream) {
String streamfilename=zipDto.getName();//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = newZipEntry(streamfilename);
zos.putNextEntry(zipEntry);//读取待压缩的文件并写进压缩包里
bis = new BufferedInputStream(zipDto.getInputstream(), 1024 * 10);int read = 0;while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs,0, read);
}
}
flag= true;
zos.close();
}catch(FileNotFoundException e) {
e.printStackTrace();throw newRuntimeException(e);
}catch(IOException e) {
e.printStackTrace();throw newRuntimeException(e);
}finally{//关闭流
try{if (null !=bis){
bis.close();
}if (null !=zos){
zos.close();
}if (null !=out){
out.close();
}
}catch(IOException e) {//e.printStackTrace();
log.error(e.getMessage());
}
}returnflag;
}
}