List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream matches
public static boolean matches(byte[] signature, int length)
From source file:mj.ocraptor.extraction.tika.parser.pkg.ZipContainerDetector.java
private static MediaType detectArchiveFormat(byte[] prefix, int length) { try {/*w w w.ja va2s . c o m*/ ArchiveStreamFactory factory = new ArchiveStreamFactory(); ArchiveInputStream ais = factory.createArchiveInputStream(new ByteArrayInputStream(prefix, 0, length)); try { if ((ais instanceof TarArchiveInputStream) && !TarArchiveInputStream.matches(prefix, length)) { // ArchiveStreamFactory is too relaxed, see COMPRESS-117 return MediaType.OCTET_STREAM; } else { return PackageParser.getMediaType(ais); } } finally { IOUtils.closeQuietly(ais); } } catch (ArchiveException e) { return MediaType.OCTET_STREAM; } }
From source file:com.codenvy.commons.lang.TarUtils.java
public static boolean isTarFile(File file) throws IOException { if (file.isDirectory()) { return false; }//from w w w . j ava 2 s.co m // http://en.wikipedia.org/wiki/Tar_(computing)#File_header final byte[] header = new byte[512]; try (FileInputStream fIn = new FileInputStream(file)) { if (fIn.read(header) != header.length) { return false; } } return TarArchiveInputStream.matches(header, header.length); }
From source file:org.apache.tika.parser.pkg.TikaArchiveStreamFactory.java
/** * Try to determine the type of Archiver * @param in input stream/* w ww . j a v a 2s. co 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"); }