2016-07-06 25 views
1

我有一個文本文件,我在其中編寫所有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文件中的東西),並將其發佈到服務器。先謝謝你。

+0

看看[這裏](https://github.com/ZipArchive/ZipArchive)庫。 – Mahesh

+0

我有大量的例子像這些@mahesh,但我不清楚標題包含和東西..所以你可以解釋,如果它是可能的? –

回答

1

按照以下方式創建記錄器文件。我只是在不同目錄中創建文件而不是文檔目錄,所以我們可以壓縮該目錄。

//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); 

現在 #進口 「SSZipArchive.h」 在viewController.m

要創建zip文件使用下面的代碼

//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"); 
} 
+0

什麼應該是HTTPHeaderField「Content-type」的設置值?應用程序/ zip? 。壓縮?? –

+0

@ManeeshSharma是的,它是應用程序/ zip或應用程序/八位字節流。 – Mahesh

+0

而且@Mahesh的其餘部分保持不變? –

相關問題