Example usage for org.apache.commons.compress.archivers ArchiveEntry isDirectory

List of usage examples for org.apache.commons.compress.archivers ArchiveEntry isDirectory

Introduction

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

Prototype

public boolean isDirectory();

Source Link

Document

True if the entry refers to a directory

Usage

From source file:org.robovm.maven.resolver.Archiver.java

public static void unarchive(Logger logger, File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;/* w w  w.ja v  a  2 s.  c  o m*/
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                logger.debug(f.getAbsolutePath());
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            f.setLastModified(entry.getLastModifiedDate().getTime());
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.robovm.templater.Templater.java

private void extractArchive(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;//  w  ww .  j  a  v  a 2  s. c o  m
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, substitutePlaceholdersInFileName(entry.getName()));
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
                substitutePlaceholdersInFile(f);
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.silverpeas.core.util.ZipUtil.java

private static void createPath(final ArchiveInputStream archiveStream, final ArchiveEntry archiveEntry,
        final File currentFile) throws IOException {
    try {/* ww w.ja v a 2s  . co m*/
        currentFile.getParentFile().mkdirs();
        if (archiveEntry.isDirectory()) {
            currentFile.mkdirs();
        } else {
            try (final FileOutputStream fos = new FileOutputStream(currentFile)) {
                IOUtils.copy(archiveStream, fos);
            }
        }
    } catch (FileNotFoundException ex) {
        SilverLogger.getLogger(ZipUtil.class).error("File not found " + currentFile.getPath(), ex);
    }
}

From source file:org.slc.sli.ingestion.landingzone.validation.ZipFileValidator.java

private static boolean isDirectory(ArchiveEntry zipEntry) {

    if (zipEntry.isDirectory()) {
        return true;
    }/*from  ww  w. j  a v  a2 s .  c om*/

    // UN: This check is to ensure that any zipping utility which does not pack a directory
    // entry
    // is verified by checking for a filename with '/'. Example: Windows Zipping Tool.
    if (zipEntry.getName().contains("/")) {
        return true;
    }

    return false;
}

From source file:org.sonatype.nexus.repository.r.internal.RDescriptionUtils.java

private static Map<String, String> extractMetadataFromArchive(final String archiveType, final InputStream is) {
    final ArchiveStreamFactory archiveFactory = new ArchiveStreamFactory();
    try (ArchiveInputStream ais = archiveFactory.createArchiveInputStream(archiveType, is)) {
        ArchiveEntry entry = ais.getNextEntry();
        while (entry != null) {
            if (!entry.isDirectory() && DESCRIPTION_FILE_PATTERN.matcher(entry.getName()).matches()) {
                return parseDescriptionFile(ais);
            }/*from  w  w  w  .  j  av  a  2 s . c o m*/
            entry = ais.getNextEntry();
        }
    } catch (ArchiveException | IOException e) {
        throw new RException(null, e);
    }
    throw new IllegalStateException("No metadata file found");
}

From source file:org.sourcepit.tools.shared.resources.internal.harness.SharedResourcesUtils.java

private static void importArchive(ZipArchiveInputStream zipIn, String archiveEntry, File outDir,
        boolean keepArchivePaths, String encoding, IFilteredCopier copier, IFilterStrategy strategy)
        throws IOException {
    boolean found = false;

    ArchiveEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        final String entryName = entry.getName();

        if (archiveEntry == null || entryName.startsWith(archiveEntry + "/")
                || entryName.equals(archiveEntry)) {
            found = true;/*  www . j a va  2 s .  c  o  m*/

            boolean isDir = entry.isDirectory();

            final String fileName;
            if (archiveEntry == null || keepArchivePaths) {
                fileName = entryName;
            } else {
                if (entryName.startsWith(archiveEntry + "/")) {
                    fileName = entryName.substring(archiveEntry.length() + 1);
                } else if (!isDir && entryName.equals(archiveEntry)) {
                    fileName = new File(entryName).getName();
                } else {
                    throw new IllegalStateException();
                }
            }

            final File file = new File(outDir, fileName);
            if (entry.isDirectory()) {
                file.mkdir();
            } else {
                file.createNewFile();
                OutputStream out = new FileOutputStream(file);
                try {
                    if (copier != null && strategy.filter(fileName)) {
                        copy(zipIn, out, encoding, copier, file);
                    } else {
                        IOUtils.copy(zipIn, out);
                    }
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        }
        entry = zipIn.getNextEntry();
    }

    if (!found) {
        throw new FileNotFoundException(archiveEntry);
    }
}

From source file:org.springframework.cloud.stream.app.tensorflow.util.ModelExtractor.java

/**
 * Traverses the Archive to find either an entry that matches the modelFileNameInArchive name (if not empty) or
 * and entry that ends in .pb if the modelFileNameInArchive is empty.
 *
 * @param modelFileNameInArchive Optional name of the archive entry that represents the frozen model file. If empty
 *                               the archive will be searched for the first entry that ends in .pb
 * @param archive Archive stream to be traversed
 * @return//from w  w w .  j  a v a 2  s  .com
 * @throws IOException
 */
private byte[] findInArchiveStream(String modelFileNameInArchive, ArchiveInputStream archive)
        throws IOException {
    ArchiveEntry entry;
    while ((entry = archive.getNextEntry()) != null) {
        //System.out.println(entry.getName() + " : " + entry.isDirectory());

        if (archive.canReadEntryData(entry) && !entry.isDirectory()) {
            if ((StringUtils.hasText(modelFileNameInArchive)
                    && entry.getName().endsWith(modelFileNameInArchive))
                    || (!StringUtils.hasText(modelFileNameInArchive)
                            && entry.getName().endsWith(this.frozenGraphFileExtension))) {
                return StreamUtils.copyToByteArray(archive);
            }
        }
    }
    throw new IllegalArgumentException("No model is found in the archive");
}

From source file:org.structr.websocket.command.UnarchiveCommand.java

private void unarchive(final SecurityContext securityContext, final File file)
        throws ArchiveException, IOException, FrameworkException {

    final App app = StructrApp.getInstance(securityContext);
    final InputStream is;

    try (final Tx tx = app.tx()) {

        final String fileName = file.getName();

        logger.log(Level.INFO, "Unarchiving file {0}", fileName);

        is = file.getInputStream();/*from   ww  w.ja v  a2s. c  o m*/
        tx.success();

        if (is == null) {

            getWebSocket().send(MessageBuilder.status().code(400)
                    .message("Could not get input stream from file ".concat(fileName)).build(), true);
            return;
        }
    }

    final ArchiveInputStream in = new ArchiveStreamFactory()
            .createArchiveInputStream(new BufferedInputStream(is));
    ArchiveEntry entry = in.getNextEntry();
    int overallCount = 0;

    while (entry != null) {

        try (final Tx tx = app.tx()) {

            int count = 0;

            while (entry != null && count++ < 50) {

                final String entryPath = "/" + PathHelper.clean(entry.getName());
                logger.log(Level.INFO, "Entry path: {0}", entryPath);

                final AbstractFile f = FileHelper.getFileByAbsolutePath(securityContext, entryPath);
                if (f == null) {

                    final Folder parentFolder = createOrGetParentFolder(securityContext, entryPath);
                    final String name = PathHelper.getName(entry.getName());

                    if (StringUtils.isNotEmpty(name) && (parentFolder == null
                            || !(FileHelper.getFolderPath(parentFolder).equals(entryPath)))) {

                        AbstractFile fileOrFolder = null;

                        if (entry.isDirectory()) {

                            fileOrFolder = app.create(Folder.class, name);

                        } else {

                            fileOrFolder = ImageHelper.isImageType(name)
                                    ? ImageHelper.createImage(securityContext, in, null, Image.class, name,
                                            false)
                                    : FileHelper.createFile(securityContext, in, null, File.class, name);
                        }

                        if (parentFolder != null) {
                            fileOrFolder.setProperty(AbstractFile.parent, parentFolder);
                        }

                        logger.log(Level.INFO, "Created {0} {1} with path {2}", new Object[] {
                                fileOrFolder.getType(), fileOrFolder, FileHelper.getFolderPath(fileOrFolder) });

                        // create thumbnails while importing data
                        if (fileOrFolder instanceof Image) {
                            fileOrFolder.getProperty(Image.tnMid);
                            fileOrFolder.getProperty(Image.tnSmall);
                        }

                    }
                }

                entry = in.getNextEntry();

                overallCount++;
            }

            logger.log(Level.INFO, "Committing transaction after {0} files..", overallCount);

            tx.success();
        }

    }

    in.close();

}

From source file:org.thingsboard.demo.loader.data.DemoData.java

private void readFromArchiveFile(String demoDataArchive, String email) throws Exception {
    InputStream inputStream = new BufferedInputStream(Files.newInputStream(Paths.get(demoDataArchive)));
    CompressorInputStream input = new CompressorStreamFactory().createCompressorInputStream(inputStream);
    ArchiveInputStream archInput = new ArchiveStreamFactory()
            .createArchiveInputStream(new BufferedInputStream(input));
    ArchiveEntry entry;

    byte[] pluginsContent = null;
    byte[] rulesContent = null;
    byte[] customersContent = null;
    byte[] devicesContent = null;
    List<byte[]> dashboardsContent = new ArrayList<>();
    while ((entry = archInput.getNextEntry()) != null) {
        if (!entry.isDirectory()) {
            String name = entry.getName();
            if (name.equals(DEMO_PLUGINS_JSON)) {
                pluginsContent = IOUtils.toByteArray(archInput);
            } else if (name.equals(DEMO_RULES_JSON)) {
                rulesContent = IOUtils.toByteArray(archInput);
            } else if (name.equals(DEMO_CUSTOMERS_JSON)) {
                customersContent = IOUtils.toByteArray(archInput);
            } else if (name.equals(DEMO_DEVICES_JSON)) {
                devicesContent = IOUtils.toByteArray(archInput);
            } else if (name.startsWith(DASHBOARDS_DIR + "/")) {
                byte[] dashboardContent = IOUtils.toByteArray(archInput);
                dashboardsContent.add(dashboardContent);
            }/*from www .j av a 2 s .  c  o m*/
        }
    }
    readData(email, pluginsContent, rulesContent, customersContent, devicesContent, dashboardsContent);
}

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

private void unpack(ArchiveInputStream inputStream) throws IOException {
    ArchiveEntry entry;
    while ((entry = inputStream.getNextEntry()) != null) {
        LOGGER.info("Extracting: {}", entry.getName());
        fileWriter.intoDestination(destinationDir + entry.getName()).withOverride(shouldOverrideFiles)
                .writeToFile(inputStream, entry.isDirectory());
    }//from  w ww  . j  a  va  2  s. c  om
}