简体   繁体   English

NSFileManager创建带有内容的符号链接

[英]NSFileManager create symbolic link with content

I need to extract files from zip archive. 我需要从zip存档中提取文件。 Part of that files can be symbolic links. 该文件的一部分可以是符号链接。 Contents of that files looks like ../../../Braintree/BraintreeUI/Public/UIColor+BTUI.h My unzip library (ZipZap) have files properties, so I always know - is this file is a symbolic link, or it is regular file: 该文件的内容看起来像../../../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;

So, if isSymlink == YES my task is to create symbolic link file and write archive item content to that file. 因此,如果isSymlink == YES我的任务是创建符号链接文件并将存档项目内容写入该文件。 I use this code: 我使用以下代码:

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];

But as result I get regular file in Finder. 但是结果是我在Finder中得到了常规文件。 When I extract my archive with built-in Mac archive utility - I get correct symbolic links. 当我使用内置的Mac存档实用程序提取存档时-得到正确的符号链接。

I also try this trick to change file type after file creation: 我也尝试使用以下技巧文件创建更改文件类型:

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];

There is no errors, but original and updated dictionaries are same: 没有错误,但是原始和更新的词典是相同的:

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;

What is the correct way to create (or update) file attributes and file type with NSFileManager, using archive entry.fileMode value? 使用归档entry.fileMode值使用entry.fileMode创建(或更新)文件属性和文件类型的正确方法是什么? In my tests it was 41453 . 在我的测试中是41453

You need to use createSymbolicLinkAtPath:withDestinationPath:error: to create a symbolic link, you cannot create a regular file and convert it to a link. 您需要使用createSymbolicLinkAtPath:withDestinationPath:error:来创建符号链接,您无法创建常规文件并将其转换为链接。 HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM