com.littcore.io.util.ZipUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.littcore.io.util.ZipUtils.java

Source

package com.littcore.io.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.littcore.io.zip.ZipEntry;
import com.littcore.io.zip.ZipFile;
import com.littcore.io.zip.ZipOutputStream;

/**
 * ZIP.
 * 
 * <pre><b>??</b>
 * java.util.zip
 * RAR?ZIP?
 * ZipEntry????"/"windowsFile.seperator??
 * </pre>
 * 
 * <pre><b></b>
 * 2013-01-16 
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">?</a>
 * @since 2008-12-4, 2013-01-16
 * @version 1.0, 1.1
 */
public class ZipUtils {

    /** The Constant logger. */
    private static final Log logger = LogFactory.getLog(ZipUtils.class);

    /** The Constant BUFFERED_SIZE. */
    public static final int BUFFERED_SIZE = 1024;

    /**
     * .
     * 
     * @param srcFileOrPath ?
     * @param targetFileNamePath ?
     * 
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void zip(String srcFileOrPath, String targetFileNamePath) throws IOException {
        zip(new File(srcFileOrPath), new File(targetFileNamePath));
    }

    /**
     * .
     *
     * @param srcFile the src file
     * @param targetFileNamePath the target file name path
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void zip(File srcFile, String targetFileNamePath) throws IOException {
        zip(srcFile, new File(targetFileNamePath));
    }

    public static void zip(File srcFileOrPath, File targetFileNamePath) throws IOException {
        //?
        CheckedOutputStream cs = new CheckedOutputStream(new FileOutputStream(targetFileNamePath), new CRC32());
        //zip?
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
        out.setEncoding("GBK");
        zip(out, srcFileOrPath, "");
        out.close();
    }

    /**
     * ?.
     * 
     * @param out ZIP?
     * @param srcFile ?
     * @param basePath 
     * 
     * @throws IOException Signals that an I/O exception has occurred.
     */
    private static void zip(ZipOutputStream out, File srcFile, String basePath) throws IOException {
        if (srcFile.isDirectory()) {
            File[] files = srcFile.listFiles();

            if (files.length == 0) {
                out.putNextEntry(new ZipEntry(basePath + srcFile.getName() + "/")); //?
                out.closeEntry();
            } else {
                basePath += srcFile.getName() + "/";
                for (int i = 0; i < files.length; i++) {
                    zip(out, files[i], basePath);
                }
            }

        } else {
            out.putNextEntry(new ZipEntry(basePath + srcFile.getName()));
            FileInputStream in = new FileInputStream(srcFile);
            int len;
            byte[] buf = new byte[BUFFERED_SIZE];
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
    }

    /**
     * ?
     * @param files 
     * @param targetFileNamePath 
     * @throws IOException
     */
    public static void batchZip(File[] files, String targetFileNamePath) throws IOException {
        //?
        CheckedOutputStream cs = new CheckedOutputStream(new FileOutputStream(targetFileNamePath), new CRC32());
        //zip?
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
        File srcFile = null;
        for (int i = 0; i < files.length; i++) {
            srcFile = files[i];
            out.putNextEntry(new ZipEntry(srcFile.getName()));
            FileInputStream in = new FileInputStream(srcFile);
            int len;
            byte[] buf = new byte[BUFFERED_SIZE];
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
        out.close();
    }

    public static void unzip(File srcZipFile, File targetFilePath, boolean isDelSrcFile) throws IOException {
        if (!targetFilePath.exists()) //?
            targetFilePath.mkdirs();
        ZipFile zipFile = new ZipFile(srcZipFile);

        Enumeration fileList = zipFile.getEntries();
        ZipEntry zipEntry = null;
        while (fileList.hasMoreElements()) {
            zipEntry = (ZipEntry) fileList.nextElement();
            if (zipEntry.isDirectory()) //?
            {
                File subFile = new File(targetFilePath, zipEntry.getName());
                if (!subFile.exists()) //??
                    subFile.mkdir();
                continue;
            } else //?
            {
                File targetFile = new File(targetFilePath, zipEntry.getName());
                if (logger.isDebugEnabled()) {
                    logger.debug("" + targetFile.getName());
                }

                if (!targetFile.exists()) {
                    File parentPath = new File(targetFile.getParent());
                    if (!parentPath.exists()) //????????
                        parentPath.mkdirs();
                    //targetFile.createNewFile();
                }

                InputStream in = zipFile.getInputStream(zipEntry);
                FileOutputStream out = new FileOutputStream(targetFile);
                int len;
                byte[] buf = new byte[BUFFERED_SIZE];
                while ((len = in.read(buf)) != -1) {
                    out.write(buf, 0, len);
                }
                out.close();
                in.close();
            }
        }
        zipFile.close();
        if (isDelSrcFile) {
            boolean isDeleted = srcZipFile.delete();
            if (isDeleted) {
                if (logger.isDebugEnabled()) {
                    logger.debug("");
                }
            }
        }
    }

    /**
     * ZIP.
     * ?ZipEntry????
     * 
     * @param srcZipFile ZIP
     * @param targetFilePath 
     * @param isDelSrcFile ???
     * 
     * @throws ZipException the zip exception
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void unzip(String srcZipFile, String targetFilePath, boolean isDelSrcFile) throws IOException {
        unzip(new File(srcZipFile), new File(targetFilePath), isDelSrcFile);
    }

    /**
     * The main method.
     * 
     * @param args the arguments
     * 
     * @throws Exception the exception
     */
    public static void main(String[] args) throws Exception {

        ZipUtils.zip("D:\\licenses\\trannms\\wenzhou\\", "D:\\zip.zip");
        //ZipUtils.zip("D:\\zip.zip", "D:\\zip1.zip");
        //ZipUtils.unzip("D:\\zip.zip", "D:\\unzip", false);
        //File[] files = new File[]{new File("D:\\signedKey.dat"),new File("D:\\temp\\dump.txt")};
        //ZipUtils.batchZip(files, "D:\\zip.zip");

        //File[] files = new File[]{new File("D:\\licenses\\trannms\\wenzhou\\license.xml"),new File("D:\\licenses\\trannms\\wenzhou\\license.key")};
        //ZipUtils.batchZip(files, "D:\\zip.zip");
    }
}