how to zip and email a file using objective c?

I need to zip my log file and email it. I'm using Objective-zip for zipping my file. And this is my code

-(void)mailLogFile {  if ([MFMailComposeViewController canSendMail])
 { NSDictionary *user = [[DBHelper getSharedInstance] getCurrentUser];
 NSString *propertyQuery = [NSString stringWithFormat:@"SELECT RecentActivityId,PropertyPIC,PropertyId,Message,IsSynced,ActivityTime,ErrorCode,ErrorMessage,ErrorData,EntityId,TableNames From RecentActivity ORDER BY ActivityTime desc"];
 NSArray *resultArry = [[DBHelper getSharedInstance] getRecordsBySQL:propertyQuery];
  NSLog(@"This is the eNVDS in Recent Activity:%@" ,resultArry);
 [[DBHelper getSharedInstance] AddLogFile:[NSString stringWithFormat:@"This is the eNVDS in Recent Activity:%@" ,resultArry]]; 
NSPredicate *notsynced = [NSPredicate predicateWithFormat:@"IsSynced == 0 || IsSynced == %@",@"0"];
 NSArray *notsyncedenvds = [resultArry filteredArrayUsingPredicate:notsynced]; 
NSLog(@"This is the eNVDS in Recent Activity which is not synced:%@" ,notsyncedenvds);
 [[DBHelper getSharedInstance] AddLogFile:[NSString stringWithFormat:@"This is the eNVDS in Recent Activity which is not synced:%@" ,notsyncedenvds]]; 
 NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
 NSString *WritableDBPath= [documentsDirectory stringByAppendingPathComponent:@"Logfile.txt"]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.zip"]; 
NSData *zipdata = [NSData dataWithContentsOfFile:path]; 
NSData *data = [NSData dataWithContentsOfFile:WritableDBPath]; 
//OZZipFile *readFile = [[OZZipFile alloc] initWithFileName:path mode:OZZipFileModeCreate]; 
OZZipFile *zipFile32= [[OZZipFile alloc] initWithFileName:path mode:OZZipFileModeCreate]; 
OZZipWriteStream *stream= [zipFile32 writeFileInZipWithName:@"Logfile.txt" compressionLevel:OZZipCompressionLevelBest];  
[stream writeData:data]; [stream finishedWriting];   
  NSString *emailTitle = [NSString stringWithFormat:@"Log file of %@",[NSDate date]]; 
NSMutableString *messageBody = [NSMutableString stringWithFormat:@"Login with %@",user[@"Username"]]; 
if(![user[@"PropertyName"] isKindOfClass:[NSNull class]])[messageBody appendFormat:@"\n%@",user[@"PropertyName"]]; 
[messageBody appendFormat:@"\nApp version : %@",APP_VERSION]; 
[messageBody appendFormat:@"\nPlease describe your problem scenario here."]; 
[messageBody appendFormat:@"\n\n\nThanks"];  
NSArray *toRecipents = [NSArray arrayWithObject:@"support@aglive.com"];  
 MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
mc.mailComposeDelegate = self; 
[mc setSubject:emailTitle];  
[mc setMessageBody:messageBody isHTML:NO]; 
[mc setToRecipients:toRecipents]; 
  [mc addAttachmentData:zipdata mimeType:@"application/zip" fileName:@"test.zip"];  
 [self presentViewController:mc animated:YES completion:nil];  [zipFile32 close]; 
 }
else{
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Aglive Pro" message:@"You have not configured your mail account. Please configure your mail account from the device settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
 [alert show]; } }

right now I'm able to send a zip file in email but when I do unzip and open the file in my mobile or mac I get alert saying that The document “Logfile 4.txt” could not be opened. The file isn’t in the correct format. I don't know where I'm going wrong.

There’s two potential sources of problems here:

  • The code creating the zip file (A)

  • The code transferring the zip file as an attachment (B)

The first thing I’d do is to eliminate B. You can do this by calculating a checksum of the attachment at the source and then verify that checksum at the destination. If the checksums match, you know the file was transferred correctly and that you have a problem with how you created it.

Once you’ve confirmed that A is the problem, my recommendation is that you take this up with whatever support channel is provided for by the third-party library you’re using (OZZipFile).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Is there anything wrong in my code?

It is hard to tell if something is wrong with your code because 1) we cannot see the rest of the code that is being called and 2) we do not know your data specifications. So, you should try to debug your app based on the suggestions @eskimo gave, and come back to this forum (or whatever forum is appropriate) if you have a specific problem.

Hi . I am also using Objective_Zip Third Party Library to Zip my file in swift . When I try to Zip it with password Protected and then send it via Email or some other source and tried opening it in my mac , It fails to Open .

It Prompts for Password Again and Again (Even the Correct Password is given).

I have attached the file Encrypted - "https://bit.ly/3w3DTN7"

But I tried opeing the same file using "B1 Free Archiver" , There it opens and unzips successfully . The Problem is why i cant able to open it in "Archive Utility " of my mac . My Mac Os Version - 11.2.3 (BigSur)

I am also using Objective_Zip Third Party Library to Zip my file

Again, I recommend that you pursue via the support channel for the third-party library you’re using.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

how to zip and email a file using objective c?
 
 
Q