Example usage for org.apache.commons.compress.utils IOUtils copy

List of usage examples for org.apache.commons.compress.utils IOUtils copy

Introduction

In this page you can find the example usage for org.apache.commons.compress.utils IOUtils copy.

Prototype

public static long copy(final InputStream input, final OutputStream output, int buffersize) throws IOException 

Source Link

Document

Copies the content of a InputStream into an OutputStream

Usage

From source file:adams.core.io.LzfUtils.java

/**
 * 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
 *//*from   ww  w.  j  av  a 2  s.c o  m*/
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;
}

From source file:adams.core.io.LzmaUtils.java

/**
 * 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
 *//*  w  w  w. ja va2  s.  c o m*/
@MixedCopyright(copyright = "Julien Ponge", license = License.APACHE2, url = "https://github.com/jponge/lzma-java/blob/master/README.md")
public static String decompress(File archiveFile, int buffer, File outputFile) {
    String result;
    LzmaInputStream 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 LzmaInputStream(new BufferedInputStream(fis), new Decoder());
        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;
}

From source file:adams.core.io.GzipUtils.java

/**
 * Decompresses the specified gzip archive.
 *
 * @param archiveFile   the gzip file to decompress
 * @param buffer   the buffer size to use
 * @param outputFile   the destination file
 * @return      the error message, null if everything OK
 *//*  w w  w . j  a v  a2s.  c o  m*/
@MixedCopyright(copyright = "Apache compress commons", license = License.APACHE2, url = "http://commons.apache.org/compress/apidocs/org/apache/commons/compress/compressors/CompressorStreamFactory.html")
public static String decompress(File archiveFile, int buffer, File outputFile) {
    String result;
    OutputStream out;
    InputStream fis;
    CompressorInputStream in;
    String msg;

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

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

        fis = new FileInputStream(archiveFile.getAbsolutePath());
        out = new FileOutputStream(outputFile.getAbsolutePath());
        in = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
        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);
    }

    return result;
}

From source file:adams.core.io.GzipUtils.java

/**
 * Decompresses the specified gzipped bytes.
 *
 * @param input   the gzip compressed bytes
 * @param buffer   the buffer size to use
 * @return      the decompressed bytes, null in case of error
 *//*w w w .j  a  v a2  s  .com*/
@MixedCopyright(copyright = "Apache compress commons", license = License.APACHE2, url = "http://commons.apache.org/compress/apidocs/org/apache/commons/compress/compressors/CompressorStreamFactory.html")
public static byte[] decompress(byte[] input, int buffer) {
    GZIPInputStream bis;
    ByteArrayOutputStream bos;

    try {
        bis = new GZIPInputStream(new ByteArrayInputStream(input));
        bos = new ByteArrayOutputStream();
        IOUtils.copy(bis, bos, buffer);
        return bos.toByteArray();
    } catch (Exception e) {
        System.err.println("Failed to decompress bytes!");
        e.printStackTrace();
        return null;
    }
}

From source file:com.baasbox.service.dbmanager.DbManagerService.java

public static void importDb(String appcode, ZipInputStream zis) throws FileFormatException, Exception {
    File newFile = null;/*from   w  ww.  ja v a  2s .  co m*/
    FileOutputStream fout = null;
    try {
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();
        if (ze == null)
            throw new FileFormatException("Looks like the uploaded file is not a valid export.");
        if (ze.isDirectory()) {
            ze = zis.getNextEntry();
        }
        if (ze != null) {
            newFile = File.createTempFile("export", ".json");
            fout = new FileOutputStream(newFile);
            IOUtils.copy(zis, fout, BBConfiguration.getImportExportBufferSize());
            fout.close();
        } else {
            throw new FileFormatException("Looks like the uploaded file is not a valid export.");
        }
        ZipEntry manifest = zis.getNextEntry();
        if (manifest != null) {
            File manifestFile = File.createTempFile("manifest", ".txt");
            fout = new FileOutputStream(manifestFile);
            for (int c = zis.read(); c != -1; c = zis.read()) {
                fout.write(c);
            }
            fout.close();
            String manifestContent = FileUtils.readFileToString(manifestFile);
            manifestFile.delete();
            Pattern p = Pattern.compile(BBInternalConstants.IMPORT_MANIFEST_VERSION_PATTERN);
            Matcher m = p.matcher(manifestContent);
            if (m.matches()) {
                String version = m.group(1);
                if (version.compareToIgnoreCase("0.6.0") < 0) { //we support imports from version 0.6.0
                    throw new FileFormatException(String.format(
                            "Current baasbox version(%s) is not compatible with import file version(%s)",
                            BBConfiguration.getApiVersion(), version));
                } else {
                    if (BaasBoxLogger.isDebugEnabled())
                        BaasBoxLogger.debug("Version : " + version + " is valid");
                }
            } else {
                throw new FileFormatException("The manifest file does not contain a version number");
            }
        } else {
            throw new FileFormatException("Looks like zip file does not contain a manifest file");
        }
        if (newFile != null) {
            DbHelper.importData(appcode, newFile);
            zis.closeEntry();
            zis.close();
        } else {
            throw new FileFormatException("The import file is empty");
        }
    } catch (FileFormatException e) {
        BaasBoxLogger.error(ExceptionUtils.getMessage(e));
        throw e;
    } catch (Throwable e) {
        BaasBoxLogger.error(ExceptionUtils.getStackTrace(e));
        throw new Exception("There was an error handling your zip import file.", e);
    } finally {
        try {
            if (zis != null) {
                zis.close();
            }
            if (fout != null) {
                fout.close();
            }
        } catch (IOException e) {
            // Nothing to do here
        }
    }
}

From source file:adams.core.io.LzfUtils.java

/**
 * 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
 *///from w w  w  . ja  v  a 2 s .  c o m
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;
}

From source file:adams.core.io.LzmaUtils.java

/**
 * 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
 *//*from  www .j  av a 2 s. c  o m*/
@MixedCopyright(copyright = "Julien Ponge", license = License.APACHE2, url = "https://github.com/jponge/lzma-java/blob/master/README.md")
public static String compress(File inputFile, int buffer, File outputFile, boolean removeInput) {
    String result;
    LzmaOutputStream 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 LzmaOutputStream.Builder(new BufferedOutputStream(fos)).build();

        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;
}

From source file:adams.core.io.GzipUtils.java

/**
 * Compresses the specified gzip archive.
 *
 * @param inputFile   the gzip 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
 *//* w ww  . j  a va  2 s  .co m*/
@MixedCopyright(copyright = "Apache compress commons", license = License.APACHE2, url = "http://commons.apache.org/compress/apidocs/org/apache/commons/compress/compressors/CompressorStreamFactory.html")
public static String compress(File inputFile, int buffer, File outputFile, boolean removeInput) {
    String result;
    FileInputStream in;
    FileOutputStream fos;
    CompressorOutputStream out;
    String msg;

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

        in = new FileInputStream(inputFile.getAbsolutePath());
        fos = new FileOutputStream(outputFile.getAbsolutePath());
        out = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        IOUtils.copy(in, out, buffer);

        FileUtils.closeQuietly(in);
        FileUtils.closeQuietly(out);
        FileUtils.closeQuietly(fos);
        in = 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(out);
        FileUtils.closeQuietly(fos);
    }

    return result;
}

From source file:fr.gael.dhus.datastore.processing.impl.ProcessProductPrepareDownload.java

/**
 * Creates a zip entry for the path specified with a name built from the base
 * passed in and the file/directory name. If the path is a directory, a 
 * recursive call is made such that the full directory is added to the zip.
 *
 * @param zOut The zip file's output stream
 * @param path The filesystem path of the file/directory being added
 * @param base The base prefix to for the name of the zip file entry
 *
 * @throws IOException If anything goes wrong
 *//*from w  w  w  .j  a va 2 s.  c  o m*/
private static void addFileToZip(ZipArchiveOutputStream zOut, String path, String base) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);

    zOut.putArchiveEntry(zipEntry);

    if (f.isFile()) {
        FileInputStream fInputStream = null;
        try {
            fInputStream = new FileInputStream(f);
            IOUtils.copy(fInputStream, zOut, 65535);
            zOut.closeArchiveEntry();
        } finally {
            fInputStream.close();
        }
    } else {
        zOut.closeArchiveEntry();
        File[] children = f.listFiles();

        if (children != null) {
            for (File child : children) {
                logger.debug("ZIP Adding " + child.getName());
                addFileToZip(zOut, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}

From source file:org.apache.ant.compress.taskdefs.PackBase.java

/**
 * packs a resource to an output stream/*from   ww w. jav a  2  s.c  o m*/
 * @throws IOException on error
 */
private void pack() {
    InputStream in = null;
    OutputStream out = null;
    try {
        in = src.getInputStream();
        out = StreamHelper.getOutputStream(factory, dest);
        if (out == null) {
            out = factory.getCompressorStream(new BufferedOutputStream(dest.getOutputStream()));
        }
        IOUtils.copy(in, out, BUFFER_SIZE);
    } catch (IOException e) {
        throw new BuildException("Error compressing " + src.getName() + " to " + dest.getName(), e);
    } finally {
        FileUtils.close(in);
        FileUtils.close(out);
    }
}