Created
November 29, 2012 12:41
-
-
Save schoenobates/4168814 to your computer and use it in GitHub Desktop.
National Archives DROID embed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uk.gov.nationalarchives.droid.core.BinarySignatureIdentifier; | |
import uk.gov.nationalarchives.droid.core.SignatureParseException; | |
import uk.gov.nationalarchives.droid.core.interfaces.IdentificationRequest; | |
import uk.gov.nationalarchives.droid.core.interfaces.IdentificationResult; | |
import uk.gov.nationalarchives.droid.core.interfaces.IdentificationResultCollection; | |
import uk.gov.nationalarchives.droid.core.interfaces.RequestIdentifier; | |
import uk.gov.nationalarchives.droid.core.interfaces.resource.FileSystemIdentificationRequest; | |
import uk.gov.nationalarchives.droid.core.interfaces.resource.RequestMetaData; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URI; | |
public class DroidFileIdentifier { | |
private static final BinarySignatureIdentifier SIG_IDENTIFER = new BinarySignatureIdentifier(); | |
static { | |
SIG_IDENTIFER.setSignatureFile("DROID_SignatureFile_V65.xml"); | |
try { | |
SIG_IDENTIFER.init(); | |
} catch (SignatureParseException e) { | |
throw new ExceptionInInitializerError(e); | |
} | |
// set to -1 for whole file scan | |
SIG_IDENTIFER.setMaxBytesToScan(1024); | |
} | |
public static void displayMime(File file) throws IOException { | |
String fileName = file.getCanonicalPath(); | |
URI uri = file.toURI(); | |
RequestMetaData metaData = new RequestMetaData(file.length(), file.lastModified(), fileName); | |
RequestIdentifier identifier = new RequestIdentifier(uri); | |
InputStream in = null; | |
IdentificationRequest request = new FileSystemIdentificationRequest(metaData, identifier); | |
try { | |
in = new FileInputStream(file); | |
request.open(in); | |
IdentificationResultCollection sigResults = SIG_IDENTIFER.matchBinarySignatures(request); | |
IdentificationResultCollection extResults = SIG_IDENTIFER.matchExtensions(request, false); | |
for (IdentificationResult result : sigResults.getResults()) { | |
System.out.printf( | |
"File : %s => mime: %s, type name: %s\n", | |
file.getCanonicalPath(), | |
result.getMimeType(), | |
result.getName() | |
); | |
} | |
for (IdentificationResult result : extResults.getResults()) { | |
System.out.printf( | |
"File : %s => mime: %s, type name: %s\n", | |
file.getCanonicalPath(), | |
result.getMimeType(), | |
result.getName() | |
); | |
} | |
} catch (Exception e) { | |
throw new IOException(e); | |
} | |
return null; | |
} | |
public static void main(String[] args) throws Exception { | |
String[] files = new String[] { | |
"MyFile.txt" | |
}; | |
for (String file : files) { | |
DroidFileIdentifier.displayMime(new File(file)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<dependency> | |
<groupId>uk.gov.nationalarchives</groupId> | |
<artifactId>droid-core</artifactId> | |
<version>${version.droid}</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>uk.gov.nationalarchives</groupId> | |
<artifactId>droid-core-interfaces</artifactId> | |
<version>${version.droid}</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>uk.gov.nationalarchives</groupId> | |
<artifactId>droid-results</artifactId> | |
<version>${version.droid}</version> | |
</dependency> | |
<dependency> | |
<groupId>uk.gov.nationalarchives</groupId> | |
<artifactId>droid-command-line</artifactId> | |
<version>${version.droid}</version> | |
<scope>compile</scope> | |
</dependency> | |
</dependencies> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This small Gist was tested with DROID version 6.1. A signature file is available from here: https://raw.github.com/digital-preservation/droid/master/droid-results/src/main/resources/DROID_SignatureFile_V65.xml