com.athena.chameleon.common.utils.ZipUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.athena.chameleon.common.utils.ZipUtil.java

Source

/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision History
 * Author         Date            Description
 * ---------------   ----------------   ------------
 * Sang-cheon Park   2012. 9. 22.      First Draft.
 */
package com.athena.chameleon.common.utils;

import java.io.File;
import java.io.IOException;

import org.apache.ant.compress.taskdefs.Unzip;
import org.apache.ant.compress.taskdefs.Zip;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.ManifestException;
import org.apache.tools.ant.types.FileSet;
import org.springframework.util.Assert;

import com.athena.chameleon.engine.entity.pdf.ArchiveType;

/**
 * <pre>
 * zip, ear, war, jar ??   ?  ? ?  ?
 * </pre>
 * 
 * @author Sang-cheon Park
 * @version 1.0
 */
public class ZipUtil {

    /**
     * <pre>
     * ?  ".zip"  .
     * </pre>
     * @param baseDir
     * @param type
     * @return
     * @throws IOException
     * @throws ManifestException 
     */
    public static boolean compress(String baseDir, ArchiveType type) throws IOException, ManifestException {
        return compress(baseDir, null, type);
    }//end of compress()

    /**
     * <pre>
     * ?  ? ? .
     * </pre>
     * @param baseDir
     * @param destFile
     * @param type
     * @return
     * @throws IOException
     * @throws ManifestException 
     */
    public static boolean compress(String baseDir, String destFile, ArchiveType type)
            throws IOException, ManifestException {
        Assert.notNull(baseDir, "baseDir cannot be null.");

        Project project = new Project();

        File filesetDir = new File(baseDir);
        File archiveFile = null;

        Assert.isTrue(filesetDir.exists(), baseDir + " does not exist.");
        Assert.isTrue(filesetDir.isDirectory(), baseDir + " is not a directory.");

        if (StringUtils.isEmpty(destFile)) {
            archiveFile = new File(filesetDir.getParent(),
                    new StringBuilder(filesetDir.getName()).append(".").append(type.value()).toString());
        } else {
            archiveFile = new File(destFile);
        }

        FileSet fileSet = new FileSet();
        fileSet.setDir(filesetDir);
        fileSet.setProject(project); // project ?  DirectoryScanner  ?  Excption? ?.

        Zip zip = new Zip();
        zip.setProject(project); // project ?  destFile   Exception ?.
        zip.setDestfile(archiveFile);
        zip.add(fileSet);

        zip.execute();

        return true;
    }//end of compress()

    /**
     * <pre>
     * ? ?? ? ? ?   . 
     * </pre>
     * @param sourceFile
     * @return
     * @throws IOException
     */
    public static boolean decompress(String sourceFile) throws IOException {
        return decompress(sourceFile, null);
    }//end of decompress()

    /**
     * <pre>
     * ? ?? ? ?  .
     * </pre>
     * @param sourceFile
     * @param destDir
     * @return
     * @throws IOException
     */
    public static boolean decompress(String sourceFile, String destDir) throws IOException {
        Assert.notNull(sourceFile, "sourceFile cannot be null.");

        File src = new File(sourceFile);
        File dest = null;

        Assert.isTrue(src.exists(), src + " does not exist.");
        Assert.isTrue(src.isFile(), sourceFile + " is not a file.");

        if (StringUtils.isEmpty(destDir)) {
            dest = new File(src.getParent(), src.getName().substring(0, src.getName().lastIndexOf(".")));
        } else {
            dest = new File(destDir);
        }

        Unzip unzip = new Unzip();
        unzip.setSrc(src);
        unzip.setDest(dest);
        unzip.execute();

        return true;
    }//end of decompress()

}// end of ZipUtil.java