2

I have a folder in the documents directory that needs to be zipped.I was able to zip regular files but i was not able to zip folders.

I referred the following link How to zip folders in iPhone SDK? But here the files in the folder are zipped separately.I would like to zip the entire folder instead of having to deal with exploring the contents(files/folders) inside the directory and zipping them one by one.

Is it possible to do this with the ZipArchive library. If it is could some one please explain by posting the necessary code?

Thank You.

Community
  • 1
  • 1
Mr.Anonymous
  • 820
  • 2
  • 13
  • 22

5 Answers5

4

You can't do that with ZipArchive but have a look at SSZipArchive it has a + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath; method you could use.

Cory Powers
  • 1,140
  • 8
  • 14
  • Hey im able to zip the folder using this library but when the folder is unzipped only the contents(files/subfolers) within the folder are brought back.. The folder that is zipped does not come back.. For eg if b c d are files within the folder A and i zip folder A then the folder A is not brought back after unzipping.. Does that make sense? – Mr.Anonymous May 08 '13 at 17:45
  • @Mr.Anonymous There is a pull request to enhance the folder zip functionality at the repo, https://github.com/soffes/ssziparchive/pull/49 – Cory Powers May 08 '13 at 17:50
2
// Path to store Zip    

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

NSString* zipfile = [dPath stringByAppendingPathComponent:@"test.zip"] ;

// File Tobe Added in Zip

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GetAllCardList" ofType:@"xml"];

NSString *fileName = @"MyFile"; // Your New ZipFile Name

ZipArchive* zip = [[ZipArchive alloc] init];
if([zip CreateZipFile2:zipfile])
{
    NSLog(@"Zip File Created");
    if([zip addFileToZip:filePath newname:[NSString stringWithFormat:@"%@.%@",fileName,[[filePath lastPathComponent] pathExtension]]])
    {
        NSLog(@"File Added to zip");
    }
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • 1
    this doesn't answer the question. he wants to zip a folder, not individual files – ngb Dec 04 '13 at 04:48
2

try SSZipArchive

+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirector;

I used

let success = SSZipArchive.createZipFileAtPath(zipFullFilename, withContentsOfDirectory:dataDir, keepParentDirectory:true)

to create zip folder. It is worked.

Wu Baiquan
  • 129
  • 7
0

You just need to get the contents of the dir prior to adding all the files to the zip as in the example. Adding them one by one is the same thing in code so just get a list then run through the list.

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];

If you are looking for specific files you can also use a predicate to filter the results

NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *pngs = [dirContents filteredArrayUsingPredicate:filter];
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
0
@implementation ZipArchive(Util)
+ (BOOL)makeZipFile:(NSString*)filepath withContentInDirectory:(NSString*)directory
{
    ZipArchive* zip = [[ZipArchive alloc] initWithFileManager:[NSFileManager defaultManager]];
    if ([zip CreateZipFile2:filepath]){
        [self enumDirectory:directory zipFile:zip];
        return [zip CloseZipFile2];
    }
    return NO;
}


+ (void)enumDirectory:(NSString*)directory zipFile:(ZipArchive*)zip
{
    NSArray* resourceKeys = @[NSURLIsDirectoryKey];
    NSDirectoryEnumerator<NSURL *> * enumerator = [[NSFileManager defaultManager] enumeratorAtURL:[NSURL fileURLWithPath:directory] includingPropertiesForKeys:resourceKeys options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:^BOOL(NSURL * _Nonnull url, NSError * _Nonnull error) {
        return NO;
    }];
    
    for (NSURL* url in enumerator) {
        
        NSDictionary<NSString *, id> *resourceValues = [url resourceValuesForKeys:resourceKeys error:nil];
        BOOL isDirectory = [[resourceValues objectForKey:NSURLIsDirectoryKey] boolValue];
        if (isDirectory) {
            continue;
        }
                
        NSInteger len = [directory length];
        NSString* subpath = [url.path substringFromIndex:len];
        if ([subpath rangeOfString:@"/"].location == 0) {
            subpath = [subpath substringFromIndex:1];
        }

        [zip addFileToZip:url.path newname:subpath];
    }
}

@end