我可以压缩多个pdf文件和个人文件夹。但是无法创建我在屏幕截图中标记的文件和文件夹的zip文件夹。这是我的代码。在If条件中,我编写了用于创建文件夹的zip文件的代码,而在其他情况下,我编写了文件的代码。如果我选择一个文件和文件夹,我需要在单个zip文件中创建文件夹和文件的zip文件。
if([[[[filePathsArray objectAtIndex:i]objectForKey:@"PdfName"] pathExtension]isEqualToString:@""]){
// For Folder - It creates a zip file in document directory .
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
BOOL isDir=NO;
NSArray *directoryContents = [[NSFileManager defaultManager] directoryContentsAtPath: docDirectory];
NSString *exportPath = [[mailArray objectAtIndex:i]objectForKey:@"PdfPath"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){
directoryContents = [fileManager subpathsAtPath:exportPath];
}
archivePath = [exportPath stringByAppendingString:[NSString stringWithFormat:@"%@.zip", [[filePathsArray objectAtIndex:i]objectForKey:@"PdfName"]]];
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
for(NSString *path in directoryContents)
{
NSString *longPath = [exportPath stringByAppendingPathComponent:path];
if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir)
{
NSString * extension = @"pdf";
if ([[longPath pathExtension]isEqualToString:@""]||[[[longPath pathExtension]lowercaseString]isEqualToString:extension]) {
[archiver addFileToZip:longPath newname:path];
}
}
}
BOOL successCompressing = [archiver CloseZipFile2];
if(successCompressing)
NSLog(@"Success");
else
NSLog(@"Fail");
NSString *path = archivePath;
[pdfData appendData:[NSMutableData dataWithContentsOfFile: path]];
}
else{
// For Single or Multiple pdf files - It creates a zip file in document directory .
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
isFolder = YES;
archivePath = [docDirectory stringByAppendingString:@"/pdfMarkup.zip"];
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
for (int i =0; i<[mailArray count]; i++)
{
[archiver addFileToZip:[[mailArray objectAtIndex:i]objectForKey:@"PdfPath"] newname:[[[mailArray objectAtIndex:i]objectForKey:@"PdfName"] stringByReplacingOccurrencesOfString:@"/" withString:@""]];
}
BOOL successCompressing = [archiver CloseZipFile2];
if(successCompressing)
{
NSLog(@"Zipp successfull");
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"message:@"Cannot zip Docs Folder"delegate:nilcancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
NSString *path = archivePath;
[pdfData appendData:[NSMutableData dataWithContentsOfFile: path]];
}