【问题标题】:How can I use iCloud to synchronize a .zip file between my apps?如何使用 iCloud 在我的应用程序之间同步 .zip 文件?
【发布时间】:2011-12-24 05:40:15
【问题描述】:

是否可以将 .zip 文件上传到 iCloud,然后在用户的所有 iOS 设备上同步?
如果是这样,我该怎么做?
如果有任何文件大小限制,则还要提及最大。允许的文件大小。

【问题讨论】:

    标签: iphone ios zip ios5 icloud


    【解决方案1】:

    这就是我与 iCloud 同步 zip 文件的方式。

    步骤:

    1) http://transoceanic.blogspot.in/2011/07/compressuncompress-files-on.html .请参阅此链接以下载具有压缩和解压缩文件夹代码的 zip api。

    2) 现在您只需使用 NSData。

    3) “MyDocument.h”文件

    #import <UIKit/UIKit.h>
    
    @interface MyDocument : UIDocument
        @property (strong) NSData *zipDataContent;
    @end
    

    4)

    #import "MyDocument.h"
    
    @implementation MyDocument
    @synthesize zipDataContent;
    
    // Called whenever the application reads data from the file system
    - (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError
    {
        self.zipDataContent = [[NSData alloc] initWithBytes:[contents bytes] length:[contents length]];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"noteModified" object:self];
        return YES;     
    }
    
    // Called whenever the application (auto)saves the content of a note
    - (id)contentsForType:(NSString *)typeName error:(NSError **)outError 
    {
        return self.zipDataContent;
    }
    
    @end
    

    5) 现在,您需要在应用程序的某个位置压缩文件夹并与 icloud 同步。

    -(BOOL)zipFolder:(NSString *)toCompress zipFilePath:(NSString *)zipFilePath
    {
        BOOL isDir=NO;
    
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *pathToCompress = [documentsDirectory stringByAppendingPathComponent:toCompress];
    
        NSArray *subpaths;
        NSFileManager *fileManager = [NSFileManager defaultManager]; 
        if ([fileManager fileExistsAtPath:pathToCompress isDirectory:&isDir] && isDir){
            subpaths = [fileManager subpathsAtPath:pathToCompress];
        } else if ([fileManager fileExistsAtPath:pathToCompress]) {
            subpaths = [NSArray arrayWithObject:pathToCompress];
        }
    
        zipFilePath = [documentsDirectory stringByAppendingPathComponent:zipFilePath];
        //NSLog(@"%@",zipFilePath);
        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];
        if(successCompressing)
            return YES;
        else
            return NO;
    }
    -(IBAction) iCloudSyncing:(id)sender  
    {
        //***** PARSE ZIP FILE : Pictures *****
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
        if([self zipFolder:@"Pictures" zipFilePath:@"iCloudPictures"])
            NSLog(@"Picture Folder is zipped");
    
        ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];
        ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"]  URLByAppendingPathComponent:@"iCloudPictures.zip"];
    
        mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage];
        NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"iCloudPictures"];
        NSURL *u = [[NSURL alloc] initFileURLWithPath:zipFilePath];
        NSData *data = [[NSData alloc] initWithContentsOfURL:u];
        // NSLog(@"%@ %@",zipFilePath,data);
        mydoc.zipDataContent = data;
    
        [mydoc saveToURL:[mydoc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) 
        {
            if (success) 
            {
                NSLog(@"PictureZip: Synced with icloud");
            }
            else
                NSLog(@"PictureZip: Syncing FAILED with icloud");
    
        }];
    }
    

    6) 您可以像这样解压缩从 iCloud 接收的数据。

    - (void)loadData:(NSMetadataQuery *)queryData {
    
    
    
        for (NSMetadataItem *item in [queryData results]) 
        {    
            NSString *filename = [item valueForAttribute:NSMetadataItemDisplayNameKey];
    
    
            NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
            MyDocument *doc = [[MyDocument alloc] initWithFileURL:url];
    
    if([filename isEqualToString:@"iCloudPictures"])
            {
                [doc openWithCompletionHandler:^(BOOL success) {
                    if (success) {
                        NSLog(@"Pictures : Success to open from iCloud");
                        NSData *file = [NSData dataWithContentsOfURL:url];
    
                        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                        NSString *zipFolder = [documentsDirectory stringByAppendingPathComponent:@"Pics.zip"];
                        [[NSFileManager defaultManager] createFileAtPath:zipFolder contents:file attributes:nil];
                        //NSLog(@"zipFilePath : %@",zipFolder);
    
                        NSString *outputFolder = [documentsDirectory stringByAppendingPathComponent:@"Pictures"];//iCloudPics
                        ZipArchive* za = [[ZipArchive alloc] init];
                        if( [za UnzipOpenFile: zipFolder] ) {
                            if( [za UnzipFileTo:outputFolder overWrite:YES] != NO ) {
                                NSLog(@"Pics : unzip successfully");
                              }
                            [za UnzipCloseFile];
                        }
                        [za release];
    
    
                        NSError *err;
                        NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:outputFolder error:&err];
                        if (files == nil) {
                            NSLog(@"EMPTY Folder: %@",outputFolder);
                        }
                        // Add all sbzs to a list    
                        for (NSString *file in files) {
                            //if ([file.pathExtension compare:@".jpeg" options:NSCaseInsensitiveSearch] == NSOrderedSame) {        
                            NSLog(@" Pictures %@",file);
    
                            //                            NSFileManager *fm = [NSFileManager defaultManager];
                            //                            NSDictionary *attributes = [fm fileAttributesAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectory,file]                                                                 traverseLink:NO];
                            //                            
                            //                            NSNumber* fileSize = [attributes objectForKey:NSFileSize];
                            //                            int e = [fileSize intValue]; //Size in bytes
                            //                            NSLog(@"%@__%d",file,e);           
                        }
    
                    }
                    else 
                    {
                        NSLog(@"Pictures : failed to open from iCloud");
                        [self hideProcessingView];
                    }
                }]; 
            }
    }
    }
    

    【讨论】:

    • 我想问你假设我创建了一个 zip 文件夹并将其与 iCloud 同步,当数据来自 icloud 时,该文件夹将被解压缩到相同的位置,或者我需要手动添加包含的每个文件在那个文件夹中到我的文档文件夹中。
    • 解压后可以保留同一个文件夹。我也是这样做的。这里 NSString *outputFolder = [documentsDirectory stringByAppendingPathComponent:@"Pictures"];在这一行中,“图片”是我的文档目录文件夹。您可以在压缩时在 if([self zipFolder:@"Pictures" zipFilePath:@"iCloudPictures"]) 看到“图片”。
    • 感谢您的回答。我在理解代码方面帮助了我很多,它对我来说就像一个魅力。我希望我能投票这么多次。再次感谢。
    • 我的荣幸。我需要找到一种方法来同步具有文本、图片和音频的 sqlite 数据库。如此绝望地,我找到了这种方法。谢谢。 :) :)
    • 我也在此发布了一个问题。stackoverflow.com/questions/10408695/… 是问题的链接。请将答案作为此页面的链接给出,以便我也可以接受并在那里投票。
    【解决方案2】:

    为了在 iCloud 中启用文档存储,您的“文档”需要封装在 UIDocument 对象中。

    由于UIDocument 链接到文件URL,您可以轻松地创建一个指向file://myzipfile.zipUIDocument,然后将压缩文件上传到iCloud

    希望对你有帮助

    【讨论】:

    • 您好 iGranDev,我看过这个文档,但没有从这个文档中得到太多帮助,请给我一些代码以了解更多信息。感谢您的关注。
    • 其实,在 iCloud 上共享文档并不需要 UIDocument。 NSFileManager 将让您手动上传文档,而无需接触 UIDocument。我写了一篇关于这个here的演练。
    • 确实!非常感谢这个演练。我刚刚发了推文。
    【解决方案3】:

    或许本教程可以为您提供更多帮助:

    http://www.raywenderlich.com/6015/beginning-icloud-in-ios-5-tutorial-part-1

    【讨论】:

      猜你喜欢
      • 2013-07-09
      • 2011-12-12
      • 2012-04-07
      • 2012-07-25
      • 2011-12-14
      • 2014-07-14
      • 2015-04-16
      • 2014-01-24
      • 1970-01-01
      相关资源
      最近更新 更多