我有一個擴展名未知的檔案。有沒有辦法通過檔案頭找到擴展的型別?
uj5u.com熱心網友回復:
你有一個好的猜測嗎?每種格式都沒有使用一個標準檔案頭。有些(如 .txt)沒有標題。其他人都有自己的自定義標題。有時即使是標題也不夠——不同型別的 .wav 和 .bmp 檔案有多個子標題。如果您有一個猜測,您可以使用該格式具有的任何標頭來測驗該猜測,但如果您甚至沒有猜測,您將無處可去。
檔案的標頭不是元資料,它只是(通常)檔案的前 N ??個位元組。
uj5u.com熱心網友回復:
用法:
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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411985.html
標籤:
上一篇:啟動ReactNative:來自maven-metadata.xml的錯誤502
下一篇:Firebase資料未顯示在Recycler視圖中。如何在recyclerview中顯示來自firebase的用戶特定資料?
