samlaintx / ziparchive

Automatically exported from code.google.com/p/ziparchive
0 stars 0 forks source link

ZIP folder addition #16

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I needed to zip an entire folder tree...

// 2010-12-22, Michael Burford (michael@burford.net)
// "pathPrefix" should be nil or @"" the first call; lets the recursive calls 
store in subfolders in the zip file.
-(NSInteger) addFolderToZip:(NSString*)path pathPrefix:(NSString*)prefix {
    NSInteger   fileCount = 0;
    NSArray     *dirArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];

    for (int i=0; i<[dirArray count]; i++) {
        NSString        *dirItem = [dirArray objectAtIndex:i];
        NSDictionary    *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:[path stringByAppendingPathComponent:dirItem] error:nil];

        if ([[dict fileType] isEqualToString:NSFileTypeDirectory] || [[dict fileType] isEqualToString:NSFileTypeSymbolicLink]) { 
            //Recursively do subfolders.
            fileCount += [self addFolderToZip:[path stringByAppendingPathComponent:dirItem] pathPrefix:([prefix length]>0 ? [prefix stringByAppendingPathComponent:dirItem] : dirItem)];
        } else {
            //Count if added OK.
            if ([self addFileToZip:[path stringByAppendingPathComponent:dirItem] newname:([prefix length]>0 ? [prefix stringByAppendingPathComponent:dirItem] : dirItem)]) {
                fileCount++;
            }
        }
    }
    return fileCount;
}

Original issue reported on code.google.com by mjburf...@gmail.com on 22 Dec 2010 at 8:46

GoogleCodeExporter commented 8 years ago
Thank you sooooooooooooo much !!

Original comment by charley...@gmail.com on 19 Aug 2011 at 2:18

GoogleCodeExporter commented 8 years ago
Thanks!

Original comment by TonyBr...@gmail.com on 2 Aug 2012 at 6:48

GoogleCodeExporter commented 8 years ago
excellent code which solves my problems.

hats off to the author

I wish someone could add it to the zipArchive class 

Original comment by kenjo...@gmail.com on 14 Jan 2015 at 3:45