ios本地文件添加属性

以前开发的时候想要给文件添加一个属性,系统提供的属性不太够, 想增加新的额外属性 . 当时没找到,今天偶然看见了,感激并记录下.

http://www.tanhao.me/pieces/1102.html/ 

http://www.cnblogs.com/goodboy-heyang/p/5201322.html

每一个文件都有一组属性信息,我们通常可以用以下几种方法来获得属性信息:

NSString *path = @"...";

//通过NSFileManager获取文件的属性

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:NULL];

NSLog(@"%@",attributes);

//通过MDItem获取文件的属性

MDItemRef item = MDItemCreate(kCFAllocatorDefault, (__bridge CFStringRef)path);

CFArrayRef names = MDItemCopyAttributeNames(item);

attributes = (__bridge_transfer NSDictionary *)(MDItemCopyAttributes(item, names));

CFRelease(item);

NSLog(@"%@",attributes);

//通过NSURL获取文件属性

NSURL *fileUrl = [NSURL fileURLWithPath:path];

attributes = [fileUrl resourceValuesForKeys:[NSArray arrayWithObject:NSURLFileSizeKey] error:NULL];

NSLog(@"%@",attributes);

 

但如果我们需要把自定义的一些数据写入到属性中怎么办呢(比如Safari下载文件之后会将文件的来源信息写入到属性中)?下面就通过两种不同的方式来实现:

 

方法一(通过定义在sys/xattr.h的函数实现):

#include <sys/xattr.h>
//为文件增加一个扩展属性
- (BOOL)extended1WithPath:(NSString *)path key:(NSString *)key value:(NSData *)value
{
    ssize_t writelen = setxattr([path fileSystemRepresentation],
                                [key UTF8String],
                                [value bytes],
                                [value length],
                                0,
                                0);
    return writelen==0?YES:NO;
}
//读取文件扩展属性
- (NSData *)extended1WithPath:(NSString *)path key:(NSString *)key
{
    ssize_t readlen = 1024;
    do {
        char buffer[readlen];
        bzero(buffer, sizeof(buffer));
        size_t leng = sizeof(buffer);
        readlen = getxattr([path fileSystemRepresentation],
                           [key UTF8String],
                           buffer,
                           leng,
                           0,
                           0);
        if (readlen < 0){
            return nil;
        }
        else if (readlen > sizeof(buffer)) {
            continue;
        }else{
            NSData *result = [NSData dataWithBytes:buffer length:readlen];
            return result;
        }
    } while (YES);
    return nil;
}

 

 

方法二(通过NSFileManager一个特殊的AttributeName):

//为文件增加一个扩展属性
- (BOOL)extended2WithPath:(NSString *)path key:(NSString *)key value:(NSData *)value
{
    NSDictionary *extendedAttributes = [NSDictionary dictionaryWithObject:
                                        [NSDictionary dictionaryWithObject:value forKey:key]
                                                                   forKey:@"NSFileExtendedAttributes"];
    
    NSError *error = NULL;
    BOOL sucess = [[NSFileManager defaultManager] setAttributes:extendedAttributes
                                                   ofItemAtPath:path error:&error];
    return sucess;
}
//读取文件扩展属性
- (NSData *)extended2WithPath:(NSString *)path key:(NSString *)key
{
    NSError *error = NULL;
    NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
    if (!attributes) {
        return nil;
    }
    NSDictionary *extendedAttributes = [attributes objectForKey:@"NSFileExtendedAttributes"];
    if (!extendedAttributes) {
        return nil;
    }
    return [extendedAttributes objectForKey:key];
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值