-(void) zipDocument{
    
    BOOL isDir=YES;
    
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSArray *subpaths;
    
    NSString *toCompress = @"";//dirToZip_OR_fileNameToZip";
    NSString *pathToCompress = [documentsDirectory stringByAppendingPathComponent:toCompress];
    NSLog(@"dir: %@",pathToCompress);
    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 *phonenumber = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"phonenumber"];
   // if(phonenumber==nil || phonenumber.length<=0) return;
    
    FileZipName=[self GetDateToString];
    FileZipName=[NSString stringWithFormat:@"%@_%@.zip",phonenumber,FileZipName];
    
    
    NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:FileZipName];
    
    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];
    }
    
    
    //BOOL successCompressing =
    [za CloseZipFile2];
    
}

By admin-powenko

Dr. Powen Ko is a teacher and CEO on LoopTek LLC, and like to teaching. if you need to class, please let PowenKo know, he will love to service and sharing. LoopTek web site is www.looptek.com

Leave a Reply