0

I have a JSON object that contains some NSData. The NSData is a zip file that I have to unzip and store and utilize the files locally in my app.

I think I can use something like ZipArchive for the unzipping, but I'm not sure how to convert the NSData to a file that I can unzip.

Does anyone know how to handle this sort of issue?

tentmaking
  • 2,076
  • 4
  • 30
  • 53
  • You can refer to https://stackoverflow.com/questions/679104/the-easiest-way-to-write-nsdata-to-a-file – jokeman Feb 14 '18 at 21:41
  • FYI - there are zip libraries that will work with data without the need for a file on the filesystem. – rmaddy Feb 14 '18 at 22:08
  • Incidentally, you say you have this in JSON. JSON cannot hold binary objects. Or did you also base-64 encode it in order to represent it in JSON? If so, you might consider another approach because it's a shame to zip something to make it smaller, only to turn around and do a base-64 representation, which then makes it 33% bigger. – Rob Feb 15 '18 at 02:40
  • Also, if it's so big as to warrant the computational overhead of zipping, are you sure you are OK with the peak memory usage to hold this zip file in RAM? If it's so big as to justify zipping, then I would have thought that you do _not_ want to try to hold it in a `NSData`. You can, but RAM is one of the most limited resources on a device, and there are significant advantages to downloading this directly to disk (e.g. a download `URLSession` with a very modest "high-water mark") and then use a "disk"-based unzipping process. – Rob Feb 15 '18 at 02:46
  • Finally, are you looking for Swift or Objective-C answers? I'm assuming you're looking for Objective-C answers, given that you referenced `NSData`, rather than the Swift equivalent, `Data`, but I thought I'd ask. – Rob Feb 15 '18 at 02:48
  • @rmaddy Could you reference some of them? I have not been able to find any. – tentmaking Feb 15 '18 at 14:12
  • @Rob All good comments and questions. Thanks for your thoroughness. Rest assured we have done our homework and found that zipping is the right choice. Our impact on memory usage will be quite minimal. I am looking for Objective-C answers. – tentmaking Feb 15 '18 at 14:15
  • “how to convert the NSData to a file that I can unzip” ... OK, then [the answer that jokeman shared](https://stackoverflow.com/questions/679104/the-easiest-way-to-write-nsdata-to-a-file) shows you how to save the `NSData` to a file. Or [SSZipArchive](https://github.com/samsoffes/ssziparchive) is based upon ZipArchive and appears to include a method to save `NSData` as a file. – Rob Feb 15 '18 at 19:00

0 Answers0