質問

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.

役に立ちましたか?

解決

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.

他のヒント

// 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");
    }
}

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.

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];
@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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top