Need help with simplemagic?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

j256
141 Stars 38 Forks ISC License 376 Commits 24 Opened issues

Description

Simple file magic number and content-type library which provides mime-type determination from files and byte arrays

Services available

!
?

Need anything else?

Contributors list

# 63,174
Java
gRPC
totp
unix
340 commits
# 650,144
unix
Shell
HTML
Perl
5 commits
# 57,811
cropper...
React
filteri...
Groovy
3 commits
# 14,417
Koa
yml
opentyp...
opam
1 commit
# 209,363
Android
kicad
unix
HTML
1 commit
# 759,469
unix
Shell
HTML
Perl
1 commit
# 301,450
Scala
C
mime
cookboo...
1 commit

Java Simple Magic

Here's a "magic" number package which allows content-type (mime-type) determination from files and byte arrays. It makes use of the magic(5) Unix content-type files to implement the same functionality as the Unix file(1) command in Java which detects the contents of a file. It uses either internal config files or can read

/etc/magic
,
/usr/share/file/magic
, or other magic(5) files and determine file content from
File
,
InputStream
, or
byte[]
.
  • For more information, visit the home page.
  • The source code be found on the git repository. CircleCI CodeCov
  • Maven packages are published via Maven Central

Enjoy, Gray Watson

Getting Started

To get started you use the SimpleMagic package like the following:

java
// create a magic utility using the internal magic file
ContentInfoUtil util = new ContentInfoUtil();
// if you want to use a different config file(s), you can load them by hand:
// ContentInfoUtil util = new ContentInfoUtil("/etc/magic");
// ...
ContentInfo info = util.findMatch("/tmp/upload.tmp");
// or
ContentInfo info = util.findMatch(inputStream);
// or
ContentInfo info = util.findMatch(contentByteArray);
Once you have the
ContentInfo
it provides:
  • Enumerated type if the type is common
  • Approximate content-name
  • Full message produced by the magic file
  • Mime-type string if one configured by the config file
  • Associated file extensions (if any)

For example:

  • HTML, mime 'text/html', msg 'HTML document text'
  • Java, msg 'Java serialization data, version 5'
  • PDF, mime 'application/pdf', msg 'PDF document, version 1.4'
  • gzip, mime 'application/x-gzip', msg 'gzip compressed data, was "", from Unix...'
  • GIF, mime 'image/gif', msg 'GIF image data, version 89a, 16 x 16'
  • PNG, mime 'image/png', msg 'PNG image, 600 x 371, 8-bit/color RGB, non-interlaced'
  • ISO, mime 'audio/mp4', msg 'ISO Media, MPEG v4 system, iTunes AAC-LC'
  • Microsoft, mime 'application/msword', msg 'Microsoft Word Document'
  • RIFF, mime 'audio/x-wav', msg 'RIFF (little-endian) data, WAVE audio, Microsoft...'
  • JPEG, mime 'image/jpeg', msg 'JPEG image data, JFIF standard 1.01'

ChangeLog Release Notes

See the ChangeLog.txt file.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.