public static String detect()

in src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java [211:296]


    public static String detect(final 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 (final IOException e) {
            throw new ArchiveException("Failure reading signature.", (Throwable) e);
        }

        // For now JAR files are detected as ZIP files.
        if (ZipArchiveInputStream.matches(signature, signatureLength)) {
            return ZIP;
        }
        // For now JAR files are detected as ZIP files.
        if (JarArchiveInputStream.matches(signature, signatureLength)) {
            return JAR;
        }
        if (ArArchiveInputStream.matches(signature, signatureLength)) {
            return AR;
        }
        if (CpioArchiveInputStream.matches(signature, signatureLength)) {
            return CPIO;
        }
        if (ArjArchiveInputStream.matches(signature, signatureLength)) {
            return ARJ;
        }
        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 (final IOException e) {
            throw new ArchiveException("IOException while reading dump signature", (Throwable) e);
        }
        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 (final IOException e) {
            throw new ArchiveException("IOException while reading tar signature", (Throwable) e);
        }
        if (TarArchiveInputStream.matches(tarHeader, signatureLength)) {
            return TAR;
        }

        // COMPRESS-117
        if (signatureLength >= TAR_HEADER_SIZE) {
            try (TarArchiveInputStream inputStream = new TarArchiveInputStream(new ByteArrayInputStream(tarHeader))) {
                // COMPRESS-191 - verify the header checksum
                // COMPRESS-644 - do not allow zero byte file entries
                TarArchiveEntry entry = inputStream.getNextEntry();
                // try to find the first non-directory entry within the first 10 entries.
                int count = 0;
                while (entry != null && entry.isDirectory() && entry.isCheckSumOK() && count++ < TAR_TEST_ENTRY_COUNT) {
                    entry = inputStream.getNextEntry();
                }
                if (entry != null && entry.isCheckSumOK() && !entry.isDirectory() && entry.getSize() > 0 || count > 0) {
                    return TAR;
                }
            } catch (final Exception ignored) {
                // can generate IllegalArgumentException as well as IOException auto-detection, simply not a TAR ignored
            }
        }
        throw new ArchiveException("No Archiver found for the stream signature");
    }