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) throws IOException 

Source Link

Document

Copies the content of a InputStream into an OutputStream.

Usage

From source file:de.fischer.thotti.core.distbuilder.CommonsCompressTest.java

public static void main(String[] args) throws IOException, ArchiveException {

    File output = new File("C:\\projekte\\thotti.master\\test.zip");
    File file1 = new File("C:\\projekte\\thotti.master\\pom.xml");
    File file2 = new File("C:\\projekte\\thotti.master\\todo.txt");

    final OutputStream out = new FileOutputStream(output);
    ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);

    os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
    IOUtils.copy(new FileInputStream(file1), os);
    os.closeArchiveEntry();/*  w w  w . j a  v  a  2  s  . co  m*/

    os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
    IOUtils.copy(new FileInputStream(file2), os);
    os.closeArchiveEntry();
    out.flush();
    os.close();
}

From source file:com.jeffy.hdfs.compression.FileCompressor.java

/**
 * @param args/*from  w  ww .  j  a va2s  .  c o  m*/
 * ??????
 * ????
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    Configuration conf = new Configuration();
    //??
    CompressionCodecFactory factory = new CompressionCodecFactory(conf);
    // For example for the 'GzipCodec' codec class name the alias are 'gzip' and 'gzipcodec'.
    CompressionCodec codec = factory.getCodecByName(args[0]);
    if (codec == null) {//???
        System.err.println("Comperssion codec not found for " + args[0]);
        System.exit(1);
    }
    String ext = codec.getDefaultExtension();
    Compressor compressor = null;
    try {
        //?CodecPool?Compressor
        compressor = CodecPool.getCompressor(codec);
        for (int i = 1; i < args.length; i++) {
            String filename = args[i] + ext;
            System.out.println("Compression the file " + filename);
            try (FileSystem outFs = FileSystem.get(URI.create(filename), conf);
                    FileSystem inFs = FileSystem.get(URI.create(args[i]), conf);
                    InputStream in = inFs.open(new Path(args[i]))) {//
                //Compressor?
                CompressionOutputStream out = codec.createOutputStream(outFs.create(new Path(filename)),
                        compressor);
                //?????
                IOUtils.copy(in, out);
                out.finish();//?finish()?flush()???
                compressor.reset(); //???????java.io.IOException: write beyond end of stream
            }
        }
    } finally {//?Compressor??
        CodecPool.returnCompressor(compressor);
    }
}

From source file:com.chnoumis.commons.zip.utils.ZipUtils.java

public static void zip(List<ZipInfo> zipInfos, OutputStream out) throws IOException, ArchiveException {

    ArchiveOutputStream os = null;/*from   ww w.j av  a2s .c  o m*/

    try {
        os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);

        for (ZipInfo zipInfo : zipInfos) {
            os.putArchiveEntry(new ZipArchiveEntry(zipInfo.getFileName()));
            InputStream o = null;
            if (zipInfo.getFileContent() != null) {
                o = new ByteArrayInputStream(zipInfo.getFileContent());
            } else {
                o = zipInfo.getInputStream();
            }
            IOUtils.copy(o, os);
            os.closeArchiveEntry();
        }
    } finally {
        if (os != null) {
            os.close();
        }
    }
    out.close();
}

From source file:company.gonapps.loghut.utils.FileUtils.java

private static void addFileToArchive(TarArchiveOutputStream tarArchiveOutputStream, String path, String base)
        throws IOException {
    File file = new File(path);
    String entryName = base + file.getName();
    tarArchiveOutputStream.putArchiveEntry(new TarArchiveEntry(file, entryName));

    if (file.isFile()) {
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            IOUtils.copy(fileInputStream, tarArchiveOutputStream);
        }//from  w  w  w .ja  v a  2  s.  c  o m
        tarArchiveOutputStream.closeArchiveEntry();
    } else {
        tarArchiveOutputStream.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children)
                addFileToArchive(tarArchiveOutputStream, child.getAbsolutePath(), entryName + "/");
        }
    }
}

From source file:azkaban.project.DirectoryFlowLoaderTest.java

private static File decompressTarBZ2(InputStream is) throws IOException {
    File outputDir = Files.createTempDir();

    try (TarArchiveInputStream tais = new TarArchiveInputStream(new BZip2CompressorInputStream(is))) {
        TarArchiveEntry entry;/*from   w w  w.j a  v  a  2s .  c  o  m*/
        while ((entry = tais.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }

            File outputFile = new File(outputDir, entry.getName());
            File parent = outputFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }

            try (FileOutputStream os = new FileOutputStream(outputFile)) {
                IOUtils.copy(tais, os);
            }
        }

        return outputDir;
    }
}

From source file:com.bahmanm.karun.Utils.java

/**
 * Copies srcPath to destPath.// ww  w .  j  av  a2s . c o  m
 * 
 * @param srcPath Path to source file
 * @param destPath Path to destination file
 * @throws FileNotFoundException
 * @throws IOException 
 */
public synchronized static void copyFile(String srcPath, String destPath)
        throws FileNotFoundException, IOException {
    FileInputStream in = new FileInputStream(srcPath);
    FileOutputStream out = new FileOutputStream(destPath);
    IOUtils.copy(in, out);
    in.close();
    out.close();
}

From source file:com.googlecode.t7mp.util.ZipUtil.java

public static void unzip(InputStream warInputStream, File destination) {
    try {/*from   w  ww  . jav a  2  s.c  o m*/
        ZipArchiveInputStream in = null;
        try {
            in = new ZipArchiveInputStream(warInputStream);

            ZipArchiveEntry entry = null;
            while ((entry = in.getNextZipEntry()) != null) {
                File outfile = new File(destination.getCanonicalPath() + "/" + entry.getName());
                outfile.getParentFile().mkdirs();
                if (entry.isDirectory()) {
                    outfile.mkdir();
                    continue;
                }
                OutputStream o = new FileOutputStream(outfile);
                try {
                    IOUtils.copy(in, o);
                } finally {
                    o.close();
                }
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
        warInputStream.close();
    } catch (FileNotFoundException e) {
        throw new TomcatSetupException(e.getMessage(), e);
    } catch (IOException e) {
        throw new TomcatSetupException(e.getMessage(), e);
    }
}

From source file:edu.jhu.hlt.acute.archivers.tar.TarArchiver.java

@Override
public void addEntry(Archivable arch) throws IOException {
    final String fn = arch.getFileName();
    TarArchiveEntry entry = new TarArchiveEntry(fn);
    byte[] cbytes = arch.getBytes();
    entry.setSize(cbytes.length);/*from w ww  . j  a  va 2s. com*/
    this.tos.putArchiveEntry(entry);
    try (ByteArrayInputStream bis = new ByteArrayInputStream(cbytes)) {
        IOUtils.copy(bis, tos);
        tos.closeArchiveEntry();
    }
}

From source file:com.pinterest.deployservice.common.TarUtils.java

static void addInputStreamToTar(TarArchiveOutputStream taos, InputStream is, String path, long size, int mode)
        throws Exception {
    TarArchiveEntry entry = new TarArchiveEntry(path);
    entry.setSize(size);//  w w w.  j  a va 2s  .  c  om
    entry.setMode(mode);
    try {
        taos.putArchiveEntry(entry);
        IOUtils.copy(is, taos);
    } finally {
        taos.closeArchiveEntry();
        Closeables.closeQuietly(is);
    }
}

From source file:com.blackducksoftware.integration.hub.detect.util.DetectZipUtil.java

public static void unzip(File zip, File dest, Charset charset) throws IOException {
    Path destPath = dest.toPath();
    try (ZipFile zipFile = new ZipFile(zip, ZipFile.OPEN_READ, charset)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            Path entryPath = destPath.resolve(entry.getName());
            if (!entryPath.normalize().startsWith(dest.toPath()))
                throw new IOException("Zip entry contained path traversal");
            if (entry.isDirectory()) {
                Files.createDirectories(entryPath);
            } else {
                Files.createDirectories(entryPath.getParent());
                try (InputStream in = zipFile.getInputStream(entry)) {
                    try (OutputStream out = new FileOutputStream(entryPath.toFile())) {
                        IOUtils.copy(in, out);
                    }/* ww  w . ja  va 2s  .com*/
                }
            }
        }
    }
}