Example usage for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory

List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory.

Prototype

ArchiveStreamFactory

Source Link

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();//ww w  .  ja va2s.  com

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

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  w  ww . j av a2 s .  co  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:net.shopxx.util.CompressUtils.java

public static void archive(File[] srcFiles, File destFile, String archiverName) {
    Assert.notNull(destFile);/*from  w  w w.  ja v a  2  s .  co m*/
    Assert.state(!destFile.exists() || destFile.isFile());
    Assert.hasText(archiverName);

    File parentFile = destFile.getParentFile();
    if (parentFile != null) {
        parentFile.mkdirs();
    }
    ArchiveOutputStream archiveOutputStream = null;
    try {
        archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(archiverName,
                new BufferedOutputStream(new FileOutputStream(destFile)));
        if (ArrayUtils.isNotEmpty(srcFiles)) {
            for (File srcFile : srcFiles) {
                if (srcFile == null || !srcFile.exists()) {
                    continue;
                }
                Set<File> files = new HashSet<File>();
                if (srcFile.isFile()) {
                    files.add(srcFile);
                }
                if (srcFile.isDirectory()) {
                    files.addAll(FileUtils.listFilesAndDirs(srcFile, TrueFileFilter.INSTANCE,
                            TrueFileFilter.INSTANCE));
                }
                String basePath = FilenameUtils.getFullPath(srcFile.getCanonicalPath());
                for (File file : files) {
                    try {
                        String entryName = FilenameUtils.separatorsToUnix(
                                StringUtils.substring(file.getCanonicalPath(), basePath.length()));
                        ArchiveEntry archiveEntry = archiveOutputStream.createArchiveEntry(file, entryName);
                        archiveOutputStream.putArchiveEntry(archiveEntry);
                        if (file.isFile()) {
                            InputStream inputStream = null;
                            try {
                                inputStream = new BufferedInputStream(new FileInputStream(file));
                                IOUtils.copy(inputStream, archiveOutputStream);
                            } catch (FileNotFoundException e) {
                                throw new RuntimeException(e.getMessage(), e);
                            } catch (IOException e) {
                                throw new RuntimeException(e.getMessage(), e);
                            } finally {
                                IOUtils.closeQuietly(inputStream);
                            }
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    } finally {
                        archiveOutputStream.closeArchiveEntry();
                    }
                }
            }
        }
    } catch (ArchiveException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }
}

From source file:com.ALC.SC2BOAserver.util.SC2BOAserverFileUtil.java

/**
 * This method extracts data to a given directory.
 * //from  w w w.  j a v a 2 s  .  c  om
 * @param directory the directory to extract into
 * @param zipIn input stream pointing to the zip file
 * @throws ArchiveException
 * @throws IOException
 * @throws FileNotFoundException
 */

public static void extractZipToDirectory(File directory, InputStream zipIn)
        throws ArchiveException, IOException, FileNotFoundException {

    ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", zipIn);
    while (true) {
        ZipArchiveEntry entry = (ZipArchiveEntry) in.getNextEntry();
        if (entry == null) {
            in.close();
            break;
        }
        //Skip empty files
        if (entry.getName().equals("")) {
            continue;
        }

        if (entry.isDirectory()) {
            File file = new File(directory, entry.getName());
            file.mkdirs();
        } else {
            File outFile = new File(directory, entry.getName());
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(outFile);
            IOUtils.copy(in, out);
            out.flush();
            out.close();
        }
    }
}

From source file:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java

public static void extractFiles(InputStream is, Path localPath) {
    ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
    try {/*  w  ww  .  j  a v a 2 s. co  m*/
        Files.createDirectories(localPath);
    } catch (IOException e) {
        throw new ArchiveExtractionException(e);
    }
    try (ArchiveInputStream ais = archiveStreamFactory.createArchiveInputStream(is);) {
        extractArchive(localPath, ais);
    } catch (ArchiveException e) {
        LOGGER.info("archiveFactory could not determine archive file type probably tar.gz");
        try (ArchiveInputStream ais = new TarArchiveInputStream(new GzipCompressorInputStream(is))) {
            extractArchive(localPath, ais);
        } catch (IOException e1) {
            throw new ArchiveExtractionException(e1);
        }
    } catch (IOException e) {
        throw new ArchiveExtractionException(e);
    }

}

From source file:com.jivesoftware.os.jive.utils.shell.utils.Untar.java

public static List<File> unTar(boolean verbose, final File outputDir, final File inputFile,
        boolean deleteOriginal) throws FileNotFoundException, IOException, ArchiveException {

    if (verbose) {
        System.out.println(String.format("untaring %s to dir %s.", inputFile.getAbsolutePath(),
                outputDir.getAbsolutePath()));
    }//  w ww  . ja v a 2s . c om

    final List<File> untaredFiles = new LinkedList<>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        String entryName = entry.getName();
        entryName = entryName.substring(entryName.indexOf("/") + 1);
        final File outputFile = new File(outputDir, entryName);
        if (entry.isDirectory()) {
            if (verbose) {
                System.out.println(String.format("Attempting to write output directory %s.",
                        getRelativePath(outputDir, outputFile)));
            }
            if (!outputFile.exists()) {
                if (verbose) {
                    System.out.println(String.format("Attempting to create output directory %s.",
                            getRelativePath(outputDir, outputFile)));
                }
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(String.format("Couldn't create directory %s.",
                            getRelativePath(outputDir, outputFile)));
                }
            }
        } else {
            try {
                if (verbose) {
                    System.out.println(
                            String.format("Creating output file %s.", getRelativePath(outputDir, outputFile)));
                }
                outputFile.getParentFile().mkdirs();
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();

                if (getRelativePath(outputDir, outputFile).contains("bin/")
                        || outputFile.getName().endsWith(".sh")) { // Hack!
                    if (verbose) {
                        System.out.println(
                                String.format("chmod +x file %s.", getRelativePath(outputDir, outputFile)));
                    }
                    outputFile.setExecutable(true);
                }

            } catch (Exception x) {
                System.err.println("failed to untar " + getRelativePath(outputDir, outputFile) + " " + x);
            }
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    if (deleteOriginal) {
        FileUtils.forceDelete(inputFile);
        if (verbose) {
            System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath()));
        }
    }
    return untaredFiles;
}

From source file:fr.gael.dhus.util.UnZip.java

public static void unCompress(String zip_file, String output_folder)
        throws IOException, CompressorException, ArchiveException {
    ArchiveInputStream ais = null;/*  w w  w  . jav a 2 s .co  m*/
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    FileInputStream fis = new FileInputStream(new File(zip_file));

    if (zip_file.toLowerCase().endsWith(".tar")) {
        ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
    } else if (zip_file.toLowerCase().endsWith(".zip")) {
        ais = asf.createArchiveInputStream(ArchiveStreamFactory.ZIP, fis);
    } else if (zip_file.toLowerCase().endsWith(".tgz") || zip_file.toLowerCase().endsWith(".tar.gz")) {
        CompressorInputStream cis = new CompressorStreamFactory()
                .createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
        ais = asf.createArchiveInputStream(new BufferedInputStream(cis));
    } else {
        try {
            fis.close();
        } catch (IOException e) {
            LOGGER.warn("Cannot close FileInputStream:", e);
        }
        throw new IllegalArgumentException("Format not supported: " + zip_file);
    }

    File output_file = new File(output_folder);
    if (!output_file.exists())
        output_file.mkdirs();

    // copy the existing entries
    ArchiveEntry nextEntry;
    while ((nextEntry = ais.getNextEntry()) != null) {
        File ftemp = new File(output_folder, nextEntry.getName());
        if (nextEntry.isDirectory()) {
            ftemp.mkdir();
        } else {
            FileOutputStream fos = FileUtils.openOutputStream(ftemp);
            IOUtils.copy(ais, fos);
            fos.close();
        }
    }
    ais.close();
    fis.close();
}

From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java

/**
 * Lower level pack operation//from   www  .  j a va  2s.  com
 * 
 * @param source
 *            source file
 * @param destination
 *            destination file
 * @return the number of bytes processed
 * @throws ArchiveException
 * @throws IOException
 */
public static long pack(String source, File destination) throws ArchiveException, IOException {

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    OutputStream outStream = new FileOutputStream(destination);

    ArchiveOutputStream outputStream;
    try {
        outputStream = factory.createArchiveOutputStream(ArchiveStreamFactory.TAR, outStream);
    } catch (ArchiveException e) {
        IOUtils.closeQuietly(outStream);

        throw e;
    }

    File startDirectory = new File(source);

    RecursiveArchiver archiver = new RecursiveArchiver(outputStream);

    try {
        archiver.archive(startDirectory);
        outputStream.flush();
        outputStream.close();

        outStream.flush();
        outStream.close();

    } catch (IOException e) {
        IOUtils.closeQuietly(outStream);
        IOUtils.closeQuietly(outputStream);

        throw e;
    }

    long ret = outputStream.getBytesWritten();

    if (logger.isDebugEnabled()) {
        logger.debug("Packed " + ret + " bytes");
    }

    return ret;
}

From source file:io.zatarox.satellite.impl.BackgroundWrapperTest.java

@BeforeClass
public static void setBefore() throws Exception {
    file = File.createTempFile("archive", ".jar");
    file.deleteOnExit();//  w ww . ja  v a  2s .  c om
    final FileOutputStream out = new FileOutputStream(file);
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            out);
    ZipArchiveEntry entry = new ZipArchiveEntry("META-INF/MANIFEST.MF");
    archive.putArchiveEntry(entry);
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().putValue("Background-Process-Class",
            FakeBackgroundProcessImpl.class.getName());
    manifest.write(archive);
    archive.closeArchiveEntry();
    archive.close();
}

From source file:fr.duminy.jbackup.core.archive.zip.ZipArchiveInputStream.java

ZipArchiveInputStream(InputStream input) throws Exception {
    this.input = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, input);
}