iOS Judging file types2013-11-06 13:14 929 People read comments (0) favorite reports
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 = [[nsbundlemainbundle] pathforresource:@ "READ"ofType:@ " Zip "];
nsdata *d = [nsdatadatawithcontentsoffile:p ath];
if (data. length<2) {
return @ "not FILE";
}
int char1 = 0, char2 =0; // must be initialized like this
[Data getbytes:&char1 range:nsmakerange (0, 1)];
[Data getbytes:&char2 range:nsmakerange (1, 1)];
NSLog (@ "%d%d", char1,char2);
nsstring *numstr = [nsstringstringWithFormat:@ "%i%i", char1,char2];
This way you can see that the output is: 8075.
It's just a zip with the numbers above.
Reference: http://hi.baidu.com/personname/item/35e96d3004d51ac51a969618
http://blog.csdn.net/my_yang/article/details/8069745
iOS Judging file types