2016-11-02 4 views
1

iCloudに.zipファイルを保存します。誰でもアップロードと復元の方法を教えてください。私はアップルのiCloudバックアップのガイドラインを読んでいます。今では、いくつかのファイルと複数のイメージを持つ2つのフォルダで1つのフォルダを作成し、そのフォルダのzipファイルを生成しています。iCloudドライブに.zipファイルをバックアップして復元するにはどうすればよいですか?

+0

これまでのコードをご記入ください。 – Eeshwar

答えて

5
  1. まず、ローカルストレージにファイルとフォルダを保存します。
  2. 次に、このファイルを作成します。&フォルダのZipファイル。
  3. 最後に、zipファイルをiCloudにアップロードします。
  4. このファイルを取得するには、逆の処理を行います。
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self CreatFileAndFolder]; 

} 

ローカルにフォルダを作成し、iCloudのドライブにアップロードする場合は、このフォルダ内のファイルを保存します。

-(void)CreatFileAndFolder{ 

    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/meetInChat"]; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 


    NSString *stringToWrite = @"1\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n4"; 


    NSString *exportPath = [dataPath stringByAppendingString:@"/mytext.txt"]; 
    [stringToWrite writeToFile:exportPath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 

} 

まず、あなたのフォルダーを提出して、iCloudのドライブ上のZipファイルをアップロードしたZipを作成してアクションを作成します。

-(IBAction) iCloudSyncing:(id)sender 
{ 
    [self zipFolder]; 

    //--------------------------Zip Folder Upload on iCloud-----------------------------// 

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"meetInChat.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:@"meetInChat.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:@"meetInChat"]; 
     } 
     else 
      NSLog(@"PictureZip: Syncing FAILED with icloud"); 

    }]; 
} 

ここ

バックのiCloudドライブからZIPファイルをゲット

-(BOOL)zipFolder 
{ 

//--------------------------Create Zip Folder -----------------------------// 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docDirectory = [paths objectAtIndex:0]; 
    BOOL isDir=NO; 
    NSArray *subpaths = nil; 
    NSString *exportPath = [docDirectory stringByAppendingString:@"/meetInChat"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){ 
     subpaths = [fileManager subpathsAtPath:exportPath]; 
    } 

    NSString *meetInChatPath = [docDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.zip",@"meetInChat"]]; 

    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:@"meetInChat"]; 
    } 

    BOOL successCompressing = [archiver CloseZipFile2]; 
    if(successCompressing) 
     return YES; 
    else 
     return NO; 
} 
はその後、逆のプロセスがファイルを解凍し、取得行うフォルダからZipファイルを作成します。データを戻す。

- (IBAction)GetData:(id)sender { 

    //--------------------------Get data back from iCloud -----------------------------// 
    id token = [[NSFileManager defaultManager] ubiquityIdentityToken]; 
    if (token == nil) 
    { 
     NSLog(@"ICloud Is not LogIn"); 
    } 
    else 
    { 
     NSLog(@"ICloud Is LogIn"); 

     NSData *dataFile = [[NSUbiquitousKeyValueStore defaultStore]dataForKey:@"meetInChat"]; 

     NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
     NSString* fileName = [NSString stringWithFormat:@"meetInChat.zip"]; 
     NSString* fileAtPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 

     [dataFile writeToFile:fileAtPath atomically:NO]; 

    }  
} 
+0

ニースは自分の日を保存しました。他の人はMyDocumentがUIDocumentのサブクラスです –

関連する問題