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; } }