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
164 Stars 40 Forks ISC License 383 Commits 25 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

# 137,701
totp
mime
CSS
Google ...
18 commits
# 635,139
unix
Shell
HTML
Perl
5 commits
# 57,937
cropper...
React
Groovy
Spring
3 commits
# 5,460
yml
lisp-in...
imagema...
curry
1 commit
# 210,777
kicad
unix
Shell
HTML
1 commit
# 747,145
unix
Shell
HTML
Perl
1 commit
# 310,329
C
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 javadoc

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'

Maven Configuration

  • Maven packages are published via Maven Central
    com.j256.simplemagic
    simplemagic
    1.16

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.