【问题标题】:How can I backup and restore .zip file on iCloud Drive?如何备份和恢复 iCloud Drive 上的 .zip 文件?
【发布时间】:2016-11-02 11:02:56
【问题描述】:

我想在 iCloud 上存储 .zip 文件。有人帮我如何上传和恢复吗?我已经阅读了 Apple 的 iCloud 备份指南,现在我创建了一个包含一些文件的文件夹和两个包含多个图像的文件夹,然后生成该文件夹的 zip 文件。

【问题讨论】:

  • 到目前为止请分享您的代码。

标签: ios objective-c iphone icloud


【解决方案1】:
  1. 首先将文件和文件夹保存在本地存储中。
  2. 然后创建此文件和文件夹的 Zip 文件。
  3. 最后在 iCloud 上上传您的 zip 文件。
  4. 为了得到这个文件做反向过程。
- (void)viewDidLoad {
    [super viewDidLoad];

    [self CreatFileAndFolder];

}

在本地创建文件夹并将文件保存在您要上传到 iCloud Drive 的此文件夹中。

-(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];

}

创建操作,首先创建您的文件夹的 Zip 文件,然后将您的 Zip 文件上传到 iCloud Drive。

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

     }];
}

从您的文件夹创建 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;
}

在此处从 iCloud Drive 取回 ZipFile,然后进行反向处理解压缩文件并取回数据。

- (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];

    }      
}

【讨论】:

  • 很高兴为其他人节省了一天,MyDocument 是 UIDocument 的子类
猜你喜欢
  • 1970-01-01
  • 2022-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多