import java.io.*;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 压缩文件并下载 多个文件
*
*
* @param urlPathList
* 需要压缩的文件路径集合
* @throws Exception
*/
public static void urldownloadZip(List<String> urlPathList,
OutputStream output){
BufferedInputStream bis=null;
ZipOutputStream zos = null;
FileInputStream in = null;
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(output);
zos = new ZipOutputStream(out);
for (int i = 0; i < urlPathList.size(); i++) {
in = new FileInputStream(urlPathList.get(i));
bis = new BufferedInputStream(in);
ZipEntry entry = new ZipEntry(urlPathList.get(i));
zos.putNextEntry(entry);
int count;
byte data[] = new byte[1024];
while ((count = bis.read(data, 0, 1024)) != -1)
{
zos.write(data, 0, count);
}
zos.closeEntry();
}
}catch (Exception e){
logger.error("压缩文件并完成下载失败!", e);
e.printStackTrace();
}finally{
try {
if (bis != null) {
bis.close();
}
if (in != null) {
in.close();
}
if (zos != null) {
zos.flush();
zos.close();
}
if (out != null) {
out.flush();
out.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}