Maneesh Sharma Maneesh Sharma - 4 months ago 21
iOS Question

How to zip a text file and post it using NSURLConnection in objective-c

I have a text file in which I write all the NSLOG's and I post it to the server whenever required. Since I want to optimise the size of the file that is sent to the server , I want to zip the text file . I referred a lot of examples but could not clearly understand . This is my code to create the file

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"Logger.txt"];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSUTF8StringEncoding],"a+",stderr);


and this is the code to post the file to the server.

NSString *finalURL = [self getSupportServletURL:persist];
NSURL *url = [NSURL URLWithString:finalURL];
NSMutableURLRequest * requests = [[NSMutableURLRequest alloc]initWithURL:url];
[requests setHTTPMethod:@"POST"];
[requests setValue:@"text" forHTTPHeaderField:@"Content-type"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Logger.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
LogInfo(@"Data being posted to server %@",finalURL);
LogTrace(@"Post Data : Data being posted to server \n%@",content);
[requests setTimeoutInterval:60.0];
[requests setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]];
[requests setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[content length]] forHTTPHeaderField:@"Content-Length"];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:requests delegate:self];


and this works perfectly. Can anyone help me in converting this to a zip file ( including the things that need to be included in .m file & .h file ) and posting it to the server . Thank You in advance.

Answer

Create logger file following way. I just created file in different directory instead of document directory so we can zip that directory.

//creating logger file in document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//logger file name
NSString *fileName =[NSString stringWithFormat:@"Logger.txt"];

//we will add log file in "LogFiles" directory so we can zip that directory
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/LogFiles/%@",fileName]];


//"LogFiles" directory path
NSString *LogFilesDirectory = [documentsDirectory stringByAppendingPathComponent:@"/LogFiles"];
NSError *error;

 //create "LogFiles" directory if not created
if (![[NSFileManager defaultManager] fileExistsAtPath:LogFilesDirectory])
    [[NSFileManager defaultManager] createDirectoryAtPath:LogFilesDirectory withIntermediateDirectories:NO attributes:nil error:&error];

//writing logger file
freopen([logFilePath cStringUsingEncoding:NSUTF8StringEncoding],"a+",stderr);

Now #import "SSZipArchive.h" in viewController.m

To create zip file use following code

//getting logger.txt file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/LogFiles"]];

NSString* zipfile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/LogFiles/Logger.Zip"]];
//create zip file, return true on success
BOOL isZipCreated=[SSZipArchive createZipFileAtPath:zipfile withContentsOfDirectory:logFilePath];

    if (isZipCreated) {

    NSLog(@"Zip file Created at Path : %@",zipfile);
    NSData *zipData = [NSData dataWithContentsOfFile:zipFile]; // note, autorelease object

    NSString *postLength = [NSString stringWithFormat:@"%d", [zipData length]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/zip" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:zipData];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:requests delegate:self];

}

else
{
    NSLog(@"Zip create error");
}
Comments