0

我正在尝试从 NSdcommentDirectory 中另一个目录的内容创建一个 zip 文件。我为此使用“SSZipArchive”。但是当我尝试获取 zip 文件时它不可用。这是我正在尝试的代码。

`

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/SourceFolder"];
NSString *stringPath2 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/ZipFolder"];

//SourceFolder Creation
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];


//ZipFolder Creation
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath2 withIntermediateDirectories:NO attributes:nil error:&error];


//Adding Files To SourceFolder
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"flower.jpg"], 1.0);
NSData *data2 = UIImageJPEGRepresentation([UIImage imageNamed:@"Wall.jpg"], 1.0);

[data writeToFile:[stringPath stringByAppendingFormat:@"/image1.jpg"] atomically:YES];
[data2 writeToFile:[stringPath stringByAppendingFormat:@"/image2.jpg"] atomically:YES];



//Retriving Path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *sampleDataPath = [documentsDirectory stringByAppendingPathComponent:@"/SourceFolder"];
NSString *sampleDataPath2 = [documentsDirectory stringByAppendingPathComponent:@"/ZipFolder"];


//To Create ZipFile under /ZipFolder from /SourceFolder
[SSZipArchive createZipFileAtPath: sampleDataPath2 withContentsOfDirectory: sampleDataPath];

//Checking If File exist in source and zip folder
NSFileManager *sharedFileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *files = [sharedFileManager enumeratorAtPath:sampleDataPath]; // The content shows two images which I have added ->image1.jpg,image2.jpg
NSDirectoryEnumerator *files2 = [sharedFileManager enumeratorAtPath:sampleDataPath2]; // However I dont see the .zip file inside it

`

我有以下问题:

  1. 如何将源目录内容作为 zip 文件获取。
  2. 如何获取整个 zip 文件,以便我可以将其作为 zip 发送到服务器。

请帮助我,我正在尝试很长时间。

4

0 回答 0