2016-07-06 67 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的其余部分保持不变? –

相关问题