0

I a macOS Cocoa application, I need to save a Bundle in my app proprietary format. For this reason, I decided to look at NSFileWrapper that appears to be the cleanest solution to deal with the problem. My code looks like this:

NSFileWrapper *bundleFileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];

NSDictionary *fileWrappers = [bundleFileWrapper fileWrappers];

if ([fileWrappers objectForKey:mboxFileName] == nil) {
    NSFileWrapper *textFileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:mboxData];
    [textFileWrapper setPreferredFilename:mboxFileName];
    [bundleFileWrapper addFileWrapper:textFileWrapper];
}
NSError *error;

BOOL success = [bundleFileWrapper writeToURL:[NSURL fileURLWithPath:path] options:NSFileWrapperWritingAtomic originalContentsURL:NULL error:&error];

NSLog(@"Error = %@",[error localizedDescription]);

My problem is that to I end up using very large NSData objects and this approach takes a lot of memory. Is there a way using NSFileWrapper to write small NSData objects in sequence ? Any help is greatly appreciated.

Alfonso Tesauro
  • 1,730
  • 13
  • 21
  • Honestly, if the data for a regular file wrapper is too big to fit in memory, then `NSFileWrapper` probably isn't the best choice. Consider just writing the file/package directly, or (maybe?) use wrappers for the structure of the package, but then write the embedded files directly. If that's not feasible, please explain why you need to use `NSFileWrapper`. – James Bucanek Dec 05 '18 at 17:53
  • Thanks, your comment answers my question. I think I will use NSFileWrapper for the structure as you suggest, then use fwrite for the big data. I use NSFileWrapper because I like that it provides an object oriented clean way to handle the creation of bundles. If you want, answer the question with your comment, I will accept certainly the answer. – Alfonso Tesauro Dec 05 '18 at 19:17

1 Answers1

1

Consider not using NSFileWrapper for your large data files. Use NSFileWrapper for the directory structure, but write big data files directly (open, write, ...).

Also, don't be shy about simply creating the directory structure yourself. A package can be as simple as, literally, a folder with an extension. And there are still tons of O-O APIs (NSURL, NSFileManager, ...) to help you examine and manipulate its content.

Good luck!

James Bucanek
  • 3,299
  • 3
  • 14
  • 30