2017-04-25 61 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,所有工作都是正確的。 (在我的問題中有相對路徑) –