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

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

Introduction

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

Prototype

public String getName();

Source Link

Document

The name of the entry in the archive.

Usage

From source file:org.owasp.dependencycheck.utils.ExtractionUtil.java

/**
 * Extracts files from an archive./*from w  ww. j a  v a 2 s  . co  m*/
 *
 * @param input the archive to extract files from
 * @param destination the location to write the files too
 * @param filter determines which files get extracted
 * @throws ArchiveExtractionException thrown if there is an exception extracting files from the archive
 */
private static void extractArchive(ArchiveInputStream input, File destination, FilenameFilter filter)
        throws ArchiveExtractionException {
    ArchiveEntry entry;
    try {
        while ((entry = input.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                final File dir = new File(destination, entry.getName());
                if (!dir.exists() && !dir.mkdirs()) {
                    final String msg = String.format("Unable to create directory '%s'.", dir.getAbsolutePath());
                    throw new AnalysisException(msg);
                }
            } else {
                extractFile(input, destination, filter, entry);
            }
        }
    } catch (IOException ex) {
        throw new ArchiveExtractionException(ex);
    } catch (Throwable ex) {
        throw new ArchiveExtractionException(ex);
    } finally {
        closeStream(input);
    }
}

From source file:org.owasp.dependencycheck.utils.ExtractionUtil.java

/**
 * Extracts a file from an archive (input stream) and correctly builds the directory structure.
 *
 * @param input the archive input stream
 * @param destination where to write the file
 * @param filter the file filter to apply to the files being extracted
 * @param entry the entry from the archive to extract
 * @throws ExtractionException thrown if there is an error reading from the archive stream
 *//*w w  w.  j av  a  2s . com*/
private static void extractFile(ArchiveInputStream input, File destination, FilenameFilter filter,
        ArchiveEntry entry) throws ExtractionException {
    final File file = new File(destination, entry.getName());
    if (filter.accept(file.getParentFile(), file.getName())) {
        LOGGER.debug("Extracting '{}'", file.getPath());
        FileOutputStream fos = null;
        try {
            createParentFile(file);
            fos = new FileOutputStream(file);
            IOUtils.copy(input, fos);
        } catch (FileNotFoundException ex) {
            LOGGER.debug("", ex);
            final String msg = String.format("Unable to find file '%s'.", file.getName());
            throw new ExtractionException(msg, ex);
        } catch (IOException ex) {
            LOGGER.debug("", ex);
            final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
            throw new ExtractionException(msg, ex);
        } finally {
            closeStream(fos);
        }
    }
}

From source file:org.panbox.core.identitymgmt.VCardProtector.java

/**
 * Method extracts the VCF file stored within the zipped import file to the
 * given destination file. It further returns the corresponding hmac stored
 * within the archive.//from   w  w  w. j ava2 s. c o  m
 * 
 * @param sourceFile
 *            import archive
 * @param tmpFile
 *            temporary file to extract the CVF to
 * @return byte[] array containing the hmac of the VCF
 * @throws Exception
 */
public static byte[] unwrapVCF(File sourceFile, File tmpFile) throws FileNotFoundException, IOException {

    ZipArchiveInputStream in = null;
    FileOutputStream fos = null;
    String hmacString = null;
    try {

        in = new ZipArchiveInputStream(new FileInputStream(sourceFile));
        ArchiveEntry entry;
        // ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // ENTRY 1: vcard contents
        in.getNextEntry();
        fos = new FileOutputStream(tmpFile);
        IOUtils.copy(in, fos);

        // ENTRY 2: sha-256 hmac
        entry = in.getNextEntry();
        hmacString = entry.getName();

        return Utils.hexToBytes(hmacString);
    } catch (StringIndexOutOfBoundsException e) {
        logger.error("Error parsing hmac: " + hmacString + " is no valid hex String", e);
        throw e;
    } catch (Exception e) {
        logger.error("Error unwrapping VCF file", e);
        throw e;
    } finally {
        if (fos != null) {
            fos.flush();
            fos.close();
        }
        if (in != null) {
            in.close();
        }
    }
}

From source file:org.polymap.p4.data.importer.archive.ArchiveReader.java

protected void handleTar(File dir, String name, InputStream in) throws Exception {
    log.info("    TAR: " + name);
    try (TarArchiveInputStream tar = new TarArchiveInputStream(in, charset.get().name())) {
        ArchiveEntry entry = null;
        File subdir = dir;//  w  w  w . ja  va  2s.  c  o m
        while ((entry = tar.getNextEntry()) != null) {
            String entryName = FilenameUtils.getName(entry.getName());
            if (entry.isDirectory()) {
                subdir = new File(subdir, entryName);
                subdir.mkdir();
            } else {
                handle(subdir, entryName, null, tar);
            }
        }
    }
}

From source file:org.polymap.p4.data.imports.archive.ArchiveReader.java

protected void handleTar(String name, InputStream in) throws Exception {
    log.info("    TAR: " + name);
    try (TarArchiveInputStream tar = new TarArchiveInputStream(in, charset.get().name())) {
        ArchiveEntry entry = null;
        while ((entry = tar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
            } else {
                handle(entry.getName(), null, tar);
            }//w ww. j  ava2s .co  m
        }
    }
}

From source file:org.polymap.p4.imports.FileImporter.java

protected void handleTar(String name, InputStream in) throws Exception {
    log.info("    TAR: " + name);
    try (TarArchiveInputStream tar = new TarArchiveInputStream(in)) {
        ArchiveEntry entry = null;
        while ((entry = tar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
            } else {
                handle(entry.getName(), null, tar);
            }/*  ww  w  .j  av  a 2s  . c o m*/
        }
    }
}

From source file:org.powertac.logtool.LogtoolCore.java

/**
 * Reads state-log from given input stream using the DomainObjectReader.
 *///ww w . ja va  2s.co  m
public String readStateLog(InputStream inputStream, Analyzer... tools) {
    Reader inputReader;
    String line = null;

    log.info("Reading state log from stream for {}", tools[0].getClass().getName());
    simEnd = false;
    isInterrupted = false;

    try {
        // Stack compression logic if appropriate
        try {
            if (!inputStream.markSupported()) {
                inputStream = new BufferedInputStream(inputStream);
            }
            inputStream = compressFactory.createCompressorInputStream(inputStream);
        } catch (CompressorException x) {
            // Stream not compressed (or unknown compression scheme)
        }

        // Stack archive logic if appropriate
        try {
            if (!inputStream.markSupported()) {
                inputStream = new BufferedInputStream(inputStream);
            }
            ArchiveInputStream archiveStream = archiveFactory.createArchiveInputStream(inputStream);
            ArchiveEntry entry;
            inputStream = null;
            while ((entry = archiveStream.getNextEntry()) != null) {
                String name = entry.getName();
                if (entry.isDirectory() || !name.startsWith("log/") || !name.endsWith(".state")
                        || name.endsWith("init.state")) {
                    continue;
                }
                inputStream = archiveStream;
                break;
            }
            if (inputStream == null) {
                return "Cannot read archive, no valid state log entry";
            }
        } catch (ArchiveException x) {
            // Stream not archived (or unknown archiving scheme)
        }

        // Recycle repos from previous session
        List<DomainRepo> repos = SpringApplicationContext.listBeansOfType(DomainRepo.class);
        for (DomainRepo repo : repos) {
            repo.recycle();
        }

        // Now go read the state-log
        inputReader = new InputStreamReader(inputStream);
        for (Analyzer tool : tools) {
            log.info("Setting up {}", tool.getClass().getName());
            tool.setup();
        }
        BufferedReader in = new BufferedReader(inputReader);
        int lineNumber = 0;
        while (!simEnd) {
            synchronized (this) {
                if (isInterrupted) {
                    in.close();
                    break;
                }
            }
            line = in.readLine();
            if (null == line) {
                log.info("Last line " + lineNumber);
                break;
            }
            lineNumber += 1;
            reader.readObject(line);
        }
        builder.report();
        for (Analyzer tool : tools) {
            tool.report();
        }
    } catch (IOException e) {
        return "Error reading from stream";
    } catch (MissingDomainObject e) {
        return "MDO on " + line;
    }
    return null;
}

From source file:org.rapidbeans.rapidenv.Unpacker.java

private void unpackArchive(final File file, final File dest, final boolean compressed)
        throws IOException, ArchiveException, CompressorException {
    if (!dest.exists()) {
        if (!dest.mkdirs()) {
            throw new RapidEnvException(
                    "Error while trying to create destination directory: " + dest.getAbsolutePath());
        }// w  ww.  j  av  a  2  s .  co  m
    }
    InputStream uncompressedIs = null;
    ArchiveInputStream is = null;
    try {
        if (compressed) {
            uncompressedIs = new BufferedInputStream(new CompressorStreamFactory()
                    .createCompressorInputStream(new BufferedInputStream(new FileInputStream(file))));
        } else {
            uncompressedIs = new BufferedInputStream(new FileInputStream(file));
        }
        is = new ArchiveStreamFactory().createArchiveInputStream(uncompressedIs);
        ArchiveEntry entry = null;
        while ((entry = is.getNextEntry()) != null) {
            final File outFile = new File(dest, entry.getName());
            FileMode fileMode = null;
            if (entry instanceof TarArchiveEntry) {
                fileMode = new FileMode(((TarArchiveEntry) entry).getMode());
                RapidEnvInterpreter.log(Level.FINE, "unpacking tar entry: " + outFile.getAbsolutePath()
                        + ", Mode: " + fileMode.toString() + ", " + fileMode.toChmodStringFull());
            } else {
                RapidEnvInterpreter.log(Level.FINE, "unpacking file entry: " + outFile.getAbsolutePath());
            }
            if (entry.isDirectory() && (!outFile.exists())) {
                if (!outFile.mkdirs()) {
                    throw new RapidEnvException(
                            "Error while trying to create directory: " + outFile.getAbsolutePath());
                }
            } else {
                FileOutputStream os = null;
                try {
                    os = new FileOutputStream(outFile);
                    final byte[] buf = new byte[BUF_SIZE];
                    int readBytesCount;
                    while ((readBytesCount = is.read(buf)) != -1) {
                        os.write(buf, 0, readBytesCount);
                    }
                } finally {
                    if (os != null) {
                        os.close();
                    }
                }
            }
            if (fileMode != null) {
                setOutFileMode(file, fileMode);
            }
        }
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.rauschig.jarchivelib.CommonsArchiver.java

private void extract(ArchiveInputStream input, File destination) throws IOException {
    ArchiveEntry entry;
    while ((entry = input.getNextEntry()) != null) {
        File file = new File(destination, entry.getName());

        if (entry.isDirectory()) {
            file.mkdirs();/*from   ww w  .  ja v a  2s .  co  m*/
        } else {
            file.getParentFile().mkdirs();
            IOUtils.copy(input, file);
        }

        FileModeMapper.map(entry, file);
    }
}

From source file:org.robovm.eclipse.RoboVMPlugin.java

private static void extractTarGz(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;//from  w  w  w .  j  a v a 2 s  . com
    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 {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            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);
    }
}