Example usage for org.apache.commons.compress.compressors CompressorException CompressorException

List of usage examples for org.apache.commons.compress.compressors CompressorException CompressorException

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors CompressorException CompressorException.

Prototype

public CompressorException(String message) 

Source Link

Document

Constructs a new exception with the specified detail message.

Usage

From source file:it.geosolutions.tools.compress.file.Extractor.java

/**
 * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
 * //from   w ww.jav a 2  s  . co  m
 * @param inputZipName
 *            - the input zip file
 * @param outputDirName
 *            - a directory with this name containing zip files will be created
 * 
 * @throws IOException
 * @throws CompressorException
 */
public static void unZip(String inputZipName, String outputDirName) throws IOException, CompressorException {
    if (inputZipName == null || outputDirName == null) {
        throw new CompressorException("Unzip: with null parameters");
    }

    final File outputDir = new File(outputDirName);
    final File inputZipFile = new File(inputZipName);

    unZip(inputZipFile, outputDir);
}

From source file:it.geosolutions.tools.compress.file.Extractor.java

public static void unZip(File inputZipFile, File outputDir) throws IOException, CompressorException {
    if (inputZipFile == null || outputDir == null) {
        throw new CompressorException("Unzip: with null parameters");
    }/*from ww w.  j  av a 2  s. c  o m*/

    if (!outputDir.exists()) {
        if (!outputDir.mkdirs()) {
            throw new CompressorException("Unzip: Unable to create directory structure: " + outputDir);
        }
    }

    final int BUFFER = Conf.getBufferSize();

    ZipInputStream zipInputStream = null;
    try {
        // Open Zip file for reading
        zipInputStream = new ZipInputStream(new FileInputStream(inputZipFile));
    } catch (FileNotFoundException fnf) {
        throw new CompressorException("Unzip: Unable to find the input zip file named: " + inputZipFile);
    }

    // extract file if not a directory
    BufferedInputStream bis = new BufferedInputStream(zipInputStream);
    // grab a zip file entry
    ZipEntry entry = null;
    while ((entry = zipInputStream.getNextEntry()) != null) {
        // Process each entry

        File currentFile = new File(outputDir, entry.getName());

        FileOutputStream fos = null;
        BufferedOutputStream dest = null;
        try {
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            fos = new FileOutputStream(currentFile);
            dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = bis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
        } catch (IOException ioe) {

        } finally {
            try {
                if (dest != null) {
                    dest.flush();
                    dest.close();
                }
                if (fos != null)
                    fos.close();
            } catch (IOException ioe) {
                throw new CompressorException(
                        "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage());
            }
        }
    }
    try {
        if (zipInputStream != null)
            zipInputStream.close();
    } catch (IOException ioe) {
        throw new CompressorException(
                "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage());
    }
    try {
        if (bis != null)
            bis.close();
    } catch (IOException ioe) {
        throw new CompressorException(
                "Unzip: unable to close the Buffered zipInputStream: " + ioe.getLocalizedMessage());
    }
}

From source file:it.geosolutions.tools.compress.file.Extractor.java

/**
 * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
 * //from  w w  w .j a  v a2 s.  c  o m
 */
public static void extractBz2(File in_file, File out_file) throws CompressorException {
    FileOutputStream out = null;
    BZip2CompressorInputStream zIn = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
        out = new FileOutputStream(out_file);
        fis = new FileInputStream(in_file);
        bis = new BufferedInputStream(fis);
        /*
         * int b = bis.read(); if (b != 'B') { throw new
         * CompressorException("Invalid bz2 file: "+in_file.getAbsolutePath()); } b =
         * bis.read(); if (b != 'Z') { throw new
         * CompressorException("Invalid bz2 file: "+in_file.getAbsolutePath()); }
         */
        zIn = new BZip2CompressorInputStream(bis);
        byte[] buffer = new byte[Conf.getBufferSize()];
        int count = 0;
        do {
            out.write(buffer, 0, count);
            count = zIn.read(buffer, 0, buffer.length);
        } while (count != -1);
    } catch (IOException ioe) {
        String msg = "Problem expanding bzip2 " + ioe.getMessage();
        throw new CompressorException(msg + in_file.getAbsolutePath());
    } finally {
        try {
            if (bis != null)
                bis.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (out != null)
                out.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (zIn != null)
                zIn.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
    }
}

From source file:it.geosolutions.tools.compress.file.Extractor.java

/**
 * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
 * // w w  w .java  2s . com
 *         Extract a GZip file to a tar
 * @param in_file
 *            the input bz2 file to extract
 * @param out_file
 *            the output tar file to extract to
 */
public static void extractGzip(File in_file, File out_file) throws CompressorException {
    FileOutputStream out = null;
    GZIPInputStream zIn = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
        out = new FileOutputStream(out_file);
        fis = new FileInputStream(in_file);
        bis = new BufferedInputStream(fis, Conf.getBufferSize());
        zIn = new GZIPInputStream(bis);
        byte[] buffer = new byte[Conf.getBufferSize()];
        int count = 0;
        while ((count = zIn.read(buffer, 0, Conf.getBufferSize())) != -1) {
            out.write(buffer, 0, count);
        }
    } catch (IOException ioe) {
        String msg = "Problem uncompressing Gzip " + ioe.getMessage() + " ";
        throw new CompressorException(msg + in_file.getAbsolutePath());
    } finally {
        try {
            if (bis != null)
                bis.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (out != null)
                out.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (zIn != null)
                zIn.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
    }
}

From source file:org.apache.tika.parser.pkg.TikaCompressorStreamFactory.java

public CompressorInputStream createCompressorInputStream(final String name, final InputStream in,
        final boolean actualDecompressConcatenated) throws CompressorException {
    if (name == null || in == null) {
        throw new IllegalArgumentException("Compressor name and stream must not be null.");
    }/* ww  w.  j a  va  2s . c o m*/

    try {

        if (GZIP.equalsIgnoreCase(name)) {
            return new GzipCompressorInputStream(in, actualDecompressConcatenated);
        }

        if (BZIP2.equalsIgnoreCase(name)) {
            return new BZip2CompressorInputStream(in, actualDecompressConcatenated);
        }

        if (XZ.equalsIgnoreCase(name)) {
            if (!XZUtils.isXZCompressionAvailable()) {
                throw new CompressorException("XZ compression is not available.");
            }
            return new XZCompressorInputStream(in, actualDecompressConcatenated);
        }

        if (LZMA.equalsIgnoreCase(name)) {
            if (!LZMAUtils.isLZMACompressionAvailable()) {
                throw new CompressorException("LZMA compression is not available");
            }
            try {
                return new SaferLZMACompressorInputStream(in);
            } catch (MemoryLimitException e) {
                throw new CompressorException("MemoryLimitException: " + e.getMessage(), e);
            }
        }

        if (PACK200.equalsIgnoreCase(name)) {
            return new Pack200CompressorInputStream(in);
        }

        if (SNAPPY_RAW.equalsIgnoreCase(name)) {
            return new SnappyCompressorInputStream(in);
        }

        if (SNAPPY_FRAMED.equalsIgnoreCase(name)) {
            return new FramedSnappyCompressorInputStream(in);
        }

        if (Z.equalsIgnoreCase(name)) {
            try {
                return new SaferZCompressorInputStream(in);
            } catch (TikaRuntimeMemoryLimitException e) {
                throw new CompressorException("MemoryLimitException: " + e.getMessage(), e);
            }
        }

        if (DEFLATE.equalsIgnoreCase(name)) {
            return new DeflateCompressorInputStream(in);
        }
        /*
        not currently supported
        if (LZ4_BLOCK.equalsIgnoreCase(name)) {
            return new BlockLZ4CompressorInputStream(in);
        }
                
        if (LZ4_FRAMED.equalsIgnoreCase(name)) {
            return new FramedLZ4CompressorInputStream(in, actualDecompressConcatenated);
        }
         */

    } catch (final IOException e) {
        throw new CompressorException("Could not create CompressorInputStream.", e);
    }

    final CompressorStreamProvider compressorStreamProvider = getCompressorInputStreamProviders()
            .get(toKey(name));
    if (compressorStreamProvider != null) {
        return compressorStreamProvider.createCompressorInputStream(name, in, actualDecompressConcatenated);
    }

    throw new CompressorException("Compressor: " + name + " not found.");
}