gpt4 book ai didi

ios - iOS 中未正确创建 Zip 文件

转载 作者:行者123 更新时间:2023-11-29 00:49:01 27 4
gpt4 key购买 nike

我正在将所有 NSLOG 写入一个文本文件,并在单击表格 View 单元格时将其发布到服务器。在使用 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");
}

任何帮助都会有很大帮助。

最佳答案

您可以使用这个库 Objective-Zip ,实际上我已经使用过它并且它对我来说工作正常。

创建 Zip 文件:

ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"Logger.zip" mode:ZipFileModeCreate];

将文件添加到 zip 文件

ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"Logger.txt" compressionLevel:ZipCompressionLevelBest];

[流写入数据: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];

希望这段代码能帮到你:)快乐编码! ;)

关于ios - iOS 中未正确创建 Zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com