2017-04-25 59 views
0

我需要从zip压缩文件中提取文件。部分文件可以是符号链接。那文件的内容看起来像../../../Braintree/BraintreeUI/Public/UIColor+BTUI.h我解压库(ZipZap)具有文件的属性,所以我总是知道 - 这个文件是一个符号链接,或者它是常规文件:NSFileManager创建与内容的符号链接

... // open and read archive, list entries 
ZZArchiveEntry *entry = ... // get one entry 
BOOL isSymlink = (entry.fileMode & S_IFLNK) == S_IFLNK; 

所以,如果isSymlink == YES我的任务是创建符号链接文件并将归档项目内容写入该文件。我使用这个代码:

NSData *fileData = [entry newDataWithError:nil]; // valid NSData, contents as described above 
NSString *filePath = ... // correct and writable path 
NSDictionary *attributes = @{NSFileType:NSFileTypeSymbolicLink}; 

[[NSFileManager defaultManager] createFileAtPath:filePath contents:fileData attributes:attributes]; 

但因为我在Finder中得到常规文件。当我使用内置的Mac归档实用工具提取存档时 - 我得到正确的符号链接。

我也试试这招后文件创建更改文件类型

NSDictionary *original = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; 

NSMutableDictionary *newProperties = [original mutableCopy]; 
newProperties[NSFileType] = NSFileTypeSymbolicLink; 

[[NSFileManager defaultManager] setAttributes:newProperties ofItemAtPath:filePath error:nil]; 

NSDictionary *updated = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; 

没有错误,但原始的和更新的字典是一样的:

NSFileCreationDate = "2017-04-25 22:09:04 +0000"; 
NSFileExtensionHidden = 0; 
NSFileGroupOwnerAccountID = 20; 
NSFileGroupOwnerAccountName = staff; 
NSFileHFSCreatorCode = 0; 
NSFileHFSTypeCode = 0; 
NSFileModificationDate = "2017-04-25 22:09:04 +0000"; 
NSFileOwnerAccountID = 501; 
NSFileOwnerAccountName = wind; 
NSFilePosixPermissions = 420; 
NSFileReferenceCount = 1; 
NSFileSize = 55; 
NSFileSystemFileNumber = 43896483; 
NSFileSystemNumber = 16777217; 
NSFileType = NSFileTypeRegular; 

什么是正确的用NSFileManager创建(或更新)文件属性和文件类型的方法,使用档案entry.fileMode值?在我的测试中,它是41453

回答

2

您需要使用createSymbolicLinkAtPath:withDestinationPath:error:来创建符号链接,您不能创建常规文件并将其转换为链接。 HTH

+0

谢谢。我从fileData字符串获取DestinationPath,所有工作都是正确的。 (在我的问题中有相对路径) –