Example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream

Introduction

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

Prototype

public ZipArchiveInputStream(InputStream inputStream) 

Source Link

Usage

From source file:org.slc.sli.ingestion.landingzone.ZipFileUtilTest.java

private void assertTargetMatchesZip(File targetDir, File zipFile) throws IOException {
    InputStream zipStream = null;
    ZipArchiveInputStream zipFileStrm = null;
    try {//from ww w. j a  v a 2s .  c  o m
        zipStream = new BufferedInputStream(new FileInputStream(zipFile));
        zipFileStrm = new ZipArchiveInputStream(zipStream);

        ArchiveEntry entry;
        ArrayList<String> zipFileSet = new ArrayList<String>();
        while ((entry = zipFileStrm.getNextEntry()) != null) {
            zipFileSet.add(File.separator + entry.getName().replace('/', File.separatorChar));
        }

        ArrayList<String> extractedFileSet = new ArrayList<String>();
        addExtractedFiles(targetDir, File.separator, extractedFileSet);
        Collections.sort(zipFileSet);
        Collections.sort(extractedFileSet);

        Assert.assertEquals(extractedFileSet, zipFileSet);
    } finally {
        IOUtils.closeQuietly(zipStream);
        IOUtils.closeQuietly(zipFileStrm);
    }
}

From source file:org.trustedanalytics.servicebroker.gearpump.service.file.ArchiverService.java

public void unzip(InputStream inputStream, boolean overrideFiles) throws IOException {
    this.shouldOverrideFiles = overrideFiles;
    try (ZipArchiveInputStream zipIn = new ZipArchiveInputStream(inputStream)) {
        unpack(zipIn);//from www.  j a  va  2 s .  c om
    }
}

From source file:org.ut.biolab.medsavant.shared.util.IOUtils.java

/**
 * Decompresses/unarchives the file given by 'f' to the destination path
 * given by dest, and returns a list of all the files contained within the archive.
 * If the input file is compressed, but not archived, the destination filename will be 
 * "dest/"+stripExtension(f.getName()).  If the input file is 
 * not compressed or archived, the input file will be moved to the new location 
 * given by dest, and returned in a one-element list.
 * /*  w  w  w  .j av  a2s.com*/
 * Note the file given by f will be deleted.
 *
 * @return A list of files that were decompressed from the input file f, or
 * a list containing the input file in its new location if the input file was not compressed/archived.
 * @throws IOException 
 * @see stripExtension
 */
public static List<File> decompressAndDelete(File f, File dest) throws IOException {
    String lcfn = f.getName().toLowerCase();
    InputStream is = new BufferedInputStream(new FileInputStream(f));
    List<File> files;

    //Detect compression type.
    if (lcfn.endsWith(".gz") || lcfn.endsWith(".tgz")) {
        is = new GzipCompressorInputStream(is, true);
    } else if (lcfn.endsWith(".bz2")) {
        is = new BZip2CompressorInputStream(is, true);
    }

    //Detect archive type.
    if (lcfn.endsWith(".tgz") || lcfn.endsWith(".tar.gz") || lcfn.endsWith(".tar")
            || lcfn.endsWith(".tar.bz2")) {
        is = new TarArchiveInputStream(is);
        files = unArchive(dest, (ArchiveInputStream) is);
    } else if (lcfn.endsWith(".zip")) {
        is = new ZipArchiveInputStream(is);
        files = unArchive(dest, (ArchiveInputStream) is);
    } else {
        String filename = f.getName();
        if (!(is instanceof BufferedInputStream)) {
            filename = stripExtension(f);
            if (filename == null) {
                filename = f.getName() + ".decompressed";
            }
        }

        File outputFile = new File(dest, filename);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputFile));
        if (!(is instanceof BufferedInputStream)) {
            IOUtils.copyStream(is, bos);
        } else {
            if (!moveFile(f, outputFile)) {
                throw new IOException(
                        "Couldn't move file " + f.getAbsolutePath() + " to " + outputFile.getAbsolutePath());
            }
        }

        bos.close();
        files = new ArrayList<File>(1);
        files.add(outputFile);
    }
    is.close();
    if (f.exists()) {
        f.delete();
    }
    return files;
}

From source file:org.waarp.common.tar.ZipUtility.java

/**
 * Extract all files from Tar into the specified directory
 * /*from  w  w  w .  j  av  a 2  s.c  om*/
 * @param tarFile
 * @param directory
 * @return the list of extracted filenames
 * @throws IOException
 */
public static List<String> unZip(File tarFile, File directory) throws IOException {
    List<String> result = new ArrayList<String>();
    InputStream inputStream = new FileInputStream(tarFile);
    ZipArchiveInputStream in = new ZipArchiveInputStream(inputStream);
    ZipArchiveEntry entry = in.getNextZipEntry();
    while (entry != null) {
        if (entry.isDirectory()) {
            entry = in.getNextZipEntry();
            continue;
        }
        File curfile = new File(directory, entry.getName());
        File parent = curfile.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        OutputStream out = new FileOutputStream(curfile);
        IOUtils.copy(in, out);
        out.close();
        result.add(entry.getName());
        entry = in.getNextZipEntry();
    }
    in.close();
    return result;
}

From source file:org.wso2.carbon.container.ArchiveExtractor.java

/**
 * Extract zip file in the system to a target Directory.
 *
 * @param path            path of the archive to be extract
 * @param targetDirectory where to extract to
 * @throws IOException on I/O error/*from   w w  w  .j  a va  2  s .c  om*/
 */
public static void extract(Path path, File targetDirectory) throws IOException {
    if (path.toString().endsWith("." + Constants.ZIP_EXTENSION)) {
        extract(new ZipArchiveInputStream(new FileInputStream(path.toFile())), targetDirectory);
    } else {
        throw new TestContainerException("Unknown packaging of distribution; only zip can be handled.");
    }
}

From source file:org.wso2.carbon.container.ArchiveExtractor.java

/**
 * Extract zip specified by url to target.
 *
 * @param sourceDistribution url of the archive
 * @param targetDirectory    path of the target
 * @throws IOException/*from  w  ww  .  ja v a 2  s .c  o  m*/
 */
private static void extractZipDistribution(URL sourceDistribution, File targetDirectory) throws IOException {
    extract(new ZipArchiveInputStream(sourceDistribution.openStream()), targetDirectory);
}

From source file:org.xwiki.contrib.confluence.filter.internal.ConfluenceXMLPackage.java

private void fromStream(InputStream stream) throws IOException {
    // Get temporary folder
    this.directory = File.createTempFile("confluencexml", "");
    this.directory.delete();
    this.directory.mkdir();
    this.temporaryDirectory = false;

    // Extract the zip
    ZipArchiveInputStream zais = new ZipArchiveInputStream(stream);
    for (ZipArchiveEntry zipEntry = zais.getNextZipEntry(); zipEntry != null; zipEntry = zais
            .getNextZipEntry()) {/*from  w  w  w .ja  va 2s  . co m*/
        if (!zipEntry.isDirectory()) {
            String path = zipEntry.getName();
            File file = new File(this.directory, path);

            FileUtils.copyInputStreamToFile(new CloseShieldInputStream(zais), file);
        }
    }
}

From source file:org.xwiki.extension.xar.internal.handler.packager.Packager.java

private XarMergeResult importXARToWiki(String comment, InputStream xarInputStream, WikiReference wikiReference,
        PackageConfiguration configuration)
        throws IOException, ComponentLookupException, XWikiException, FilterException {
    XarMergeResult mergeResult = new XarMergeResult();

    ZipArchiveInputStream zis = new ZipArchiveInputStream(xarInputStream);

    XWikiContext xcontext = this.xcontextProvider.get();

    String currentWiki = xcontext.getWikiId();
    try {/*from w  w  w .j  a v  a  2 s  .  c o m*/
        xcontext.setWikiId(wikiReference.getName());

        this.observation.notify(new XARImportingEvent(), null, xcontext);

        for (ArchiveEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (!entry.isDirectory()) {
                // Only import what should be imported
                if (!entry.getName().equals(XarModel.PATH_PACKAGE)
                        && (configuration.getEntriesToImport() == null
                                || configuration.getEntriesToImport().contains(entry.getName()))) {
                    XarEntryMergeResult entityMergeResult = importDocumentToWiki(comment, wikiReference, zis,
                            configuration);
                    if (entityMergeResult != null) {
                        mergeResult.addMergeResult(entityMergeResult);
                    }
                }
            }
        }
    } finally {
        this.observation.notify(new XARImportedEvent(), null, xcontext);

        xcontext.setWikiId(currentWiki);
    }

    return mergeResult;
}

From source file:org.xwiki.filter.confluence.xml.internal.ConfluenceXMLPackage.java

public ConfluenceXMLPackage(InputSource source) throws IOException, FilterException, XMLStreamException,
        FactoryConfigurationError, NumberFormatException, ConfigurationException {
    InputStream stream;/* ww w  .j  a v a 2 s .co m*/

    if (source instanceof InputStreamInputSource) {
        stream = ((InputStreamInputSource) source).getInputStream();
    } else {
        throw new FilterException(
                String.format("Unsupported input source of type [%s]", source.getClass().getName()));
    }

    try {
        // Get temporary folder
        this.directory = File.createTempFile("confluencexml", "");
        this.directory.delete();
        this.directory.mkdir();
        this.directory.deleteOnExit();

        // Extract the zip
        ZipArchiveInputStream zais = new ZipArchiveInputStream(stream);
        for (ZipArchiveEntry zipEntry = zais.getNextZipEntry(); zipEntry != null; zipEntry = zais
                .getNextZipEntry()) {
            if (!zipEntry.isDirectory()) {
                String path = zipEntry.getName();
                File file = new File(this.directory, path);

                if (path.equals("entities.xml")) {
                    this.entities = file;
                } else if (path.equals("exportDescriptor.properties")) {
                    this.descriptor = file;
                }

                FileUtils.copyInputStreamToFile(new CloseShieldInputStream(zais), file);
            }
        }
    } finally {
        source.close();
    }

    // Initialize

    createTree();
}

From source file:org.xwiki.wikistream.confluence.xml.internal.ConfluenceXMLPackage.java

public ConfluenceXMLPackage(InputSource source) throws IOException, WikiStreamException, XMLStreamException,
        FactoryConfigurationError, NumberFormatException, ConfigurationException {
    InputStream stream;/* w w  w  . j  a  v a 2 s. c o m*/

    if (source instanceof InputStreamInputSource) {
        stream = ((InputStreamInputSource) source).getInputStream();
    } else {
        throw new WikiStreamException(
                String.format("Unsupported input source of type [%s]", source.getClass().getName()));
    }

    try {
        // Get temporary folder
        this.directory = File.createTempFile("confluencexml", "");
        this.directory.delete();
        this.directory.mkdir();
        this.directory.deleteOnExit();

        // Extract the zip
        ZipArchiveInputStream zais = new ZipArchiveInputStream(stream);
        for (ZipArchiveEntry zipEntry = zais.getNextZipEntry(); zipEntry != null; zipEntry = zais
                .getNextZipEntry()) {
            if (!zipEntry.isDirectory()) {
                String path = zipEntry.getName();
                File file = new File(this.directory, path);

                if (path.equals("entities.xml")) {
                    this.entities = file;
                } else if (path.equals("exportDescriptor.properties")) {
                    this.descriptor = file;
                }

                FileUtils.copyInputStreamToFile(new CloseShieldInputStream(zais), file);
            }
        }
    } finally {
        source.close();
    }

    // Initialize

    createTree();
}