Skip to content

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

License

Notifications You must be signed in to change notification settings

j256/simplemagic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

// 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
<dependency>
	<groupId>com.j256.simplemagic</groupId>
	<artifactId>simplemagic</artifactId>
	<version>1.17</version>
</dependency>

ChangeLog Release Notes

See the ChangeLog.txt file.

About

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

Topics

Resources

License

Stars

Watchers

Forks