【问题标题】:How to Get Raw File extension from Header in Kotlin如何从 Kotlin 中的 Header 获取原始文件扩展名
【发布时间】:2022-01-16 07:00:50
【问题描述】:

我有一个扩展名未知的文件。 有没有办法通过文件头找到扩展名的类型?

【问题讨论】:

    标签: java android kotlin


    【解决方案1】:

    用法:

    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();
            }
        }
    

    【讨论】:

      【解决方案2】:

      你猜对了吗?每种格式都没有使用一个标准文件头。有些(如 .txt)没有标题。其他人都有自己的自定义标题。有时即使是标题也不够——不同类型的 .wav 和 .bmp 文件有多个子标题。如果您有猜测,您可以使用该格式具有的任何标头来测试该猜测,但如果您甚至没有猜测,您将一事无成。

      文件头不是元数据,它只是(通常)文件的前 N ​​个字节。

      【讨论】:

      • 我想知道文件是pdf、doc、docx、zip、rar
      • 这个答案是针对java的,也不完整stackoverflow.com/a/29033128/16474396
      • 好的,如果你有一个这样的固定列表是可行的。你只需要单独检查每一个。幸运的是,该特定列表都是众所周知的文档格式,因此找到规范应该不难。例如,zip 文件格式将始终以 0x04034b50 作为前 4 个字节开始。请记住,虽然进行这样的检查意味着它很可能是一个 zip 文件,但它有可能不是一个有效的文件,或者它是某种其他格式并且恰好有前 4 个字节。
      • 老实说,大多数应用程序只是查看扩展名并信任它。这在绝大多数时候是正确的。如果您真的想解析和使用数据,只需进行防御性编码,以确保它们不会试图欺骗您进行某种内存访问漏洞利用。
      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2013-05-28
      相关资源
      最近更新 更多