Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip folders in iPhone SDK?

In my Application,I am taking screenshots of image View and then I am saving those screen shots in document folder of the application.Now I want to Email all those images with the same folder structure they are in.Zipping all the folders containing the images and then attaching the zip file to the mail will solve the problem but how can I zip these folders and then attach them to the mail?

Any help is appreciated!

like image 607
Yogi Avatar asked Nov 16 '11 10:11

Yogi


People also ask

Can you zip a folder on iPhone?

Open the Files app, then choose a location like On My iPhone or iCloud Drive. Tap Select, then choose one or more files. Tap More, then tap Compress. If you selected one file, a ZIP file with the same filename saves to that folder.

How do I zip multiple folders at once?

To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.

How do I zip an entire folder?

To zip (compress) a file or folder Locate the file or folder that you want to zip. Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.


2 Answers

I have used this code to create a zip file of the documents directory of my app and it worked

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
BOOL isDir=NO;
NSArray *subpaths;
NSString *exportPath = docDirectory;
NSFileManager *fileManager = [NSFileManager defaultManager];    
if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){
    subpaths = [fileManager subpathsAtPath:exportPath];
}

NSString *archivePath = [docDirectory stringByAppendingString:@"/test.zip"];

ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
for(NSString *path in subpaths)
{
    NSString *longPath = [exportPath stringByAppendingPathComponent:path];
    if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir)
    {
        [archiver addFileToZip:longPath newname:path];      
    }
}

if([archiver CloseZipFile2])
    NSLog(@"Success");
else
    NSLog(@"Fail");
like image 95
Robin Avatar answered Sep 17 '22 15:09

Robin


ZipArchive is an Objective-C class to compress or uncompress zip files, which is base on open source code "MiniZip".

It can be used for iPhone application development, and cocoa on Mac OSX as well.

see this : http://code.google.com/p/ziparchive/downloads/list

like image 37
Ankur Avatar answered Sep 20 '22 15:09

Ankur