我用zipArchives创建zip文件,代码如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [paths objectAtIndex:0];
NSString *zipFile = [docspath stringByAppendingPathComponent:@"file.zip"];
ZipArchive *za = [[ZipArchive alloc] init];
[za CreateZipFile2:zipFile Password:@"1234"];
NSString *imagePath = [docspath stringByAppendingPathComponent:@"photo.png"];
NSString *textPath = [docspath stringByAppendingPathComponent:@"text.txt"];
[za addFileToZip:imagePath newname:@"photozip.png"];
[za addFileToZip:textPath newname:@"textzip.txt"];
BOOL success = [za CloseZipFile2];
这段代码应该没问题,当我进入我的目录时,存在该文件,当我尝试打开时,他要求输入密码。
注意:如果我输入正确的密码,则zip文件显示密码不正确...
我使用此代码打开并解压缩zip文件中的文件:
NSString *subdirectory = @"archive"; // the folder name to which you will unzip the files
NSString
*documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *archivePath = [documentsPath stringByAppendingPathComponent:subdirectory];
ZipArchive *za1 = [[ZipArchive alloc] init];
if ([za1 UnzipOpenFile:zipFile Password:@"1234"]) {
BOOL ret = [za1 UnzipFileTo:archivePath overWrite:YES];
NSAssert(ret, @"Unzip failed");
}
无论有没有密码,他都可以'提取' 文件,密码用实际字节提取真实文件,无需密码(考虑到zip文件是用密码保护的)可以使用其名称提取文件,但使用0字节。
如何解决这个问题?