我有一個文本文件,我在其中編寫所有NSLOG,並在需要時將其發佈到服務器。由於我想優化發送到服務器的文件的大小,我想壓縮文本文件。我提到了很多例子,但不能清楚地理解。這是我創建文件的代碼如何壓縮文本文件並使用NSURLConnection在objective-c
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);
並且這是將文件發佈到服務器的代碼。
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];
並且這個效果很好。任何人都可以幫助我將其轉換爲zip文件(包括需要包含在.m文件中的東西),並將其發佈到服務器。先謝謝你。
看看[這裏](https://github.com/ZipArchive/ZipArchive)庫。 – Mahesh
我有大量的例子像這些@mahesh,但我不清楚標題包含和東西..所以你可以解釋,如果它是可能的? –