1、FileToZip接口類
/**
*
*/
package com.sale.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 將文件夾下面的文件
* 打包成zip壓縮文件
* @author Saffi
* @date 2017-10-16
*/
public final class FileToZip {
private FileToZip(){}
/**
* 將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下
* @param sourceFilePath :待壓縮的文件路徑
* @param zipFilePath :壓縮后存放路徑
* @param fileName :壓縮后文件的名稱
* @return
* @throws IOException
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName) throws IOException{
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if(sourceFile.exists() == false){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"不存在.");
}else{
try {
File zipFile = new File(zipFilePath + "/" + fileName +".zip");
if(zipFile.exists()){
System.out.println(zipFilePath + "目錄下存在名字為:" + fileName +".zip" +"打包文件.");
}else{
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1){
System.out.println("待壓縮的文件目錄:" + sourceFilePath + "里面不存在文件,無需壓縮.");
}else{
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++){
//創建ZIP實體,並添加進壓縮包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//讀取待壓縮的文件並寫進壓縮包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
if(null != bis){
bis.close();
}
}
flag = true;
if(null != zos){
zos.closeEntry();
zos.close();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//關閉流
try {
if(null != fis){
fis.close();
}
if(null != fos){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return flag;
}
}
2、調用接口實現生成壓縮包並下載
/**
* 打包Zip並下載
*
*/
@RequestMapping("tevo_testzip.action")
public void downloadZips(HttpServletRequest request,HttpServletResponse response) {
//原文件地址
String sourceFilePath = request.getSession().getServletContext().getRealPath("")+"/download/goodsbarcode";
//生成Zip存放地址
String zipFilePath = request.getSession().getServletContext().getRealPath("")+"/download";
//文件名
String fileName = "goodsbarcode";
try {
//調用FileToZip接口生成壓縮包
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失敗!");
}
//Zip壓縮包文件名
String fileNames = "goodsbarcode.zip";
//Zip壓縮包路徑
String path = request.getSession().getServletContext().getRealPath("")+"/download/goodsbarcode.zip";
File file = new File(path);
if(file.length()<1||file==null){
System.out.println("文件不存在!");
}else{
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(fileNames.getBytes("ISO8859-1"), "UTF-8"));
response.setContentLength((int) file.length());
response.setContentType("application/zip");// 定義輸出類型
FileInputStream fis = new FileInputStream(file);
BufferedInputStream buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];// 相當於我們的緩存
long k = 0;// 該值用於計算當前實際下載了多少字節
OutputStream myout = response.getOutputStream();// 從response對象中得到輸出流,准備下載
// 開始循環下載
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
// 刷新此輸出流並強制將所有緩沖的輸出字節被寫出
myout.flush();
//關閉流
myout.close();
buff.close();
fis.close();
//刪除生成的壓縮包文件
file.delete();
}
//刪除項目文件夾下所有的圖片
File filedel = new File(sourceFilePath);
if (filedel.isDirectory()) { //如果path表示的是一個目錄
File[] fileList = filedel.listFiles();
for (int i = 0; i < fileList.length; i++) {
File delfile = fileList[i];
if (!delfile.isDirectory()) { //如果文件的不是一個目錄,則刪除
//刪除文件
delfile.delete();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}