美文网首页
OC 判断文件类型

OC 判断文件类型

作者: 微笑中的你 | 来源:发表于2020-06-08 19:42 被阅读0次
    
    - (void)getFileType {
        [self getHtml];
        [self getPng];
    }
    
    - (void)getPng {
        NSString * type = [self getFileTypeWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"png"]];
        
        NSLog(type);
    }
    
    - (void)getHtml {
        NSString * type = [self getFileTypeWithPath:[[NSBundle mainBundle] pathForResource:@"jsTest" ofType:@"html"]];
        
        NSLog(type);
    }
    
    - (NSString *)getFileTypeWithPath:(NSString *)path {
        NSData * data = [NSData dataWithContentsOfFile:path];
        return [self getDataType:data];
    }
    
    
    - (NSString *)getDataType:(NSData *)data {
    
        if (data.length < 2) {
            return @"unknown File";
        }
        
        int char1 = 0, char2 = 0;
        [data getBytes:&char1 range:NSMakeRange(0, 1)];
        [data getBytes:&char2 range:NSMakeRange(1, 1)];
        NSString *numStr = [NSString stringWithFormat:@"%d%d",char1,char2];
        NSString * type = nil;
        if ([numStr isEqualToString:@"255216"]) {
            type = @"jpg";
        } else if ([numStr isEqualToString:@"7173"]) {
            type = @"gif";
        } else if ([numStr isEqualToString:@"6677"]) {
            type = @"bmp";
        } else if ([numStr isEqualToString:@"13780"]) {
            type = @"png";
        } else if ([numStr isEqualToString:@"6787"]) {
            type = @"swf";
        } else if ([numStr isEqualToString:@"7790"]) {
            type = @"exe dll";
        } else if ([numStr isEqualToString:@"8297"]) {
            type = @"rar";
        } else if ([numStr isEqualToString:@"8075"]) {
            type = @"zip";
        } else if ([numStr isEqualToString:@"55122"]) {
            type = @"7z";
        } else if ([numStr isEqualToString:@"6063"]) {
            type = @"xml";
        } else if ([numStr isEqualToString:@"6033"]) {
            type = @"html";
        } else if ([numStr isEqualToString:@"239187"]) {
            type = @"aspx";
        } else if ([numStr isEqualToString:@"117115"]) {
            type = @"cs";
        } else if ([numStr isEqualToString:@"119105"]) {
            type = @"js";
        } else if ([numStr isEqualToString:@"102100"]) {
            type = @"txt";
        } else if ([numStr isEqualToString:@"255254"]) {
            type = @"sql";
        }
        return type;
    }
    
    

    感谢来源:https://blog.csdn.net/cos_sin_tan/article/details/50956293

    相关文章

      网友评论

          本文标题:OC 判断文件类型

          本文链接:https://www.haomeiwen.com/subject/qqrdtktx.html