我如何使用iCloud在我的应用程序之间同步.zip文件?

是否可以将.zipfile upload到iCloud ,然后在所有用户的iOS设备上进行同步?
如果是这样,我该怎么做呢?
如果有任何文件大小限制,那么也提到最大。 文件大小允许。

这就是我使用iCloud同步zip文件的方法。

脚步:

1) http://transoceanic.blogspot.in/2011/07/compressuncompress-files-on.html 。 请参考这个链接下载zip压缩包,其中有压缩和解压缩文件夹的代码。

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

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

由于UIDocument链接到文件URL,因此可以轻松创build指向file://myzipfile.zip ,然后将zip文档上载到iCloud

我希望这有帮助