1. 程式人生 > >Java zip/gzip檔案壓縮和解壓縮

Java zip/gzip檔案壓縮和解壓縮

Java IO

為了減少傳輸時的資料量 在Java中提供了專門的壓縮流將檔案或者資料夾壓縮成zip,gzip,jar等檔案形式。

壓縮流實現

Java支援的三種壓縮格式:zip、jar、gzip。

1.zip是一種較為常見的壓縮格式,Java提供了java.util.zip包,常用類:

  • ZipInputStream
  • ZipOutputStream
  • ZipEntry
  • ZipFile

依靠上述4個類完成zip型別檔案操作。

2.Jar是Java專有的一種壓縮格式,Java提供了java.util.jar包,常用類:

  • JarInputStream
  • JarOutputStream
  • JarEntry
  • JarFile


3.GZIP是Unix系統的檔案壓縮格式,Linux系統中經常用的.gz 檔案就是GZIP格式,Java提供了

  • GZipInputStream
  • GZipOutputStream


本文以zip 壓縮/解壓縮為例來講解。

壓縮

@Test
public void testZip() throws IOException {

    File zipFile = new File("D:\\log.zip"); //壓縮後的檔案
    ZipOutputStream zipOut = null;
    try{
        zipOut = new
ZipOutputStream(new FileOutputStream(zipFile)); File dir = new File("D:\\logs\\info\\"); //待壓縮的資料夾 File[] files = dir.listFiles(); for (File file : files){ InputStream in = null; try{ zipOut.putNextEntry(new ZipEntry(file.getName())) ; zipOut.setComment("test zip"
) ; // 設定註釋 in = new FileInputStream(file) ; // 定義檔案的輸入流 IoUtils.copy(in, zipOut); }finally { IoUtils.closeQuietly(in); } } }finally { IoUtils.closeQuietly(zipOut); } System.out.println("壓縮檔案完成"); }

解壓縮

@Test
public void testUnZip() throws IOException {

    File dir = new File("D:\\test");
    if(!dir.exists()){
        dir.mkdirs();
    }

    File srcFile = new File("D:\\log.zip");
    ZipFile zipFile = new ZipFile(srcFile);

    ZipInputStream zipIn = null;
    try{
        zipIn = new ZipInputStream(new FileInputStream(srcFile));
        ZipEntry entry = null;
        while((entry = zipIn.getNextEntry())!=null){
            System.out.println("解壓縮檔案:" + entry.getName());
            OutputStream out = null;
            InputStream in = null;
            try{
                File file = new File(dir, entry.getName());
                in = zipFile.getInputStream(entry);
                out = new FileOutputStream(file);
                IoUtils.copy(in, out);
            }finally {
                IoUtils.closeQuietly(out);
                IoUtils.closeQuietly(in);
            }
        }
    }finally {
        IoUtils.closeQuietly(zipIn);
    }

    System.out.println("解壓縮檔案完成");
}

IoUtils.java

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 * @create 2016-10-31 10:19
 */
public class IoUtils {

    public static void closeQuietly(InputStream input){
        closeQuietly((Closeable)input);
    }

    public static void closeQuietly(OutputStream output){
        closeQuietly((Closeable)output);
    }

    public static void closeQuietly(Closeable closeable) {
        try {
            if(closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void closeQuietly(Closeable... closeables){
        for (Closeable closeable : closeables){
            closeQuietly(closeable);
        }
    }

    public static long copy(InputStream in, OutputStream out, int bufferSize) throws IOException {
        byte[] buff = new byte[bufferSize];
        return copy(in, out, buff);
    }

    public static long copy(InputStream in, OutputStream out) throws IOException {
        byte[] buff = new byte[1024];
        return copy(in, out, buff);
    }

    public static long copy(InputStream in, OutputStream out, byte[] buff) throws IOException {
        long count = 0;
        int len = -1;
        while((len=in.read(buff, 0, buff.length))!=-1){
            out.write(buff, 0, len);
            count += len;
        }
        return count;
    }
}