1. 程式人生 > >文件壓縮工具類

文件壓縮工具類

urn nal entry read puts prope += ext reat

package com.csf.myproduct.common;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Createy by user on 9/14/2018.10:48
 */
public class ZipCompressorUtils {

    
private static final int BUFFER = 8192; public static void main(String[] args) { String namePath = "E:/壓縮test/sys.zip"; List<String> list = new ArrayList<String>(); list.add("E:/壓縮test/file/log4j.properties"); list.add("E:/壓縮test/file/pom引入jar包.txt"); list.add(
"E:/壓縮test/file/PUSH網關常見問題排查手冊.doc"); list.add("E:/壓縮test/file/交易賬戶信息2018-08-15.xls"); // list.add("E:/壓縮test/file/推送代碼.zip"); compress(namePath, list.toArray(new String[list.size()])); System.out.println("\n壓縮完成 路徑:" + namePath); } /** * 壓縮入口 * * @param compPathname 壓縮輸出路徑 *
@param pathName 需要進行壓縮的文件列表 */ public static void compress(String compPathname, String... pathName) { File zipFile = new File(compPathname); ZipOutputStream out = null; try { FileOutputStream fileOutputStream = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); out = new ZipOutputStream(cos); String basedir = ""; for (int i = 0; i < pathName.length; i++) { compress(new File(pathName[i]), out, basedir); } out.close(); } catch (Exception e) { System.out.println("壓縮異常============"); throw new RuntimeException(e); } } public static void compress(String srcPathName, String compPathname) throws IOException { File zipFile = new File(compPathname); File file = new File(srcPathName); if (!file.exists()) throw new RuntimeException(srcPathName + "不存在!"); try { FileOutputStream fileOutputStream = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos); String basedir = ""; File[] files = file.listFiles(); for (File file2 : files) { compress(file2, out, basedir); } out.close(); } catch (Exception e) { System.out.println("壓縮異常============"); throw new RuntimeException(e); } } /** * @param file 需要壓縮的文件 * @param out 壓縮工具 * @param basedir */ private static void compress(File file, ZipOutputStream out, String basedir) { /* 判斷是目錄還是文件 */ if (file.isDirectory()) { System.out.println("壓縮:" + basedir + file.getName()); compressDirectory(file, out, basedir); } else { System.out.println("壓縮:" + basedir + file.getName()); compressFile(file, out, basedir); } } /** * 壓縮一個目錄 * * @param dir 需要壓縮的文件 * @param out 壓縮工具 * @param basedir */ private static void compressDirectory(File dir, ZipOutputStream out, String basedir) { if (!dir.exists()) return; try { // if (!dir.getName().startsWith("2017")) { // basedir += dir.getName() + "/"; // out.putNextEntry(new ZipEntry(basedir)); // } basedir += dir.getName() + "/"; out.putNextEntry(new ZipEntry(basedir)); File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { /* 遞歸 */ compress(files[i], out, basedir); } } catch (Exception e) { e.printStackTrace(); } } /** * 壓縮一個文件 * * @param file 需要壓縮的文件 * @param out 壓縮工具 * @param basedir */ private static void compressFile(File file, ZipOutputStream out, String basedir) { if (!file.exists()) { System.out.println("文件不存在"); return; } try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(basedir + file.getName()); out.putNextEntry(entry); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } bis.close(); } catch (Exception e) { throw new RuntimeException(e); } } }

文件壓縮工具類