利用zip压缩和解压文件(目录)

package com.yonge.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;

/**
 * 压缩目标文件为zip文件
 * @author wb-gaoy
 * @version $Id: CompressByZip.java,v 0.1 2013-1-8 上午10:18:16 wb-gaoy Exp $
 */
public class ZipUtil {

    public static void zip(File srcFile, File targetFile) throws IOException {
        ZipOutputStream zipOutputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(targetFile);
            zipOutputStream = new ZipOutputStream(fileOutputStream);
            zip(zipOutputStream, srcFile, "");
        } finally {
            if (zipOutputStream != null) {
                zipOutputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
    }

    public static void zip(ZipOutputStream zipOutputStream, File srcFile, String baseURL)
                                                                                         throws IOException {

        baseURL = baseURL == null || baseURL.length() == 0 ? srcFile.getName() : baseURL
                                                                                 + "/"
                                                                                 + srcFile
                                                                                     .getName();
        if (srcFile.isDirectory()) {
            File[] files = srcFile.listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    zip(zipOutputStream, f, baseURL);
                }
            } else {
                zipOutputStream.putNextEntry(new ZipEntry(baseURL + "/"));
            }
        } else {
            zipOutputStream.putNextEntry(new ZipEntry(baseURL));

            FileInputStream fileInputStream = null;
            try {
                //读数据
                fileInputStream = new FileInputStream(srcFile);
                byte[] bytes = new byte[1024];
                int length = -1;
                while ((length = fileInputStream.read(bytes)) != -1) {
                    zipOutputStream.write(bytes, 0, length);
                }
            } finally {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            }
        }
    }

    public static void unzip(File srcFile, File targetFile) throws IOException {
        FileInputStream fileInputStream = null;
        ZipInputStream zipInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(srcFile);
            zipInputStream = new ZipInputStream(fileInputStream);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            ZipEntry entry = zipInputStream.getNextEntry();
            File tempFile;
            while (entry != null) {
                tempFile = new File(targetFile, entry.getName());
                if (entry.isDirectory()) {
                    tempFile.mkdir();
                } else {
                    tempFile.getParentFile().mkdirs();
                    fileOutputStream = new FileOutputStream(tempFile);
                    IOUtils.copy(zipInputStream, fileOutputStream);
                }
                entry = zipInputStream.getNextEntry();
            }
        } finally {
            if (zipInputStream != null) {
                zipInputStream.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
    }

    public static void main(String[] args) {
        File file = new File("C:\\ecmng\\site\\tmp\\test");
        try {
            //zip(file, new File("demo.zip"));
            unzip(new File("demo.zip"), file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(压缩,ZipOutputStream,解压,ZipInputStream)