1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| - (void)getFileExtension { NSString *path = [[NSBundle mainBundle] pathForResource:@"2019工资单 (1)" ofType:@"docx"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSString *extension = [self contentTypeForImageData:data]; NSLog(@"%@", extension); }
- (NSString *)contentTypeForImageData:(NSData *)data { const NSInteger maxLength = 28; Byte headBytes[maxLength]; [data getBytes:headBytes length:maxLength]; NSString *headStr = [self hexStringFromByte:headBytes]; NSDictionary *dict = [self dict]; for (NSString *key in dict) { if ([headStr hasPrefix:[key uppercaseString]]) { return dict[key]; } } return @""; }
- (NSString *)hexStringFromByte:(Byte *)byta { NSString *hexStr = @""; for(int i = 0; i < 28; i++) { NSString *newHexStr = [NSString stringWithFormat:@"%x",byta[i]&0xff]; if([newHexStr length]==1) hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr]; else hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; } return [hexStr uppercaseString]; }
- (NSDictionary *)dict { return @{ @"FFD8FF" : @"JPEG" , @"89504E47" : @"PNG", @"47494638" : @"GIF", @"49492A00" : @"TIFF", @"424D" : @"BMP", @"41433130" : @"DWG", @"38425053" : @"PSD", @"7B5C727466" : @"RTF", @"3C3F786D6C" : @"XML", @"68746D6C3E" : @"HTML", @"48544D4C207B0D0A0942" : @"CSS", @"696B2E71623D696B2E71" : @"JS", @"44656C69766572792D646174653A" : @"EML", @"CFAD12FEC5FD746F" : @"DBX", @"2142444E" : @"PST", @"D0CF11E0" : @"XLS_DOC", @"504B0304" : @"XLSX_DOCX", @"255044462D" : @"PDF", @"5374616E64617264204A" : @"MDB", @"d0cf11e0a1b11ae10000" : @"WPS", @"504B0304" : @"ZIP", @"52617221" : @"RAR", @"4D5A9000030000000400" : @"EXE", @"57415645" : @"WAV", @"41564920" : @"AVI", @"000001BA" : @"MPG", @"6D6F6F76" : @"MOV", @"00000020667479706d70" : @"MP4", @"49443303000000002176" : @"MP3", @"464C5601050000000900" : @"FLV" }; }
|