【发布时间】:2022-01-16 07:00:50
【问题描述】:
我有一个扩展名未知的文件。 有没有办法通过文件头找到扩展名的类型?
【问题讨论】:
我有一个扩展名未知的文件。 有没有办法通过文件头找到扩展名的类型?
【问题讨论】:
用法:
GetFileHeader.isPDF(filename)
GetFileHeader 类是用 java 写的,但你可以在 kotlin 中使用它
public class GetFileHeader {
private static final int PDF_MAGIC[] = new int[] { 0x25, 0x50, 0x44, 0x46};
private static final int DOC1_MAGIC[] = new int[] { 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1};
private static final int DOC2_MAGIC[] = new int[] { 0x0d, 0x44, 0x4f, 0x43};
private static final int DOC3_MAGIC[] = new int[] { 0xcf, 0x11, 0xe0, 0xa1, 0xb1,0x1a, 0xe1, 0x00};
private static final int DOC4_MAGIC[] = new int[] { 0xdb, 0xa5, 0x2d, 0x00};
private static final int DOC5_MAGIC[] = new int[] { 0xec, 0xa5, 0xc1, 0x00};
private static final int DOCX1_MAGIC[] = new int[] { 0x50, 0x4b, 0x03, 0x04};
private static final int DOCX2_MAGIC[] = new int[] { 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x06, 0x00};
public static boolean isPDF(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < PDF_MAGIC.length; ++i) {
if(ins.read() != PDF_MAGIC[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
public static boolean isDoc1(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < DOC1_MAGIC.length; ++i) {
if(ins.read() != DOC1_MAGIC[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
public static boolean isDoc2(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < DOC2_MAGIC.length; ++i) {
if(ins.read() != DOC2_MAGIC[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
public static boolean isDoc3(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < DOC3_MAGIC.length; ++i) {
if(ins.read() != DOC3_MAGIC[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
public static boolean isDoc4(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < DOC4_MAGIC.length; ++i) {
if(ins.read() != DOC4_MAGIC[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
public static boolean isDoc5(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < DOC5_MAGIC.length; ++i) {
if(ins.read() != DOC5_MAGIC[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
public static boolean isDocX1(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < DOCX1_MAGIC.length; ++i) {
if(ins.read() != DOCX1_MAGIC[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
public static boolean isDocX2(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < DOCX2_MAGIC.length; ++i) {
if(ins.read() != DOCX2_MAGIC[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
【讨论】:
你猜对了吗?每种格式都没有使用一个标准文件头。有些(如 .txt)没有标题。其他人都有自己的自定义标题。有时即使是标题也不够——不同类型的 .wav 和 .bmp 文件有多个子标题。如果您有猜测,您可以使用该格式具有的任何标头来测试该猜测,但如果您甚至没有猜测,您将一事无成。
文件头不是元数据,它只是(通常)文件的前 N 个字节。
【讨论】: