簡體   English   中英

NSFileManager創建帶有內容的符號鏈接

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM