iOSDeV iOSDeV - 1 month ago 15
iOS Question

How can I backup and restore .zip file on iCloud Drive?

I want to store .zip files on iCloud. Anybody help me how to upload and restore? I have read the Apple guideline for iCloud backup and now I have created one folder with some files and two folders with multiple images and then generate zip file of that folder.

Answer
  1. First Save your File and Folder on Local Storage.
  2. Then Create this File & Folder's Zip File.
  3. And Last Upload Your zip file on iCloud.
  4. For get this file doing reverse process.

     -(IBAction) iCloudSyncing:(id)sender
                {
                    [self zipFolder];
    
                    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                    NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"abc.zip"];
                    NSLog(@"FilePath=>%@",zipFilePath);
                    NSURL *u = [[NSURL alloc] initFileURLWithPath:zipFilePath];
                    NSData *data = [[NSData alloc] initWithContentsOfURL:u];
    
                    NSURL *ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];
                    NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"]URLByAppendingPathComponent:@"abc.zip"];
                    Mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage];
                    Mydoc.zipDataContent = data;
    
                    [Mydoc saveToURL:[Mydoc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success)
                     {
                         if (success)
                         {
                             NSLog(@"PictureZip: Synced with icloud");
    
                             [[NSUbiquitousKeyValueStore defaultStore]setData:data forKey:@"abc"];
                         }
                         else
                             NSLog(@"PictureZip: Syncing FAILED with icloud");
    
                     }];
                }
                -(BOOL)zipFolder
                {
    
                    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                    NSString *docDirectory = [paths objectAtIndex:0];
                    BOOL isDir=NO;
                    NSArray *subpaths = nil;
                    NSString *exportPath = [docDirectory stringByAppendingString:@"/abc"];
    
                    NSFileManager *fileManager = [NSFileManager defaultManager];
                    if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){
                        subpaths = [fileManager subpathsAtPath:exportPath];
                    }
    
                    NSString *meetInChatPath = [docDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.zip",@"abc"]];
    
                    ZipArchive *archiver = [[ZipArchive alloc] init];
                    [archiver CreateZipFile2:meetInChatPath];
    
    
                    if (isDir) {
                        for(NSString *path in subpaths){
                            NSString *fullPath = [exportPath stringByAppendingPathComponent:path];
                            if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir){
                                [archiver addFileToZip:fullPath newname:path];
                            }
                        }
                    } else {
                        [archiver addFileToZip:exportPath newname:@"abc"];
                    }
    
                    BOOL successCompressing = [archiver CloseZipFile2];
                    if(successCompressing)
                        return YES;
                    else
                        return NO;
                }
    
    
                - (IBAction)GetData:(id)sender {
    
                    id token = [[NSFileManager defaultManager] ubiquityIdentityToken];
                    if (token == nil)
                    {
                        NSLog(@"ICloud Is not LogIn");
                    }
                    else
                    {
                        NSLog(@"ICloud Is LogIn");
    
                        NSData *dataFile = [[NSUbiquitousKeyValueStore defaultStore]dataForKey:@"abc"];
    
                        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                        NSString* fileName = [NSString stringWithFormat:@"abc.zip"];
                        NSString* fileAtPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
                        [dataFile writeToFile:fileAtPath atomically:NO];
    
                    }
              }