ZipArchive memory issues on iPhone for large archive
I am trying to compress multiple files into one ZIP archive and I am running low memory warning. Since the full zip file is loaded into memory, I think that is the problem. Is there a way that I can better manage the compression / decompression with ZipArchive so that not all of the data is in memory at once?
Thanks!
a source to share
After doing some research on ZipArchive alternatives, I found another project called Objective-zip that seems to be slightly better than ZipArchive. Here's a link:
http://code.google.com/p/objective-zip/
The API is pretty simple. One thing I ran into was that in begging, I was reading data and never releasing it, so if you add large files to the zip file remember to release the data. Here's the little code I used:
ZipFile * zipFile = [[ZipFile alloc] initWithFileName: archivePath mode: ZipFileModeCreate];
for (NSString * path in subpaths) {
NSData * data = [[NSData alloc] initWithContentsOfFile: longPath];
ZipWriteStream * stream = [zipFile writeFileInZipWithName: path compressionLevel: ZipCompressionLevelNone];
[stream writeData: data];
[stream finishedWriting];
[data release];
}
[zipFile close];
[zipFile release];
I hope this is helpful for anyone else who is facing the same problem.
a source to share
An easier way to deal with this is to simply change the ZipArchive method to read the file into NSData. Just change the following code
data = [ NSData dataWithContentsOfFile:file ];
to
data = [ NSData dataWithContentsOfMappedFile:file ];
This will force the OS to read the file as a card. Basically it just uses less memory as it reads from the file as it needs to rather than load it into memory right away.
a source to share