Recently doing something that has download zip, just the server sent over is binary, need to according to binary to determine what type of file, thereby saving operations. At first it was not understood, and it was later discovered that the first 2 bits of the ASCII code could be judged by binary binary. As follows:
255216 jpg;
7173 gif;
6677 BMP,
13780 PNG;
6787 SWF
7790 exe DLL,
8297 rar
8075 zip
55122 7z
6063 XML
6033 html
239187 aspx
117115 CS
119105 JS
102100 txt
255254 SQL
In iOS, use the OC code to do the following:
NSString *path = [[NSBundle mainbundle] Pathforresource:@"Test"OfType:@"Zip"]; NSData*data =[NSData Datawithcontentsoffile:path];if(data.length<2) { return ; }intCHAR1 =0, char2 =0;//This must be initialized[Data getBytes:&char1 Range:nsmakerange (0,1)]; [Data getBytes:&char2 Range:nsmakerange (1,1)]; NSLog (@"%d%d", CHAR1,CHAR2); NSString*NUMSTR = [NSString stringWithFormat:@"%i%i", CHAR1,CHAR2]; NSLog (@"%@", NUMSTR);
This way you can see that the output is: 8075.
It's just a zip with the numbers above.
iOS file type judgment