Example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream matches

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream matches

Introduction

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

Prototype

public static boolean matches(byte[] signature, int length) 

Source Link

Document

Checks if the signature matches what is expected for a zip file.

Usage

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

/**
 * Try to determine the type of Archiver
 * @param in input stream//from ww  w  . j a  v a 2s . com
 * @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");
}

From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializer.java

boolean isArchiveStream(InputStream in) {
    if (in == null) {
        throw new IllegalArgumentException("Stream must not be null.");
    }//from  w  ww  . j a va  2  s.  c  om

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

    final byte[] signature = new byte[12];
    in.mark(signature.length);
    int signatureLength;
    try {
        signatureLength = IOUtils.readFully(in, signature);
        in.reset();
    } catch (IOException e) {
        throw new RuntimeException(String.format(ERR_UNMARSHALLING_STREAM, "<unknown>", e.getMessage()), e);
    }
    return ZipArchiveInputStream.matches(signature, signatureLength);
}

From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializerTest.java

@Test
public void testUnmarshalEntireState() throws Exception {
    // First, create a state file to unmarshal
    // 1. It must be a zip archive; unmarshalling a non-archive package state file isn't supported
    // 2. We use the liveMarshallerMap; no mocks.
    // 3. We use a live ArchiveStreamFactory
    underTest.setArchive(true);/*www .jav a 2 s  .c  o  m*/
    underTest.setMarshallerMap(liveMarshallerMap);
    underTest.setArxStreamFactory(new ZipArchiveStreamFactory());
    File tmp = File.createTempFile(this.getClass().getName() + "_UnmarshalEntireState", ".zip");
    OutputStream out = new FileOutputStream(tmp);
    underTest.serialize(state, out);
    assertTrue(tmp.exists() && tmp.length() > 1);

    // Verify that we wrote out a zip file.
    final byte[] signature = new byte[12];
    InputStream in = new BufferedInputStream(new FileInputStream(tmp));
    in.mark(signature.length);
    int bytesRead = org.apache.commons.compress.utils.IOUtils.readFully(in, signature);
    assertTrue(ZipArchiveInputStream.matches(signature, bytesRead));
    in.reset();

    // Create a new instance of PackageState, and deserialize the zip archive created above, which contains the
    // test objects from the {@link #state prepared instance} of PackageState
    PackageState state = new PackageState(); // a new instance of PackageState with no internal state
    underTest.deserialize(state, in);

    Map<StreamId, PropertyDescriptor> pds = SerializationAnnotationUtil
            .getStreamDescriptors(PackageState.class);
    assertTrue(pds.size() > 0);
    pds.keySet().stream().forEach(streamId -> {
        try {
            assertNotNull("Expected non-null value for PackageState field '" + pds.get(streamId).getName()
                    + "', " + "StreamId '" + streamId + "'", pds.get(streamId).getReadMethod().invoke(state));
        } catch (IllegalAccessException | InvocationTargetException e) {
            fail(e.getMessage());
        }
    });

    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tmp);
}

From source file:org.xwiki.filter.test.internal.ZIPFileAssertComparator.java

public static boolean isZip(File file) throws IOException {
    final byte[] signature = new byte[12];

    int signatureLength;
    try (FileInputStream stream = new FileInputStream(file)) {
        stream.mark(signature.length);/*from w w  w  . ja  va 2  s. c om*/
        signatureLength = stream.read(signature);
    }

    return ZipArchiveInputStream.matches(signature, signatureLength);
}

From source file:org.xwiki.filter.xar.internal.input.XARInputFilterStream.java

private Boolean isZip(InputStream stream) throws IOException {
    if (!stream.markSupported()) {
        // ZIP by default
        return null;
    }//  ww w  .  ja v a 2s.c  o  m

    final byte[] signature = new byte[12];
    stream.mark(signature.length);
    int signatureLength = stream.read(signature);
    stream.reset();

    return ZipArchiveInputStream.matches(signature, signatureLength);
}

From source file:org.xwiki.wikistream.test.internal.ZIPFileAssertComparator.java

public static boolean isZip(File file) throws IOException {
    final byte[] signature = new byte[12];

    FileInputStream stream = new FileInputStream(file);
    stream.mark(signature.length);/*w w  w .  j av  a2  s  . c o  m*/
    int signatureLength = stream.read(signature);
    stream.close();

    return ZipArchiveInputStream.matches(signature, signatureLength);
}