java的ant.jar zip打包 utils工具類

n24d 9年前發布 | 5K 次閱讀 Java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {

public static void ZipFiles(List<String> srcfile, String zipPath) { byte[] buf = new byte[4096]; ZipOutputStream out = null; try { // 創建zip輸出流 out = new ZipOutputStream(new FileOutputStream(zipPath)); // 循環將源文件列表添加到zip文件中 for (int i = 0; i < srcfile.size(); i++) { File file = new File(srcfile.get(i)); FileInputStream in = new FileInputStream(file); String fileName = file.getName(); out.putNextEntry(new ZipEntry(fileName)); int len; while ( (len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } } catch (IOException e) { e.printStackTrace(); } finally { if (null != out) { try { out.close(); out = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

} }

public static void ZipFiles(HttpServletRequest request, HttpServletResponse response, List<String> srcfile, String downloadZipFileName) { byte[] buf = new byte[4096]; try { // Create the ZIP file // ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath)); ZipOutputStream out = new ZipOutputStream(response.getOutputStream());//--設置成這樣可以不用保存在本地,再輸出, 通過response流輸出。 // Compress the files if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) { downloadZipFileName = new String(downloadZipFileName.getBytes("GB2312"),"ISO-8859-1"); } else { // 對文件名進行編碼處理中文問題 downloadZipFileName = java.net.URLEncoder.encode(downloadZipFileName, "UTF-8");// 處理中文文件名的問題 downloadZipFileName = new String(downloadZipFileName.getBytes("UTF-8"), "GBK");// 處理中文文件名的問題 } response.reset(); // 重點突出 response.setCharacterEncoding("UTF-8"); // 重點突出 response.setContentType("application/x-msdownload");// 不同類型的文件對應不同的MIME類型 // 重點突出 // inline在瀏覽器中直接顯示,不提示用戶下載 // attachment彈出對話框,提示用戶進行下載保存本地 // 默認為inline方式 response.setHeader("Content-Disposition", "attachment;filename="+downloadZipFileName); for (int i = 0; i < srcfile.size(); i++) { File file = new File(srcfile.get(i)); FileInputStream in = new FileInputStream(file); // Add ZIP entry to output stream. String fileName = file.getName(); out.putNextEntry(new ZipEntry(fileName)); // Transfer bytes from the file to the ZIP file int len; while ( (len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); System.out.println("壓縮完成."); } catch (IOException e) { e.printStackTrace(); } }

public static void main(String[] args) { List<String> paths = new ArrayList<String>(); paths.add("d:/地理編碼系統詳細設計V1.0.docx"); paths.add("d:/中國聯通協同辦公項目.ppt"); ZipFiles(paths, "d:/zipfiles/info.zip"); } } </pre>

 本文由用戶 n24d 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!