private static String perseMimeType(byte[] bytes){ try { MagicMatch match = Magic.getMagicMatch(bytes); String mimeType = match.getMimeType(); return mimeType; } catch (Exception e) { return null; } } }
byte[] data = FileUtils.toByteArray("file.msg"); MagicMatch match = Magic.getMagicMatch(data); String mimeType = match.getMimeType();
/** Get a match from a stream of data. */ public static MagicMatch getMagicMatch(final byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
/** Create a parser and initialize it. */ public static synchronized void initialize() throws MagicParseException { if (!initialized) { magicParser = new MagicParser(); magicParser.initialize(); // build hint map final Iterator<MagicMatcher> i = magicParser.getMatchers().iterator(); while (i.hasNext()) { final MagicMatcher matcher = i.next(); final String ext = matcher.getMatch().getExtension(); if (ext != null && !ext.trim().equals("")) { //$NON-NLS-1$ addHint(ext, matcher); } else if (matcher.getMatch().getType().equals("detector")) { //$NON-NLS-1$ final String[] exts = matcher.getDetectorExtensions(); for (final String ext2 : exts) { addHint(ext2, matcher); } } } initialized = true; } }
/** Get a match from a stream of data. */ public static MagicMatch getMagicMatch(final byte[] data, final boolean onlyMimeMatch) throws MagicParseException, MagicMatchNotFoundException, MagicException { if (!initialized) { initialize(); } final Collection<MagicMatcher> matchers = magicParser.getMatchers(); MagicMatcher matcher = null; MagicMatch match = null; final Iterator<MagicMatcher> i = matchers.iterator(); while (i.hasNext()) { matcher = i.next(); try { if ((match = matcher.test(data, onlyMimeMatch)) != null) { return match; } } catch (final Throwable e) { throw new MagicException(e); } } throw new MagicMatchNotFoundException(); } }
@Override protected Object clone() throws CloneNotSupportedException { final MagicMatcher clone = new MagicMatcher(); clone.setMatch((MagicMatch) this.match.clone()); final Iterator<MagicMatcher> i = this.subMatchers.iterator(); final ArrayList<MagicMatcher> sub = new ArrayList<>(); while (i.hasNext()) { final MagicMatcher m = i.next(); sub.add((MagicMatcher) m.clone()); } clone.setSubMatchers(sub); return clone; }
/** * print a magic match * * @param stream DOCUMENT ME! * @param matcher DOCUMENT ME! * @param spacing DOCUMENT ME! */ private static void printMagicMatcher(PrintStream stream, MagicMatcher matcher, String spacing) { stream.println(spacing + "name: " + matcher.getMatch().getDescription()); stream.println(spacing + "children: "); Collection matchers = matcher.getSubMatchers(); Iterator i = matchers.iterator(); while (i.hasNext()) { printMagicMatcher(stream, (MagicMatcher) i.next(), spacing + " "); } }
private String guessMimeTypeFromDescription(MagicMatch match) { if ("MS-DOS executable (EXE)".equals(match.getDescription())) { return "application/x-dosexec"; } return "???"; } }
/** Carga una clase excluyendo de la ruta de búsqueda de clases las URL que no correspondan con JAR. * @param className Nombre de la clase a cargar. * @return Clase cargada. * @throws ClassNotFoundException cuando no se encuentra la clase a cargar. */ static Class<?> classForName(final String className) throws ClassNotFoundException { return getCleanClassLoader().loadClass(className); }
private String getMimeMagic(Path filePath) { try { final MagicMatch match = Magic.getMagicMatch(filePath.toFile(), true, true); if (match == null) return null; return match.getMimeType(); } catch (MagicParseException | MagicMatchNotFoundException | MagicException e) { return null; } }
MagicMatch match = parser.getMagicMatch(f); System.out.println(match.getMimeType()) ;
/** * get a match from a stream of data * * @param data DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
/** * print a magic match * * @param stream DOCUMENT ME! * @param matcher DOCUMENT ME! * @param spacing DOCUMENT ME! */ private static void printMagicMatcher(PrintStream stream, MagicMatcher matcher, String spacing) { stream.println(spacing + "name: " + matcher.getMatch().getDescription()); stream.println(spacing + "children: "); Collection<MagicMatcher> matchers = matcher.getSubMatchers(); Iterator<MagicMatcher> i = matchers.iterator(); while (i.hasNext()) { printMagicMatcher(stream, (MagicMatcher) i.next(), spacing + " "); } }
/** * add a submatch to this magic match * * @param m a magic match */ public void addSubMatch(MagicMatch m) { log.debug("adding submatch '" + m.getDescription() + "' to '" + getDescription() + "'"); subMatches.add(m); }
MagicMatch match = Magic.getMagicMatch(your_byte_array); String mimeType = match.getMimeType(); if(mimeType.equals("image/x-emf")) { //here is emf } if(mimeType.equals("image/x-wmf")) { //here is wmf }
/** * get a match from a stream of data * * @param data DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
MagicMatch match = Magic.getMagicMatch(file.getBytes()); System.out.println(match.getMimeType());
/** * get a match from a file * * @param file the file to match content in * @param extensionHints whether or not to use extension to optimize order of content tests * * @return the MagicMatch object representing a match in the file * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(File file, boolean extensionHints) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(file, extensionHints, false); }
byte[] data = someData MagicMatch match = Magic.getMagicMatch(data); System.out.println( match.getMimeType());
/** * get a match from a file * * @param file the file to match content in * @param extensionHints whether or not to use extension to optimize order of content tests * * @return the MagicMatch object representing a match in the file * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(File file, boolean extensionHints) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(file, extensionHints, false); }