gpt4 book ai didi

objective-c - Objective Zip 不适用于 iOS5.1 设备,无法正确解压缩

转载 作者:行者123 更新时间:2023-11-28 18:39:33 32 4
gpt4 key购买 nike

我在项目中使用 objective zip 将文件解压缩并存储到文档目录。它适用于 iOS 5.0 及以下版本。使用 5.1 模拟器工作正常。但是在 5.1 设备上工作时,只有几个文件被解压缩。文件夹中没有显示任何其他内容。下面是用于解压和存储的代码。

for (NSString *file in fileArray) {

// Get the file info/name, prepare the target name/path
ZipReadStream *readStream = [unzipFile readCurrentFileInZip];
FileInZipInfo *fileInfo = [unzipFile getCurrentFileInZipInfo];
fileNameString = [NSString stringWithFormat:@"%@",[fileInfo name]];

NSLog(@"fileNameString %@",fileNameString);

NSString *DirName = [targetFolder stringByAppendingPathComponent:moduleName];
NSLog(@"targetFolder %@",targetFolder);
NSLog(@"DirName %@",DirName);

NSLog(@"fileNameString %@",fileNameString);

if (![fileManager fileExistsAtPath:DirName isDirectory:nil]) {
[fileManager createDirectoryAtPath:DirName attributes:nil];
NSLog(@"created directory %@", DirName);
}

NSLog(@"fileNameString %@",fileNameString);

NSString *unzipFilePath = [DirName stringByAppendingPathComponent:fileNameString];

NSLog(@"unzipFilePath-- %@",unzipFilePath);

// Create a file handle for writing the unzipped file contents
if (![fileManager fileExistsAtPath:unzipFilePath]) {

NSString *dir = unzipFilePath;//[unzipFilePath stringByDeletingLastPathComponent];
if ([[fileNameString pathExtension] isEqualToString:@""]) {
[fileManager createDirectoryAtPath:dir attributes:nil];
NSLog(@"created directory %@", dir);
}
[fileManager createFileAtPath:unzipFilePath contents:nil attributes:nil];
}

fileHandle = [NSFileHandle fileHandleForWritingAtPath:unzipFilePath];
// Read-then-write buffered loop to conserve memory
do {
// Reset buffer length
[unzipBuffer setLength:BUFFER_SIZE];
// Expand next chunk of bytes
int bytesRead = [readStream readDataWithBuffer:unzipBuffer];
if (bytesRead > 0) {
// Write what we have read
[unzipBuffer setLength:bytesRead];
[fileHandle writeData:unzipBuffer];
} else
break;
} while (YES);

[readStream finishedReading];

// NOTE: Disable iCloud backup for unzipped file if applicable here!
/*...*/


//[fileHandle writeData:unZipped];

[fileHandle closeFile];

[unzipFile goToNextFileInZip];
}

[unzipFile close]; // Be sure to also manage your memory manually if not using ARC!

// Also delete the zip file here to conserve disk space if applicable!
[recievedData release];

NSLog(@"Delete -- %@", documentsDirectory);
[fileManager removeItemAtPath:documentsDirectory error:nil];

return YES;

}

请帮忙! ! !

提前致谢

最佳答案

使用以下方法使用 Objective-zip 解压缩 zip 文件。这在 iOS 4.3 到 6.0(可能更早和更高版本)下工作正常。 “文件名”参数是 zip 文件的完整路径。

- (BOOL)unzipPath:(NSString *)filename toDirectory:(NSString *)directory error:(NSError **)error {
if (error) {
*error = nil;
}

ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:filename mode:ZipFileModeUnzip];
int cnt = [unzipFile numFilesInZip];
[unzipFile goToFirstFileInZip];
for (int i = 0; i < cnt; i++) {
FileInZipInfo *info = [unzipFile getCurrentFileInZipInfo];
NSString *name = info.name;
if (![name hasSuffix:@"/"]) {
NSString *filePath = [directory stringByAppendingPathComponent:name];
NSString *basePath = [filePath stringByDeletingLastPathComponent];
if (![[NSFileManager defaultManager] createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:error]) {
[unzipFile close];

return NO;
}

[[NSData data] writeToFile:filePath options:0 error:nil];

NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
ZipReadStream *read = [unzipFile readCurrentFileInZip];
NSUInteger count;
NSMutableData *data = [NSMutableData dataWithLength:2048];
while ((count = [read readDataWithBuffer:data])) {
data.length = count;
[handle writeData:data];
data.length = 2048;
}
[read finishedReading];
[handle closeFile];
}

[unzipFile goToNextFileInZip];
}

[unzipFile close];

return YES;
}

关于objective-c - Objective Zip 不适用于 iOS5.1 设备,无法正确解压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13012661/

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