![](/img/trans.png)
[英]Bug in NSFileManager (instance method is traversing symbolic link)
[英]NSFileManager create symbolic link with content
我需要从zip存档中提取文件。 该文件的一部分可以是符号链接。 该文件的内容看起来像../../../Braintree/BraintreeUI/Public/UIColor+BTUI.h
我的解压缩库(ZipZap)具有文件属性,所以我一直都知道-这个文件是符号链接,还是它是常规文件:
... // 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;
使用归档entry.fileMode
值使用entry.fileMode
创建(或更新)文件属性和文件类型的正确方法是什么? 在我的测试中是41453
。
您需要使用createSymbolicLinkAtPath:withDestinationPath:error:
来创建符号链接,您无法创建常规文件并将其转换为链接。 高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.