我正在将所有NSLOG写入文本文件,并在点击表格视图单元格后将其发布到服务器。我在使用NSURLConnection发布之前将文本文件转换为zip文件。但是,Zip文件中存在一些垃圾数据,但文本文件具有正确的内容。我正在使用SSZipArchive,我用来发布文件的代码是
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Logger.txt"]];
NSString* zipfile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"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);
NSString *contentOfZipFile = [NSString stringWithContentsOfFile:zipfile encoding:NSUTF8StringEncoding error:NULL];
NSData *zipData = [contentOfZipFile dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[zipData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:finalURL]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/zip" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:zipData ];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
else {
NSLog(@"Zip create error");
}
任何帮助都会有很大的帮助。
答案 0 :(得分:1)
你可以使用这个库Objective-Zip,实际上我已经使用了它,它对我来说很好。
创建Zip文件:
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"Logger.zip" mode:ZipFileModeCreate];
将文件添加到zip文件
ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"Logger.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData]; [stream finishedWriting];
从zip文件中读取文件:
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"Logger.zip" mode:ZipFileModeUnzip];
[unzipFile goToFirstFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
希望这段代码可以帮到你:) 快乐编码!! ;)