Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple Documents in a zip file in iOS?

Tags:

ios

zip

I want make a zip file containing multiple documents, documents are taken from my Document Directory.

BOOL isDir=NO;

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSArray *subpaths;
for(int i=0; i<[arrdocument count]; i++)
{
    NSString *toCompress = [arrdocument objectAtIndex:i];
    NSString *pathToCompress = [documentsDirectory stringByAppendingPathComponent:toCompress];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:pathToCompress isDirectory:&isDir] && isDir){
        subpaths = [fileManager subpathsAtPath:pathToCompress];
    } else if ([fileManager fileExistsAtPath:pathToCompress]) {
        subpaths = [NSArray arrayWithObject:pathToCompress];
    }

    NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"myZipFileName2.zip"];

    ZipArchive *za = [[ZipArchive alloc] init];
    [za CreateZipFile2:zipFilePath];

    if (isDir) {
        for(NSString *path in subpaths){
            NSString *fullPath = [pathToCompress stringByAppendingPathComponent:path];
            if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir){
                [za addFileToZip:fullPath newname:path];
            }
        }
    } else {
        [za addFileToZip:pathToCompress newname:toCompress];
    }
}

But when I look at the zip file it shows only one document inside the zip file?

like image 613
Shahzad Ali Avatar asked Jan 31 '26 11:01

Shahzad Ali


1 Answers

It seems like you recreate the zip file in each loop iteration. You should instead move the creation of the zip file out of the loop or just specify appending when you create the zip file like this:

[za CreateZipFile2:zipFilePath append:YES];
like image 126
Henrik Solgaard Avatar answered Feb 03 '26 06:02

Henrik Solgaard