com.dv.util.DataViewerZipUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.dv.util.DataViewerZipUtil.java

Source

/*
 * DataViewerZipUtil.java  9/25/13 2:25 PM
 *
 * Copyright (C) 2012-2013 Nick Ma
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.dv.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 * 
 * (?ZIP?).
 * JDK1.6,???
 * @author zyh
 */
public class DataViewerZipUtil {
    /**
     * ?.
     * ?, .
     * @throws IOException
     */
    public static void zipFile(String srcFile, String destFile) throws IOException {
        zipFile(new File(srcFile), new File(destFile));
    }

    /**
     * ?.
     * ?, .
     * @param srcFile ?
     * @param destFile ?
     * @throws IOException
     */
    public static void zipFile(File srcFile, File destFile) throws IOException {
        zipFile(srcFile, FileUtils.openOutputStream(destFile));
    }

    /**
     * ??.
     *
     * @param srcFile ?
     * @param outputStream ??
     */
    private static void zipFile(File srcFile, OutputStream outputStream) {
        zipFile(srcFile, new ZipOutputStream(outputStream));
    }

    /**
     * ?ZIP?.
     *
     * @param srcFile ?
     * @param zipOut ?ZIP?
     */
    private static void zipFile(File srcFile, ZipOutputStream zipOut) {
        try {
            doZipFile(srcFile, zipOut, "");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(zipOut);
        }
    }

    /**
     * ZipOutputStream
     * @param srcFile 
     * @param zipOut ZipOutputStream?
     * @param ns ZIP
     * @throws IOException
     */
    private static void doZipFile(File srcFile, ZipOutputStream zipOut, String ns) throws IOException {
        if (srcFile.isFile()) {
            zipOut.putNextEntry(new ZipEntry(ns + srcFile.getName()));
            InputStream is = FileUtils.openInputStream(srcFile);
            try {
                IOUtils.copy(is, zipOut);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(is);
            }
            return;
        }
        for (File file : srcFile.listFiles()) {
            String entryName = ns + file.getName();

            if (file.isDirectory()) {
                entryName += File.separator;
                zipOut.putNextEntry(new ZipEntry(entryName));
            }
            doZipFile(file, zipOut, entryName);
        }
    }

    /**
     * .
     * ??, .
     *
     * @param zipFile 
     * @param destFile ?
     */
    public static void unzipFile(String zipFile, String destFile) throws IOException {
        unzipFile(new File(zipFile), new File(destFile));
    }

    /**
     * .
     * ??, .
     *
     * @param zipFile 
     * @param destFile ?
     */
    public static void unzipFile(File zipFile, File destFile) throws IOException {
        unzipFile(FileUtils.openInputStream(zipFile), destFile);
        //      doUnzipFile(new ZipFile(zipFile), destFile);
    }

    public static void doUnzipFile(ZipFile zipFile, File dest) throws IOException {
        if (!dest.exists()) {
            FileUtils.forceMkdir(dest);
        }
        Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File file = new File(dest, entry.getName());
            if (entry.getName().endsWith(File.separator)) {
                FileUtils.forceMkdir(file);
            } else {
                OutputStream out = FileUtils.openOutputStream(file);
                InputStream in = zipFile.getInputStream(entry);
                try {
                    IOUtils.copy(in, out);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        }
        zipFile.close();
    }

    /**
     * ?.
     *
     * @param is ??
     * @param destFile ?
     * @throws IOException
     */
    public static void unzipFile(InputStream is, File destFile) throws IOException {
        try {
            if (is instanceof ZipInputStream) {
                doUnzipFile((ZipInputStream) is, destFile);
            } else {
                doUnzipFile(new ZipInputStream(is), destFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    /**
     *
     * @param zipInput
     * @param dest
     * @throws IOException
     */
    private static void doUnzipFile(ZipInputStream zipInput, File dest) throws IOException {
        if (!dest.exists()) {
            FileUtils.forceMkdir(dest);
        }
        for (ZipEntry entry; (entry = zipInput.getNextEntry()) != null;) {
            String entryName = entry.getName();

            File file = new File(dest, entry.getName());
            if (entryName.endsWith(File.separator)) {
                FileUtils.forceMkdir(file);
            } else {
                OutputStream out = FileUtils.openOutputStream(file);
                try {
                    IOUtils.copy(zipInput, out);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            zipInput.closeEntry();
        }
    }

    //    // 
    //    public static void main(String[] args) {
    //        try {
    ////         zipFile("M:\\ZIP\\test\\tempZIP", "M:\\ZIP\\test\\bbc.zip");
    //            unzipFile("M:\\ZIP\\test\\bbc.zip", "M:\\ZIP\\test\\bb\\");
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //    }
}