开发者

How to find file types in objective c

开发者 https://www.devze.com 2023-02-15 17:06 出处:网络
I have done开发者_开发百科 a lot of research on this and haven\'t found anything useful. I want am making a simple program in objective c for personal use that opens files and gives information about

I have done开发者_开发百科 a lot of research on this and haven't found anything useful. I want am making a simple program in objective c for personal use that opens files and gives information about them. The only problem i have encountered is that i cannot find the file of a file i am opening. Without simply looking at the extension, is there a way to find the full file format of a file in objective c?

If possible, i would also like to be able to save that file in a different format. Information on this subject is also important for this application. Help will be greatly appreciated.


Mac OS X has type information attached to each file which specifies what the type of the file is supposed to be. This information is given by the application which last saved the file, so it is not necessarily correct. Also, new versions of OS X ignore this information and go entirely off of the file extension. However, the information is still stored and can be retrieved using NSFileManager's attributesOfItemAtPath:error: method.

As mentioned by quixoto above, OS X now maps extensions to UTIs. The UTI of a file can be retrieved using NSWorkspace, which can also tell you what the UTI means. This code will get the localized description of the file at /full/path/to/file:

NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSString *description = [ws localizedDescriptionForType:[ws typeOfFile:@"/full/path/to/file" error:nil]];

The typeOfFile:error: method requires an absolute path.


Consider: what do you mean by "full file format"? What kinds of files and level of detail do you care about?

You can't really get "format" information from the file system like you want. Believe it or not, the file system has no idea what format files are in.

File systems traditionally store files as a stream of bytes with a name, a size, and some other attributes ("hidden", permissions, etc). That's all that it's responsible for, and it's the application's problem to read those bytes and interpret them to mean something useful.

The extension is the traditional hint to an application about what a file contains, but as you might guess, it's certainly not a verified guarantee.

Modern Mac OS X has Quick Look, which uses a system-wide framework that adds some smarts on top of this, with mappings between extensions and UTIs, which are a richer notion of file type. Docs are here. I don't think there's a way to query this system for the mappings between extensions and UTIs, but I'm not sure.

Loading a file of one format and saving in another is 100% dependent on the file types you're talking about, and you're going to have ask very specific questions about specific formats if you really care to accomplish this. (And that topic extends well beyond a couple built-in method calls in Cocoa.)


This information is now (since OS X 10.5) handled through UTIs.

You can get it like this:

NSURL *desktopURL = [[[NSFileManager defaultManager] URLsForDirectory: NSDesktopDirectory
                                                       inDomains: NSUserDomainMask] lastObject];

NSURL *workingdirURL = [desktopURL URLByAppendingPathComponent:@"WorkingDir" isDirectory:YES];

NSArray *docURLs = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:workingdirURL 
                                                 includingPropertiesForKeys:@[NSURLTypeIdentifierKey] options:NSDirectoryEnumerationSkipsHiddenFiles 
                                                                      error: nil];

NSMutableArray *pdfURLs = [NSMutableArray array];

for (NSURL *docURL in docURLs) {
    id resourceValue;
    BOOL found = [docURL getResourceValue: &resourceValue
                                   forKey: NSURLTypeIdentifierKey
                                    error: nil];

    if ( found && [resourceValue isEqual:@"com.adobe.pdf"]) {
        [pdfURLs addObject: docURL];
    }
}

You can see the docs for more information about the file properties that can be retrieved this way. There is quite a lot of information available through this method.

0

精彩评论

暂无评论...
验证码 换一张
取 消