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) 

Source Link

Document

Constructs a new exception with the specified detail message.

Usage

From source file:org.apache.tika.parser.pkg.TikaArchiveStreamFactory.java

@Override
public ArchiveInputStream createArchiveInputStream(final String archiverName, final InputStream in,
        final String actualEncoding) throws ArchiveException {

    if (archiverName == null) {
        throw new IllegalArgumentException("Archivername must not be null.");
    }//from w  w w.j  a v  a  2s . c o  m

    if (in == null) {
        throw new IllegalArgumentException("InputStream must not be null.");
    }

    if (AR.equalsIgnoreCase(archiverName)) {
        return new ArArchiveInputStream(in);
    }
    if (ARJ.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new ArjArchiveInputStream(in, actualEncoding);
        }
        return new ArjArchiveInputStream(in);
    }
    if (ZIP.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new ZipArchiveInputStream(in, actualEncoding);
        }
        return new ZipArchiveInputStream(in);
    }
    if (TAR.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new TarArchiveInputStream(in, actualEncoding);
        }
        return new TarArchiveInputStream(in);
    }
    if (JAR.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new JarArchiveInputStream(in, actualEncoding);
        }
        return new JarArchiveInputStream(in);
    }
    if (CPIO.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new CpioArchiveInputStream(in, actualEncoding);
        }
        return new CpioArchiveInputStream(in);
    }
    if (DUMP.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new DumpArchiveInputStream(in, actualEncoding);
        }
        return new DumpArchiveInputStream(in);
    }
    if (SEVEN_Z.equalsIgnoreCase(archiverName)) {
        throw new StreamingNotSupportedException(SEVEN_Z);
    }

    final ArchiveStreamProvider archiveStreamProvider = getArchiveInputStreamProviders()
            .get(toKey(archiverName));
    if (archiveStreamProvider != null) {
        return archiveStreamProvider.createArchiveInputStream(archiverName, in, actualEncoding);
    }

    throw new ArchiveException("Archiver: " + archiverName + " not found.");
}

From source file:org.apache.tika.parser.pkg.TikaArchiveStreamFactory.java

@Override
public ArchiveOutputStream createArchiveOutputStream(final String archiverName, final OutputStream out,
        final String actualEncoding) throws ArchiveException {
    if (archiverName == null) {
        throw new IllegalArgumentException("Archivername must not be null.");
    }//from ww  w  .ja v  a 2  s  .  co m
    if (out == null) {
        throw new IllegalArgumentException("OutputStream must not be null.");
    }

    if (AR.equalsIgnoreCase(archiverName)) {
        return new ArArchiveOutputStream(out);
    }
    if (ZIP.equalsIgnoreCase(archiverName)) {
        final ZipArchiveOutputStream zip = new ZipArchiveOutputStream(out);
        if (actualEncoding != null) {
            zip.setEncoding(actualEncoding);
        }
        return zip;
    }
    if (TAR.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new TarArchiveOutputStream(out, actualEncoding);
        }
        return new TarArchiveOutputStream(out);
    }
    if (JAR.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new JarArchiveOutputStream(out, actualEncoding);
        }
        return new JarArchiveOutputStream(out);
    }
    if (CPIO.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new CpioArchiveOutputStream(out, actualEncoding);
        }
        return new CpioArchiveOutputStream(out);
    }
    if (SEVEN_Z.equalsIgnoreCase(archiverName)) {
        throw new StreamingNotSupportedException(SEVEN_Z);
    }

    final ArchiveStreamProvider archiveStreamProvider = getArchiveOutputStreamProviders()
            .get(toKey(archiverName));
    if (archiveStreamProvider != null) {
        return archiveStreamProvider.createArchiveOutputStream(archiverName, out, actualEncoding);
    }

    throw new ArchiveException("Archiver: " + archiverName + " not found.");
}

From source file:org.apache.tika.parser.pkg.TikaArchiveStreamFactory.java

/**
 * Try to determine the type of Archiver
 * @param in input stream/* w  w  w.j  a v  a  2 s  . c o  m*/
 * @return type of archiver if found
 * @throws ArchiveException if an archiver cannot be detected in the stream
 * @since 1.14
 */
public static String detect(InputStream in) throws ArchiveException {
    if (in == null) {
        throw new IllegalArgumentException("Stream must not be null.");
    }

    if (!in.markSupported()) {
        throw new IllegalArgumentException("Mark is not supported.");
    }

    final byte[] signature = new byte[SIGNATURE_SIZE];
    in.mark(signature.length);
    int signatureLength = -1;
    try {
        signatureLength = IOUtils.readFully(in, signature);
        in.reset();
    } catch (IOException e) {
        throw new ArchiveException("IOException while reading signature.");
    }

    if (ZipArchiveInputStream.matches(signature, signatureLength)) {
        return ZIP;
    } else if (JarArchiveInputStream.matches(signature, signatureLength)) {
        return JAR;
    }
    if (ArArchiveInputStream.matches(signature, signatureLength)) {
        return AR;
    } else if (CpioArchiveInputStream.matches(signature, signatureLength)) {
        return CPIO;
    } else if (ArjArchiveInputStream.matches(signature, signatureLength)) {
        return ARJ;
    } else if (SevenZFile.matches(signature, signatureLength)) {
        return SEVEN_Z;
    }

    // Dump needs a bigger buffer to check the signature;
    final byte[] dumpsig = new byte[DUMP_SIGNATURE_SIZE];
    in.mark(dumpsig.length);
    try {
        signatureLength = IOUtils.readFully(in, dumpsig);
        in.reset();
    } catch (IOException e) {
        throw new ArchiveException("IOException while reading dump signature");
    }
    if (DumpArchiveInputStream.matches(dumpsig, signatureLength)) {
        return DUMP;
    }

    // Tar needs an even bigger buffer to check the signature; read the first block
    final byte[] tarHeader = new byte[TAR_HEADER_SIZE];
    in.mark(tarHeader.length);
    try {
        signatureLength = IOUtils.readFully(in, tarHeader);
        in.reset();
    } catch (IOException e) {
        throw new ArchiveException("IOException while reading tar signature");
    }
    if (TarArchiveInputStream.matches(tarHeader, signatureLength)) {
        return TAR;
    }

    // COMPRESS-117 - improve auto-recognition
    if (signatureLength >= TAR_HEADER_SIZE) {
        TarArchiveInputStream tais = null;
        try {
            tais = new TarArchiveInputStream(new ByteArrayInputStream(tarHeader));
            // COMPRESS-191 - verify the header checksum
            if (tais.getNextTarEntry().isCheckSumOK()) {
                return TAR;
            }
        } catch (final Exception e) { // NOPMD
            // can generate IllegalArgumentException as well
            // as IOException
            // autodetection, simply not a TAR
            // ignored
        } finally {
            IOUtils.closeQuietly(tais);
        }
    }
    throw new ArchiveException("No Archiver found for the stream signature");
}