2017-08-16 339 views
0

我上傳一個密碼保護的zip文件作爲備份到iCloud,它被上傳,然後我下載,解壓並使用相同的文件,只有當我們從同一個設備上完成所有這些很酷的東西時,它才能工作得很好。奇怪:密碼保護的壓縮文件從iCloud更改它的格式一旦備份,無法解壓縮

考慮這種情況下, 1.壓縮並從一個設備上傳文件。 2.下載和其他設備將其解壓縮(不能解壓,因爲它不能識別該文件是zip格式)

注: 我有交叉驗證文件被成功地在其他設備通過使我的iTunes文件共享下載。

但我無法解壓文件。

+1

你是怎麼確定格式被改變的? – zneak

+0

「格式」是什麼意思?另外,SO是關於編碼不是一般的用戶問題。 – zaph

+0

@zaph這是一個代碼問題,我從代碼中壓縮一個文件,並在密碼保護後將其上傳到iCloud。 –

回答

0

我找到了解決辦法我自己希望這將節省別人的時間,密碼保護的zip文件轉換成數據,並上傳,下載,解壓縮相同

任何文件都可以上傳到任何大小的icloud的容器(是的,你應該在iCloud中擁有那麼多空間)讓我們來舉個例子SampleData.zip

// 1 This method will upload or sync SampleData.zip file in iCloud container, iCloud actually checks the metadata of your file before it uploads it into your iCloud container (so for first time it will upload the file and from next time it will only upload the changes) 

-(void) iCloudSyncing:(id)sender 
{ 
    //Doc dir 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"SampleData.zip"]; 
    NSURL *u = [[NSURL alloc] initFileURLWithPath:filePath]; 
    NSData *data = [[NSData alloc] initWithContentsOfURL:u]; 

    //Get iCloud container URL 
    NSURL *ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];// in place of nil you can add your container name 
    //Create Document dir in iCloud container and upload/sync SampleData.zip 
    NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"]URLByAppendingPathComponent:@"SampleData.zip"]; 
    Mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage]; 
    Mydoc.zipDataContent = data; 

    [Mydoc saveToURL:[Mydoc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) 
    { 
     if (success) 
     { 
      NSLog(@"SampleData.zip: Synced with icloud"); 
     } 
     else 
      NSLog(@"SampleData.zip: Syncing FAILED with icloud"); 

    }]; 
} 



    // 2 Download data from the iCloud Container 

- (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"); 

     NSError *error = nil; 
     NSURL *ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];// in place of nil you can add your container name 
     NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"]URLByAppendingPathComponent:@"SampleData.zip"]; 
     BOOL isFileDounloaded = [[NSFileManager defaultManager]startDownloadingUbiquitousItemAtURL:ubiquitousPackage error:&error]; 
     if (isFileDounloaded) { 
      NSLog(@"%d",isFileDounloaded); 
      NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
      //changing the file name as SampleData.zip is already present in doc directory which we have used for upload 
      NSString* fileName = [NSString stringWithFormat:@"RecSampleData.zip"]; 
      NSString* fileAtPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
      NSData *dataFile = [NSData dataWithContentsOfURL:ubiquitousPackage]; 
      BOOL fileStatus = [dataFile writeToFile:fileAtPath atomically:NO]; 
      if (fileStatus) { 
       NSLog(@"success"); 
      } 
     } 
     else{ 
      NSLog(@"%d",isFileDounloaded); 
     } 
    } 
} 

//3 voila its done :)