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

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

Introduction

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

Prototype

public ArchiveException(String message, Exception cause) 

Source Link

Document

Constructs a new exception with the specified detail message and cause.

Usage

From source file:com.qwazr.tools.ArchiverTool.java

public void extract(File sourceFile, File destDir) throws IOException, ArchiveException {
    final InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));
    try {/*from  www .j  av  a  2 s.  c  om*/
        ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(is);
        try {
            ArchiveEntry entry;
            while ((entry = in.getNextEntry()) != null) {
                if (!in.canReadEntryData(entry))
                    continue;
                if (entry.isDirectory()) {
                    new File(destDir, entry.getName()).mkdir();
                    continue;
                }
                if (entry instanceof ZipArchiveEntry)
                    if (((ZipArchiveEntry) entry).isUnixSymlink())
                        continue;
                File destFile = new File(destDir, entry.getName());
                File parentDir = destFile.getParentFile();
                if (!parentDir.exists())
                    parentDir.mkdirs();
                IOUtils.copy(in, destFile);
            }
        } catch (IOException e) {
            throw new IOException("Unable to extract the archive: " + sourceFile.getPath(), e);
        } finally {
            IOUtils.closeQuietly(in);
        }
    } catch (ArchiveException e) {
        throw new ArchiveException("Unable to extract the archive: " + sourceFile.getPath(), e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.qwazr.library.archiver.ArchiverTool.java

public void extract(final Path sourceFile, final Path destDir) throws IOException, ArchiveException {
    try (final InputStream is = new BufferedInputStream(Files.newInputStream(sourceFile))) {
        try (final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(is)) {
            ArchiveEntry entry;//from   w  w  w. j a va2 s  .  co m
            while ((entry = in.getNextEntry()) != null) {
                if (!in.canReadEntryData(entry))
                    continue;
                if (entry.isDirectory()) {
                    final Path newDir = destDir.resolve(entry.getName());
                    if (!Files.exists(newDir))
                        Files.createDirectory(newDir);
                    continue;
                }
                if (entry instanceof ZipArchiveEntry)
                    if (((ZipArchiveEntry) entry).isUnixSymlink())
                        continue;
                final Path destFile = destDir.resolve(entry.getName());
                final Path parentDir = destFile.getParent();
                if (!Files.exists(parentDir))
                    Files.createDirectories(parentDir);
                final long entryLastModified = entry.getLastModifiedDate().getTime();
                if (Files.exists(destFile) && Files.isRegularFile(destFile)
                        && Files.getLastModifiedTime(destFile).toMillis() == entryLastModified
                        && entry.getSize() == Files.size(destFile))
                    continue;
                IOUtils.copy(in, destFile);
                Files.setLastModifiedTime(destFile, FileTime.fromMillis(entryLastModified));
            }
        } catch (IOException e) {
            throw new IOException("Unable to extract the archive: " + sourceFile.toAbsolutePath(), e);
        }
    } catch (ArchiveException e) {
        throw new ArchiveException("Unable to extract the archive: " + sourceFile.toAbsolutePath(), e);
    }
}