com.pactera.edg.am.metamanager.extractor.util.AntZip.java Source code

Java tutorial

Introduction

Here is the source code for com.pactera.edg.am.metamanager.extractor.util.AntZip.java

Source

/*
 * Copyright 2009 by pactera.edg.am Corporation. Address:HePingLi East Street No.11
 * 5-5, BeiJing,
 * 
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of pactera.edg.am
 * Corporation ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with pactera.edg.am.
 */

package com.pactera.edg.am.metamanager.extractor.util;

import java.io.BufferedInputStream;
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.io.OutputStream;
import java.util.Enumeration;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class AntZip {
    public static void zip(String sourceDir, String zipFile) {

        OutputStream os;

        try {

            os = new FileOutputStream(zipFile);

            BufferedOutputStream bos = new BufferedOutputStream(os);

            ZipOutputStream zos = new ZipOutputStream(bos);

            File file = new File(sourceDir);

            String basePath = null;

            if (file.isDirectory()) {

                basePath = file.getPath();

            } else {// ??

                basePath = file.getParent();

            }

            zipFile(file, basePath, zos);

            zos.closeEntry();

            zos.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    /**
     * ?zip
     * 
     * @param source
     * @param basePath
     *            
     * @param zos
     */

    private static void zipFile(File source, String basePath,

            ZipOutputStream zos) {

        File[] files = new File[0];

        if (source.isDirectory()) {

            files = source.listFiles();

        } else {

            files = new File[1];

            files[0] = source;

        }

        String pathName;// ()

        byte[] buf = new byte[1024];

        int length = 0;

        try {

            for (File file : files) {

                if (file.isDirectory()) {

                    pathName = file.getPath().substring(basePath.length() + 1)

                            + "/";

                    zos.putNextEntry(new ZipEntry(pathName));

                    zipFile(file, basePath, zos);

                } else {

                    pathName = file.getPath().substring(basePath.length() + 1);

                    InputStream is = new FileInputStream(file);

                    BufferedInputStream bis = new BufferedInputStream(is);

                    zos.putNextEntry(new ZipEntry(pathName));

                    while ((length = bis.read(buf)) > 0) {

                        zos.write(buf, 0, length);

                    }

                    is.close();

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    /**
     *  zip ? zip 
     * 
     * @param zipfile
     * @param destDir
     */

    public static void unZip(String zipfile, String destDir) {

        destDir = destDir.endsWith("\\") ? destDir : destDir + "\\";

        byte b[] = new byte[1024];

        int length;

        ZipFile zipFile;

        try {

            zipFile = new ZipFile(new File(zipfile));

            Enumeration enumeration = zipFile.getEntries();

            ZipEntry zipEntry = null;

            while (enumeration.hasMoreElements()) {

                zipEntry = (ZipEntry) enumeration.nextElement();

                File loadFile = new File(destDir + zipEntry.getName());

                if (zipEntry.isDirectory()) {

                    loadFile.mkdirs();

                } else {

                    if (!loadFile.getParentFile().exists()) {

                        loadFile.getParentFile().mkdirs();

                    }

                    OutputStream outputStream = new FileOutputStream(loadFile);

                    InputStream inputStream = zipFile.getInputStream(zipEntry);

                    while ((length = inputStream.read(b)) > 0)

                        outputStream.write(b, 0, length);

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    public static void zipFile(String inputFileName, HttpServletResponse response) {
        try {
            response.setContentType("text/plain;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=data.zip");
            response.setStatus(HttpServletResponse.SC_OK); //??
            OutputStream output = response.getOutputStream();
            ZipOutputStream zipOut = new ZipOutputStream(output);
            zip(zipOut, new File(inputFileName), "");
            if (zipOut != null)
                zipOut.close();
            if (output != null)
                output.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {

        }

    }

    private static void zip(ZipOutputStream out, File f, String base) {
        try {
            if (f.isDirectory()) {
                File[] fl = f.listFiles();
                if (base != null && !"".equals(base))
                    out.putNextEntry(new ZipEntry(base + "/"));
                base = base.length() == 0 ? "" : base + "/";
                for (int i = 0; i < fl.length; i++) {
                    zip(out, fl[i], base + fl[i].getName());
                }
            } else {
                out.putNextEntry(new ZipEntry(base));
                FileInputStream in = new FileInputStream(f);
                int b = -1;
                int buff = 0;
                while ((b = in.read()) != -1) {
                    out.write(b);
                    buff++;
                    if (buff > 10240) {
                        buff = 0;
                        out.flush();
                    }
                }
                out.flush();
                if (in != null) {
                    in.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}