1. 程式人生 > 實用技巧 >Java Zip工具類(全)

Java Zip工具類(全)

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
public class ZipUtils {

    /**
     * 將存放在sourceFilePath目錄下的原始檔,打包成fileName名稱的zip檔案,並存放到zipFilePath路徑下
     * 
@param sourceFilePath 待壓縮的檔案路徑 * @param zipFilePath 壓縮後存放路徑 * @param fileName 壓縮後文件的名稱 * @return */ public static boolean folderToZip(String sourceFilePath, String zipFilePath, String fileName) { boolean flag = false; File sourceFile = new File(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 = new FileOutputStream(zipFile); } catch (FileNotFoundException e) { // e.printStackTrace(); log.error(e.getMessage()); } finally { if ( null != fos ){ fos.close(); } } try { zos = new ZipOutputStream(new BufferedOutputStream(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 = new ZipEntry(sourceFiles[i].getName()); zos.putNextEntry(zipEntry); // 讀取待壓縮的檔案並寫進壓縮包裡 try { fis = new FileInputStream(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 new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { // 關閉流 try { if (null != bis){ bis.close(); } if (null != zos){ zos.close(); } } catch (IOException e) { e.printStackTrace(); } } } return flag; } /** * 將sourceFilePath檔案,打包成fileName名稱的zip檔案,並存放到zipFilePath路徑下 * @param sourceFilePath 待壓縮的檔案路徑 * @param zipFilePath 壓縮後存放路徑 * @param fileName 壓縮後文件的名稱 * @return */ public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) { boolean flag = false; File sourceFile = new File(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 = new FileOutputStream(zipFile); } catch (FileNotFoundException e) { // e.printStackTrace(); log.error(e.getMessage()); } finally { if (null != fos ){ fos.close(); } } try { zos = new ZipOutputStream(new BufferedOutputStream(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 = new ZipEntry(sourceFile.getName()); if ( zos != null ){ zos.putNextEntry(zipEntry); } // 讀取待壓縮的檔案並寫進壓縮包裡 try { fis = new FileInputStream(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 new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { // 關閉流 try { if (null != bis){ bis.close(); } if (null != zos){ zos.close(); } } catch (IOException e) { // e.printStackTrace(); log.error(e.getMessage()); } } } return flag; } /** * 將流的內容打包成fileName名稱的zip檔案,並存放到zipFilePath路徑下 * @param streamfilename 待壓縮的檔案路徑 * @param zipFilePath 壓縮後存放路徑 * @param fileName 壓縮後文件的名稱 * @return */ public static boolean streamToZip(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 = new FileOutputStream(zipFile); } catch (FileNotFoundException e) { // e.printStackTrace(); log.error(e.getMessage()); } finally { if (null != fos ){ fos.close(); } } try { zos = new ZipOutputStream(new BufferedOutputStream(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 = new ZipEntry(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 new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { // 關閉流 try { if (null != bis){ bis.close(); } if (null != zos){ zos.close(); } } catch (IOException e) { e.printStackTrace(); } } return flag; } /** * 將流轉成zip檔案輸出 * @param inputstream 檔案流 * @param streamfilename 流檔案的名稱 * @param fileName zip包的名稱 * @param response * @return */ public static boolean streamToZipStream(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 = new ZipOutputStream(out); byte[] bufs = new byte[1024 * 10]; // 建立ZIP實體,並新增進壓縮包 ZipEntry zipEntry = new ZipEntry(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 new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { // 關閉流 try { if (null != bis){ bis.close(); } if (null != zos){ zos.close(); } if (null != out){ out.close(); } } catch (IOException e) { e.printStackTrace(); } } return flag; } /** * 將多個流轉成zip檔案輸出 * @param listStream 檔案流實體類物件 * @param fileName zip包的名稱 * @param response * @return */ public static boolean listStreamToZipStream(List<ZipDto> listStream, 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 = new ZipOutputStream(out); byte[] bufs = new byte[1024 * 10]; for (ZipDto zipDto : listStream) { String streamfilename = zipDto.getName(); // 建立ZIP實體,並新增進壓縮包 ZipEntry zipEntry = new ZipEntry(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 new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(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()); } } return flag; } }