a tool for detect file type
- reference https://www.garykessler.net/library/file_sigs.html
- reference xml/ooxml
#####1、build and publish to local
gradle clean build publishToMavenLocal -x test
#####2、gradle (gradle.org)
implementation 'io.github.kylin-hunter:k-file-detector:1.0.11'
#####3、maven (maven.apache.org)
<dependency>
<groupId>io.github.kylin-hunter</groupId>
<artifactId>k-file-detector</artifactId>
<version>1.0.11</version>
</dependency>
all supported file types: https://github.com/kylin-hunter/k-file-detector/blob/main/k-file-detector/src/main/resources/signature/file_types.yml
- by file
DetectResult detectResult = FileDetector.detect(new File("xxxx.xxx")); //by file
List<FileType> possibleFileTypes = detectResult.getPossibleFileTypes();
for (FileType fileType : possibleFileTypes) {
System.out.println("id=" + fileType.getId()); // file type id
System.out.println("extensions=" + fileType.getExtensions()); // the extensions, may be empty
System.out.println("desc=" + fileType.getDesc()); // the description
}
- by InputStream
try (InputStream in = new FileInputStream(new File("xxx.xxx"))) {
// if know the file name, you can invoke: FileDetector.detect(in,"xxx.xxx)
DetectResult detectResult = FileDetector.detect(in);
List<FileType> possibleFileTypes = detectResult.getPossibleFileTypes();
for (FileType fileType : possibleFileTypes) {
System.out.println("id=" + fileType.getId()); // file type id,
System.out.println("extensions=" + fileType.getExtensions()); // the extensions, may be empty
System.out.println("desc=" + fileType.getDesc()); // the description
}
}
- by bytes
byte[] content = FileUtils.readFileToByteArray(new File("xxx.xxx"));
// if know the file name, you can invoke: FileDetector.detect(content,"xxx.xxx)
DetectResult detectResult = FileDetector.detect(content);
List<FileType> possibleFileTypes = detectResult.getPossibleFileTypes();
for (FileType fileType : possibleFileTypes) {
System.out.println("id=" + fileType.getId()); // file type id
System.out.println("extensions=" + fileType.getExtensions()); // the extensions, may be empty
System.out.println("desc=" + fileType.getDesc()); // the description
}