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:com.ifs.megaprofiler.helper.FileExtractor.java

/**
 * Obtains an apache compress {@link ArchiveInputStream} to the given
 * archive file./*from  w ww .java2s .  c  o m*/
 * 
 * @param src
 *            the archive file.
 * @return the stream.
 */
private static ArchiveInputStream getStream(String src) {
    FileInputStream fis = null;
    ArchiveInputStream is = null;

    try {

        fis = new FileInputStream(src);

        if (src.endsWith(".zip")) {

            is = new ZipArchiveInputStream(fis);

        } else {

            boolean zip = src.endsWith(".tgz") || src.endsWith(".gz");
            InputStream imp = (zip) ? new GZIPInputStream(fis, BUFFER_SIZE)
                    : new BufferedInputStream(fis, BUFFER_SIZE);
            is = new TarArchiveInputStream(imp, BUFFER_SIZE);

        }

    } catch (IOException e) {
        // LOG.warn(
        // "An error occurred while obtaining the stream to the archive '{}'. Error: {}",
        // src, e.getMessage() );
    }
    return is;
}

From source file:cc.arduino.utils.ArchiveExtractor.java

public void extract(File archiveFile, File destFolder, int stripPath, boolean overwrite)
        throws IOException, InterruptedException {

    // Folders timestamps must be set at the end of archive extraction
    // (because creating a file in a folder alters the folder's timestamp)
    Map<File, Long> foldersTimestamps = new HashMap<>();

    ArchiveInputStream in = null;/*  w  ww. j  a v a  2 s  . c  o m*/
    try {

        // Create an ArchiveInputStream with the correct archiving algorithm
        if (archiveFile.getName().endsWith("tar.bz2")) {
            in = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(archiveFile)));
        } else if (archiveFile.getName().endsWith("zip")) {
            in = new ZipArchiveInputStream(new FileInputStream(archiveFile));
        } else if (archiveFile.getName().endsWith("tar.gz")) {
            in = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(archiveFile)));
        } else if (archiveFile.getName().endsWith("tar")) {
            in = new TarArchiveInputStream(new FileInputStream(archiveFile));
        } else {
            throw new IOException("Archive format not supported.");
        }

        String pathPrefix = "";

        Map<File, File> hardLinks = new HashMap<>();
        Map<File, Integer> hardLinksMode = new HashMap<>();
        Map<File, String> symLinks = new HashMap<>();
        Map<File, Long> symLinksModifiedTimes = new HashMap<>();

        // Cycle through all the archive entries
        while (true) {
            ArchiveEntry entry = in.getNextEntry();
            if (entry == null) {
                break;
            }

            // Extract entry info
            long size = entry.getSize();
            String name = entry.getName();
            boolean isDirectory = entry.isDirectory();
            boolean isLink = false;
            boolean isSymLink = false;
            String linkName = null;
            Integer mode = null;
            long modifiedTime = entry.getLastModifiedDate().getTime();

            {
                // Skip MacOSX metadata
                // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
                int slash = name.lastIndexOf('/');
                if (slash == -1) {
                    if (name.startsWith("._")) {
                        continue;
                    }
                } else {
                    if (name.substring(slash + 1).startsWith("._")) {
                        continue;
                    }
                }
            }

            // Skip git metadata
            // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html
            if (name.contains("pax_global_header")) {
                continue;
            }

            if (entry instanceof TarArchiveEntry) {
                TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
                mode = tarEntry.getMode();
                isLink = tarEntry.isLink();
                isSymLink = tarEntry.isSymbolicLink();
                linkName = tarEntry.getLinkName();
            }

            // On the first archive entry, if requested, detect the common path
            // prefix to be stripped from filenames
            if (stripPath > 0 && pathPrefix.isEmpty()) {
                int slash = 0;
                while (stripPath > 0) {
                    slash = name.indexOf("/", slash);
                    if (slash == -1) {
                        throw new IOException("Invalid archive: it must contain a single root folder");
                    }
                    slash++;
                    stripPath--;
                }
                pathPrefix = name.substring(0, slash);
            }

            // Strip the common path prefix when requested
            if (!name.startsWith(pathPrefix)) {
                throw new IOException("Invalid archive: it must contain a single root folder while file " + name
                        + " is outside " + pathPrefix);
            }
            name = name.substring(pathPrefix.length());
            if (name.isEmpty()) {
                continue;
            }
            File outputFile = new File(destFolder, name);

            File outputLinkedFile = null;
            if (isLink) {
                if (!linkName.startsWith(pathPrefix)) {
                    throw new IOException("Invalid archive: it must contain a single root folder while file "
                            + linkName + " is outside " + pathPrefix);
                }
                linkName = linkName.substring(pathPrefix.length());
                outputLinkedFile = new File(destFolder, linkName);
            }
            if (isSymLink) {
                // Symbolic links are referenced with relative paths
                outputLinkedFile = new File(linkName);
                if (outputLinkedFile.isAbsolute()) {
                    System.err.println(I18n.format(tr("Warning: file {0} links to an absolute path {1}"),
                            outputFile, outputLinkedFile));
                    System.err.println();
                }
            }

            // Safety check
            if (isDirectory) {
                if (outputFile.isFile() && !overwrite) {
                    throw new IOException(
                            "Can't create folder " + outputFile + ", a file with the same name exists!");
                }
            } else {
                // - isLink
                // - isSymLink
                // - anything else
                if (outputFile.exists() && !overwrite) {
                    throw new IOException("Can't extract file " + outputFile + ", file already exists!");
                }
            }

            // Extract the entry
            if (isDirectory) {
                if (!outputFile.exists() && !outputFile.mkdirs()) {
                    throw new IOException("Could not create folder: " + outputFile);
                }
                foldersTimestamps.put(outputFile, modifiedTime);
            } else if (isLink) {
                hardLinks.put(outputFile, outputLinkedFile);
                hardLinksMode.put(outputFile, mode);
            } else if (isSymLink) {
                symLinks.put(outputFile, linkName);
                symLinksModifiedTimes.put(outputFile, modifiedTime);
            } else {
                // Create the containing folder if not exists
                if (!outputFile.getParentFile().isDirectory()) {
                    outputFile.getParentFile().mkdirs();
                }
                copyStreamToFile(in, size, outputFile);
                outputFile.setLastModified(modifiedTime);
            }

            // Set file/folder permission
            if (mode != null && !isSymLink && outputFile.exists()) {
                platform.chmod(outputFile, mode);
            }
        }

        for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
            if (entry.getKey().exists() && overwrite) {
                entry.getKey().delete();
            }
            platform.link(entry.getValue(), entry.getKey());
            Integer mode = hardLinksMode.get(entry.getKey());
            if (mode != null) {
                platform.chmod(entry.getKey(), mode);
            }
        }

        for (Map.Entry<File, String> entry : symLinks.entrySet()) {
            if (entry.getKey().exists() && overwrite) {
                entry.getKey().delete();
            }
            platform.symlink(entry.getValue(), entry.getKey());
            entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()));
        }

    } finally {
        IOUtils.closeQuietly(in);
    }

    // Set folders timestamps
    for (File folder : foldersTimestamps.keySet()) {
        folder.setLastModified(foldersTimestamps.get(folder));
    }
}

From source file:com.kalix.tools.kibana.KibanaController.java

/**
 * download kibana from remote server//  w  w w  .  j  a  va  2s.c  o m
 *
 * @throws Exception
 */
public void download() throws Exception {
    File target = new File(workingDirectory, KIBANA_FOLDER);
    if (target.exists()) {
        LOGGER.warn("Kibana folder already exists, download is skipped");
        return;
    }
    LOGGER.debug("Downloading Kibana from {}", KIBANA_LOCATION);
    if (isWindows()) {
        try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(
                new URL(KIBANA_LOCATION).openStream())) {
            ZipArchiveEntry entry;
            while ((entry = (ZipArchiveEntry) inputStream.getNextEntry()) != null) {
                File file = new File(workingDirectory, entry.getName());
                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    int read;
                    byte[] buffer = new byte[4096];
                    try (FileOutputStream outputStream = new FileOutputStream(file)) {
                        while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                            outputStream.write(buffer, 0, read);
                        }
                    }
                }
            }
        }
    } else {
        try (GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(
                new URL(KIBANA_LOCATION).openStream())) {
            try (TarArchiveInputStream inputStream = new TarArchiveInputStream(gzInputStream)) {
                TarArchiveEntry entry;
                while ((entry = (TarArchiveEntry) inputStream.getNextEntry()) != null) {
                    File file = new File(workingDirectory, entry.getName());
                    if (entry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        int read;
                        byte[] buffer = new byte[4096];
                        try (FileOutputStream outputStream = new FileOutputStream(file)) {
                            while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                                outputStream.write(buffer, 0, read);
                            }
                        }
                        file.setLastModified(entry.getLastModifiedDate().getTime());
                        if (entry instanceof TarArchiveEntry) {
                            int mode = ((TarArchiveEntry) entry).getMode();
                            if ((mode & 00100) > 0) {
                                file.setExecutable(true, (mode & 00001) == 0);
                            }
                        }
                    }
                }
            }
        }
    }
    overrideConfig();
}

From source file:com.glaf.core.util.ZipUtils.java

/**
 * //from   w  ww.j ava  2s  . c  om
 * zip
 * 
 * @param zipFilePath
 *            zip,  "/var/data/aa.zip"
 * 
 * @param saveFileDir
 *            ?, "/var/test/"
 */
public static void decompressZip(String zipFilePath, String saveFileDir) {
    if (isEndsWithZip(zipFilePath)) {
        File file = new File(zipFilePath);
        if (file.exists() && file.isFile()) {
            InputStream inputStream = null;
            ZipArchiveInputStream zais = null;
            try {
                inputStream = new FileInputStream(file);
                zais = new ZipArchiveInputStream(inputStream);
                ArchiveEntry archiveEntry = null;
                while ((archiveEntry = zais.getNextEntry()) != null) {
                    String entryFileName = archiveEntry.getName();
                    String entryFilePath = saveFileDir + entryFileName;
                    byte[] content = new byte[(int) archiveEntry.getSize()];
                    zais.read(content);
                    OutputStream os = null;
                    try {
                        File entryFile = new File(entryFilePath);
                        os = new BufferedOutputStream(new FileOutputStream(entryFile));
                        os.write(content);
                    } catch (IOException e) {
                        throw new IOException(e);
                    } finally {
                        IOUtils.closeStream(os);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeStream(zais);
                IOUtils.closeStream(inputStream);
            }
        }
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * Top unzip method. extract tarfile to constituent parts processing gzips 
 * along the way e.g. yyyyMMdd.zip->/yyyyMMdd/INode-CH_RNC01/A2010...zip
 *//*  w w  w . ja  va 2 s  .  c  o  m*/
protected void unzip1(File zipfile) throws FileNotFoundException {

    try {
        ZipArchiveInputStream zais = new ZipArchiveInputStream(new FileInputStream(zipfile));
        ZipArchiveEntry z1 = null;
        while ((z1 = zais.getNextZipEntry()) != null) {
            if (z1.isDirectory()) {
                /*hack to add vcc identifier because fucking ops cant rename a simple file*/
                if (z1.getName().contains("account"))
                    identifier = ".vcc";
                else
                    identifier = "";
            } else {
                String fn = z1.getName().substring(z1.getName().lastIndexOf("/"));
                File f = new File(getCalTempPath() + fn);
                FileOutputStream fos = new FileOutputStream(f);
                BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

                int n = 0;
                byte[] content = new byte[BUFFER];
                while (-1 != (n = zais.read(content))) {
                    fos.write(content, 0, n);
                }

                bos.flush();
                bos.close();
                fos.close();

                File unz = null;
                if (f.getName().endsWith("zip"))
                    unz = unzip3(f);
                else
                    unz = ungzip(f);

                if (unz != null)
                    allfiles.add(unz);
                f.delete();
            }
        }
        zais.close();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

From source file:ezbake.helpers.cdh.Cdh2EzProperties.java

public Configuration getConfiguration(InputStreamDataSource configStream) throws IOException {
    Configuration configuration = new Configuration(false);
    try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(configStream.getInputStream())) {
        ZipArchiveEntry zipEntry = zipInputStream.getNextZipEntry();
        while (zipEntry != null) {
            String name = zipEntry.getName();
            if (name.endsWith("core-site.xml") || name.endsWith("hdfs-site.xml")) {
                if (verbose)
                    System.err.println("Reading \"" + name + "\" into Configuration.");
                ByteArrayOutputStream boas = new ByteArrayOutputStream();
                IOUtils.copy(zipInputStream, boas);
                configuration.addResource(new ByteArrayInputStream(boas.toByteArray()), name);
            }// w ww  .  j a va2 s  .  c  o  m
            zipEntry = zipInputStream.getNextZipEntry();
        }
    }
    return configuration;
}

From source file:mj.ocraptor.extraction.tika.parser.iwork.IWorkPackageParser.java

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {
    ZipArchiveInputStream zip = new ZipArchiveInputStream(stream);
    ZipArchiveEntry entry = zip.getNextZipEntry();

    TikaImageHelper helper = new TikaImageHelper(metadata);
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    IWORKDocumentType type = null;//  w w w .  j av  a  2 s . c  o  m
    try {
        while (entry != null) {

            // ------------------------------------------------ //
            // -- handle image files
            // ------------------------------------------------ //
            String entryExtension = null;
            try {
                entryExtension = FilenameUtils.getExtension(new File(entry.getName()).getName());
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (entryExtension != null && FileType.isValidImageFileExtension(entryExtension)) {
                File imageFile = null;
                try {
                    imageFile = TikaImageHelper.saveZipEntryToTemp(zip, entry);
                    helper.addImage(imageFile);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (imageFile != null) {
                        imageFile.delete();
                    }
                }
            }

            // ------------------------------------------------ //
            // --
            // ------------------------------------------------ //

            if (!IWORK_CONTENT_ENTRIES.contains(entry.getName())) {
                entry = zip.getNextZipEntry();
                continue;
            }

            InputStream entryStream = new BufferedInputStream(zip, 4096);
            entryStream.mark(4096);
            type = IWORKDocumentType.detectType(entryStream);
            entryStream.reset();

            if (type != null) {
                ContentHandler contentHandler;

                switch (type) {
                case KEYNOTE:
                    contentHandler = new KeynoteContentHandler(xhtml, metadata);
                    break;
                case NUMBERS:
                    contentHandler = new NumbersContentHandler(xhtml, metadata);
                    break;
                case PAGES:
                    contentHandler = new PagesContentHandler(xhtml, metadata);
                    break;
                case ENCRYPTED:
                    // We can't do anything for the file right now
                    contentHandler = null;
                    break;
                default:
                    throw new TikaException("Unhandled iWorks file " + type);
                }

                metadata.add(Metadata.CONTENT_TYPE, type.getType().toString());
                xhtml.startDocument();
                if (contentHandler != null) {
                    context.getSAXParser().parse(new CloseShieldInputStream(entryStream),
                            new OfflineContentHandler(contentHandler));
                }
            }
            entry = zip.getNextZipEntry();
        }

        helper.addTextToHandler(xhtml);
        xhtml.endDocument();
    } catch (Exception e) {
        // TODO: logging
        e.printStackTrace();
    } finally {
        if (zip != null) {
            zip.close();
        }
        if (helper != null) {
            helper.close();
        }
    }
}

From source file:com.haulmont.cuba.core.app.importexport.EntityImportExport.java

@Override
public Collection<Entity> importEntitiesFromZIP(byte[] zipBytes, EntityImportView view) {
    Collection<Entity> result = new ArrayList<>();
    Collection<? extends Entity> entities;
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipBytes);
    ZipArchiveInputStream archiveReader = new ZipArchiveInputStream(byteArrayInputStream);
    try {// w  w w  . j a va 2 s  . c om
        try {
            while (archiveReader.getNextZipEntry() != null) {
                String json = new String(readBytesFromEntry(archiveReader), StandardCharsets.UTF_8);
                entities = entitySerialization.entitiesCollectionFromJson(json, null,
                        EntitySerializationOption.COMPACT_REPEATED_ENTITIES);
                result.addAll(importEntities(entities, view));
            }
        } catch (IOException e) {
            throw new RuntimeException("Exception occurred while importing report", e);
        }
    } finally {
        IOUtils.closeQuietly(archiveReader);
    }
    return result;
}

From source file:com.offbynull.coroutines.instrumenter.testhelpers.TestUtils.java

/**
 * Loads up a ZIP file that's contained in the classpath.
 * @param path path of ZIP resource/* w  w w .  j  a  v  a  2  s .  c o m*/
 * @return contents of files within the ZIP
 * @throws IOException if any IO error occurs
 * @throws NullPointerException if any argument is {@code null} or contains {@code null} elements
 * @throws IllegalArgumentException if {@code path} cannot be found, or if zipPaths contains duplicates
 */
public static Map<String, byte[]> readZipFromResource(String path) throws IOException {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL url = cl.getResource(path);
    Validate.isTrue(url != null);

    Map<String, byte[]> ret = new LinkedHashMap<>();

    try (InputStream is = url.openStream(); ZipArchiveInputStream zais = new ZipArchiveInputStream(is)) {
        ZipArchiveEntry entry;
        while ((entry = zais.getNextZipEntry()) != null) {
            ret.put(entry.getName(), IOUtils.toByteArray(zais));
        }
    }

    return ret;
}

From source file:com.github.jarlakxen.embedphantomjs.PhantomJSReference.java

private void downloadPhantomJS(File binaryFile) throws IOException {
    Properties properties = new Properties();
    properties.load(this.getClass().getClassLoader().getResourceAsStream(PHANTOMJS_DATA_FILE));

    String name = properties.getProperty(this.getVersion().getDescription() + "." + this.getHostOs() + ".name");

    String architecture = this.getArchitecture().indexOf("64") >= 0 ? "x86_64" : "i686";

    LOGGER.debug("System Data: Arch [" + architecture + "] - OS [" + this.getHostOs() + "]");

    if (this.getHostOs().equals("linux")) {
        name = String.format(name, architecture);
    }//  w w  w  .j  ava2 s .  c  om

    // Download PhantomJS
    URL downloadPath = new URL(this.getDownloadUrl() + name);
    File phantomJsCompressedFile = new File(System.getProperty("java.io.tmpdir") + "/" + name);

    LOGGER.info("Downloading " + downloadPath.getPath() + " ...");

    FileUtils.copyURLToFile(downloadPath, phantomJsCompressedFile);

    ArchiveInputStream archiveInputStream = null;

    if (phantomJsCompressedFile.getName().endsWith(".zip")) {

        archiveInputStream = new ZipArchiveInputStream(new FileInputStream(phantomJsCompressedFile));

    } else if (phantomJsCompressedFile.getName().endsWith(".bz2")) {

        archiveInputStream = new TarArchiveInputStream(
                new BZip2CompressorInputStream(new FileInputStream(phantomJsCompressedFile)));

    } else if (phantomJsCompressedFile.getName().endsWith(".gz")) {

        archiveInputStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(new FileInputStream(phantomJsCompressedFile)));

    }

    ArchiveEntry entry;
    while ((entry = archiveInputStream.getNextEntry()) != null) {
        if (entry.getName().endsWith(PHANTOMJS_DOWNLOAD_BINARY_PATH)
                || entry.getName().toLowerCase().endsWith("phantomjs.exe")) {

            // Create target folder
            new File(this.getTargetInstallationFolder() + "/" + this.getVersion().getDescription()).mkdirs();

            FileUtils.forceMkdir(new File(binaryFile.getParent()));

            if (!binaryFile.exists()) {
                binaryFile.createNewFile();
            }

            binaryFile.setExecutable(true);
            binaryFile.setReadable(true);

            // Untar the binary file
            FileOutputStream outputBinary = new FileOutputStream(binaryFile);

            LOGGER.info("Un-compress download to " + downloadPath.getPath() + " ...");
            IOUtils.copy(archiveInputStream, outputBinary);

            outputBinary.close();
        }
    }

    archiveInputStream.close();
}