adams.core.io.LzfUtils.java Source code

Java tutorial

Introduction

Here is the source code for adams.core.io.LzfUtils.java

Source

/*
 *   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
 *   (at your option) 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.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * LzfUtils.java
 * Copyright (C) 2012-2015 University of Waikato, Hamilton, New Zealand
 */
package adams.core.io;

import com.ning.compress.lzf.LZFInputStream;
import com.ning.compress.lzf.LZFOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * Helper class for LZF related operations.
 *
 * @author  fracpete (fracpete at waikato dot ac dot nz)
 * @version $Revision$
 */
public class LzfUtils {

    /** the default extension. */
    public final static String EXTENSION = ".lzf";

    /**
     * Decompresses the specified lzma archive to a file without the ".lzf"
     * extension.
     *
     * @param archiveFile   the gzip file to decompress
     * @param buffer   the buffer size to use
     * @return      the error message, null if everything OK
     */
    public static String decompress(File archiveFile, int buffer) {
        return decompress(archiveFile, buffer,
                new PlaceholderFile(archiveFile.getAbsolutePath().replaceAll("\\" + EXTENSION + "$", "")));
    }

    /**
     * Decompresses the specified lzma archive.
     *
     * @param archiveFile   the lzma file to decompress
     * @param buffer   the buffer size to use
     * @param outputFile   the destination file
     * @return      the error message, null if everything OK
     */
    public static String decompress(File archiveFile, int buffer, File outputFile) {
        String result;
        LZFInputStream in;
        OutputStream out;
        FileInputStream fis;
        FileOutputStream fos;
        String msg;

        in = null;
        fis = null;
        out = null;
        fos = null;
        result = null;
        try {

            // does file already exist?
            if (outputFile.exists())
                System.err.println("WARNING: overwriting '" + outputFile + "'!");

            fis = new FileInputStream(archiveFile.getAbsolutePath());
            in = new LZFInputStream(fis);
            fos = new FileOutputStream(outputFile.getAbsolutePath());
            out = new BufferedOutputStream(fos);

            IOUtils.copy(in, out, buffer);
        } catch (Exception e) {
            msg = "Failed to decompress '" + archiveFile + "': ";
            System.err.println(msg);
            e.printStackTrace();
            result = msg + e;
        } finally {
            FileUtils.closeQuietly(in);
            FileUtils.closeQuietly(fis);
            FileUtils.closeQuietly(out);
            FileUtils.closeQuietly(fos);
        }

        return result;
    }

    /**
     * Compresses the specified lzma archive to a file with the ".lzf"
     * extension.
     *
     * @param inputFile   the file to compress
     * @param buffer   the buffer size to use
     * @return      the error message, null if everything OK
     */
    public static String compress(File inputFile, int buffer) {
        return compress(inputFile, buffer, new PlaceholderFile(inputFile.getAbsolutePath() + EXTENSION));
    }

    /**
     * Compresses the specified lzma archive. Does not remove the input file.
     *
     * @param inputFile   the file to compress
     * @param buffer   the buffer size to use
     * @param outputFile   the destination file (the archive)
     * @return      the error message, null if everything OK
     */
    public static String compress(File inputFile, int buffer, File outputFile) {
        return compress(inputFile, buffer, outputFile, false);
    }

    /**
     * Compresses the specified lzma archive.
     *
     * @param inputFile   the file to compress
     * @param buffer   the buffer size to use
     * @param outputFile   the destination file (the archive)
     * @param removeInput   whether to remove the input file
     * @return      the error message, null if everything OK
     */
    public static String compress(File inputFile, int buffer, File outputFile, boolean removeInput) {
        String result;
        LZFOutputStream out;
        BufferedInputStream in;
        FileInputStream fis;
        FileOutputStream fos;
        String msg;

        in = null;
        fis = null;
        out = null;
        fos = null;
        result = null;
        try {
            // does file already exist?
            if (outputFile.exists())
                System.err.println("WARNING: overwriting '" + outputFile + "'!");

            fis = new FileInputStream(inputFile.getAbsolutePath());
            in = new BufferedInputStream(fis);
            fos = new FileOutputStream(outputFile.getAbsolutePath());
            out = new LZFOutputStream(fos);

            IOUtils.copy(in, out, buffer);

            FileUtils.closeQuietly(in);
            FileUtils.closeQuietly(fis);
            FileUtils.closeQuietly(out);
            FileUtils.closeQuietly(fos);
            in = null;
            fis = null;
            out = null;
            fos = null;

            // remove input file?
            if (removeInput) {
                if (!inputFile.delete())
                    result = "Failed to delete input file '" + inputFile + "' after successful compression!";
            }
        } catch (Exception e) {
            msg = "Failed to compress '" + inputFile + "': ";
            System.err.println(msg);
            e.printStackTrace();
            result = msg + e;
        } finally {
            FileUtils.closeQuietly(in);
            FileUtils.closeQuietly(fis);
            FileUtils.closeQuietly(out);
            FileUtils.closeQuietly(fos);
        }

        return result;
    }
}