org.shaf.lib.io.Zip.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.lib.io.Zip.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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.
 */
package org.shaf.lib.io;

import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.Path;
import org.shaf.core.anno.Option;
import org.shaf.core.anno.Tag;
import org.shaf.core.io.Location;

/**
 * The process for compressing data to ZIP archive.
 * 
 * @author Mykola Galushka
 */
@Tag(cmd = "zip", group = "io", descr = "Adds data to zip-archive.")
public class Zip extends IOProcess {

    @Option(name = "s", arg = "FILE|DIR", descr = "sets compressing file or directory")
    private Path source;

    @Option(name = "f", arg = "WILDCARD", descr = "sets wildcard for selecting files and directories", value = "NULL", required = false)
    private String wildcard;

    @Option(name = "t", arg = "ARCHIVE", descr = "sets archive file")
    private Path target;

    /**
     * Construct a new process for compressing data to ZIP archive.
     */
    public Zip() {
        super("compress");
    }

    /**
     * Performs data compression.
     */
    @Override
    protected Object logic() throws IOException {
        Location src = new Location(super.getFileSystem(), super.getBasePath(), this.source,
                this.wildcard.equals("NULL") ? null : this.wildcard).setQualifier("compressing data")
                        .shouldBeFound();
        Location trg = new Location(super.getFileSystem(), super.getBasePath(), this.target)
                .setQualifier("zip-archive");

        try (ZipOutputStream out = new ZipOutputStream(trg.getDataOutputStream())) {

            if (src.isDirectory()) {
                for (Location loc : src.getContent()) {
                    if (loc.isFile()) {
                        out.putNextEntry(new ZipEntry((src.isFilterDefined() ? "" : src.getName() + Path.SEPARATOR)
                                + loc.getPathAsString()));
                        try (FSDataInputStream in = loc.getDataInputStream()) {
                            super.copyBlock(in, out, loc.getSize());
                        }
                    }
                }
            } else {
                out.putNextEntry(new ZipEntry(src.getName()));
                try (FSDataInputStream in = src.getDataInputStream()) {
                    super.copyBlock(in, out, src.getSize());
                }
            }

        } catch (Exception exc) {
            throw new IOException("Failed to compress location " + this.source + " to archive " + this.target, exc);
        }

        return null;
    }
}