在Swift中获取文件大小


74

我尝试了几种方法来获取文件大小,但始终为零。

let path = NSBundle.mainBundle().pathForResource("movie", ofType: "mov")
let attr = NSFileManager.defaultManager().attributesOfFileSystemForPath(path!, error: nil)
if let attr = attr {
    let size: AnyObject? = attr[NSFileSize]
    println("File size = \(size)")
}

我在日志中: File size = nil


您确定文件位于顶级应用程序捆绑包中吗?
zaph 2015年

4
或者尝试使用attributesOfItemAtPath而不是attributesOfFileSystemForPath。您可以在attr上调用.fileSize()以获取文件大小。
Duyen-Hoa

1
@ tyt_g207我相信这是正确的解决方案,但您应该将其写为答案而不是评论,以便将来寻求答案的人更容易找到。
林德西·斯科特

Answers:


129

使用attributesOfItemAtPath而不是attributesOfFileSystemForPath +在attr上调用.fileSize()。

var filePath: NSString = "your path here"
var fileSize : UInt64
var attr:NSDictionary? = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: nil)
if let _attr = attr {
    fileSize = _attr.fileSize();
}

在Swift 2.0中,我们使用do try catch模式,如下所示:

let filePath = "your path here"
var fileSize : UInt64 = 0

do {
    let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(filePath)

    if let _attr = attr {
        fileSize = _attr.fileSize();
    }
} catch {
    print("Error: \(error)")
}

在Swift 3.x / 4.0中:

let filePath = "your path here"
var fileSize : UInt64

do {
    //return [FileAttributeKey : Any]
    let attr = try FileManager.default.attributesOfItem(atPath: filePath)
    fileSize = attr[FileAttributeKey.size] as! UInt64

    //if you convert to NSDictionary, you can get file size old way as well.
    let dict = attr as NSDictionary
    fileSize = dict.fileSize()
} catch {
    print("Error: \(error)")
}

3
是不是只是我认为SWIFT 2件只是做事情很多更糟。我对&error非常满意。不是表达式的try-catch很烂。
Martin Algesten'6

1
您随时可以使用try?并获得可选的返回。
2013年

1
这是什么结果?我是fe32768.0吗?相当32KB?
David Seek

1
@BrandonA,以字节为单位
Duyen-Hoa

36

Swift4:URL扩展名可轻松访问文件属性

延期:

extension URL {
    var attributes: [FileAttributeKey : Any]? {
        do {
            return try FileManager.default.attributesOfItem(atPath: path)
        } catch let error as NSError {
            print("FileAttribute error: \(error)")
        }
        return nil
    }

    var fileSize: UInt64 {
        return attributes?[.size] as? UInt64 ?? UInt64(0)
    }

    var fileSizeString: String {
        return ByteCountFormatter.string(fromByteCount: Int64(fileSize), countStyle: .file)
    }

    var creationDate: Date? {
        return attributes?[.creationDate] as? Date
    }
}

用法:

let fileUrl: URL
print("file size = \(fileUrl.fileSize), \(fileUrl.fileSizeString)")

1
URL直接使用提供文件属性resourceValues(forKeys
vadian '18

这是最好的答案。应该是公认的答案
Ahufford '19

19

SWIFT 3来自@Hoa的答案,再加上一个使UInt64可读的String的函数。

func sizeForLocalFilePath(filePath:String) -> UInt64 {
    do {
        let fileAttributes = try FileManager.default.attributesOfItem(atPath: filePath)
        if let fileSize = fileAttributes[FileAttributeKey.size]  {
            return (fileSize as! NSNumber).uint64Value
        } else {
            print("Failed to get a size attribute from path: \(filePath)")
        }
    } catch {
        print("Failed to get file attributes for local path: \(filePath) with error: \(error)")
    }
    return 0
}
func covertToFileString(with size: UInt64) -> String {
    var convertedValue: Double = Double(size)
    var multiplyFactor = 0
    let tokens = ["bytes", "KB", "MB", "GB", "TB", "PB",  "EB",  "ZB", "YB"]
    while convertedValue > 1024 {
        convertedValue /= 1024
        multiplyFactor += 1
    }
    return String(format: "%4.2f %@", convertedValue, tokens[multiplyFactor])
}

2
除covertToFileString上的错误外,其他均正常运行。替换while size > 1024while convertedValue > 1024以避免无限循环
Nicolas Lauquin

17

在Swift 3+中,您可以直接从URL获取文件大小,(NS)FileManager这不是必需的。并且ByteCountFormatter是一种显示文件大小的聪明方法。

let url = Bundle.main.url(forResource:"movie", withExtension: "mov")!
do {
    let resourceValues = try url.resourceValues(forKeys: [.fileSizeKey])
    let fileSize = resourceValues.fileSize!
    print("File size = " + ByteCountFormatter().string(fromByteCount: Int64(fileSize)))
} catch { print(error) }

实际上,您可以URL在Swift 2中从偶数获得文件的大小,但是语法有点麻烦。


“是,但是”资源值.fileSize定义为Int,而FileManager.size通过NSNumber长时间未签名。无论如何在不久的将来都不会在iOS上有所作为,但必须指出。
Frederik Winkelsdorf

10

这是Biodave的答案,带有正确的文件管理器调用

func sizeForLocalFilePath(filePath:String) -> UInt64 {
    do {
        let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(filePath)
        if let fileSize = fileAttributes[NSFileSize]  {
            return (fileSize as! NSNumber).unsignedLongLongValue
        } else {
            print("Failed to get a size attribute from path: \(filePath)")
        }
    } catch {
        print("Failed to get file attributes for local path: \(filePath) with error: \(error)")
    }
    return 0
}

对于Swift 4.2:

func sizeForLocalFilePath(filePath:String) -> UInt64 {
    do {
        let fileAttributes = try FileManager.default.attributesOfItem(atPath: filePath)
        if let fileSize = fileAttributes[FileAttributeKey.size]  {
            return (fileSize as! NSNumber).uint64Value
        } else {
            print("Failed to get a size attribute from path: \(filePath)")
        }
    } catch {
        print("Failed to get file attributes for local path: \(filePath) with error: \(error)")
    }
    return 0
}

9

Swift 4解决方案:此函数返回MB大小。

func sizePerMB(url: URL?) -> Double {
    guard let filePath = url?.path else {
        return 0.0
    }
    do {
        let attribute = try FileManager.default.attributesOfItem(atPath: filePath)
        if let size = attribute[FileAttributeKey.size] as? NSNumber {
            return size.doubleValue / 1000000.0
        }
    } catch {
        print("Error: \(error)")
    }
    return 0.0
}

直到我这么说,我才真正绊倒。做了几乎相同的事情,但是没有使用URL的path属性。谢了哥们!
Felipe

如何使其返回kb大小?
Khodour.F,

7

这是Swift 4的另外两个不同的实现,一个实现了详细格式,另一个实现了十进制格式。

带有NumberFomatter

func fileSize(fromPath path: String) -> String? {
    var size: Any?
    do {
        size = try FileManager.default.attributesOfItem(atPath: path)[FileAttributeKey.size]
    } catch (let error) {
        print("File size error: \(error)")
        return nil
    }
    guard let fileSize = size as? UInt64 else {
        return nil
    }

    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.formatterBehavior = .behavior10_4
    return formatter.string(from: NSNumber(value: fileSize))
}

另一个是用尺寸单位定义:

func fileSize(fromPath path: String) -> String? {
    guard let size = try? FileManager.default.attributesOfItem(atPath: path)[FileAttributeKey.size],
        let fileSize = size as? UInt64 else {
        return nil
    }

    // bytes
    if fileSize < 1023 {
        return String(format: "%lu bytes", CUnsignedLong(fileSize))
    }
    // KB
    var floatSize = Float(fileSize / 1024)
    if floatSize < 1023 {
        return String(format: "%.1f KB", floatSize)
    }
    // MB
    floatSize = floatSize / 1024
    if floatSize < 1023 {
        return String(format: "%.1f MB", floatSize)
    }
    // GB
    floatSize = floatSize / 1024
    return String(format: "%.1f GB", floatSize)
}

7

Swift中的URL扩展名以获取文件大小(如果有)。

public extension URL {

    var fileSize: Int? {
        let value = try? resourceValues(forKeys: [.fileSizeKey])
        return value?.fileSize
    }
}

返回一个可选的Int的原因是因为一个未知的文件大小不能被认为是零大小。


6

尝试这个。

let MyUrl = NSURL(fileURLWithPath: "*** Custom File Path ***")                  
let fileAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(MyUrl.path!)
let fileSizeNumber = fileAttributes[NSFileSize] as! NSNumber
let fileSize = fileSizeNumber.longLongValue
var sizeMB = Double(fileSize / 1024)
sizeMB = Double(sizeMB / 1024)
print(String(format: "%.2f", sizeMB) + " MB")

1

在Swift 3.0中,尝试以下操作:

let fileSize = try! FileManager.default.attributesOfItem(atPath: "/bin/bash")[FileAttributeKey.size] as! Int

甚至更好:

let fileSize = (try! FileManager.default.attributesOfItem(atPath: "/bin/bash")[FileAttributeKey.size] as! NSNumber).uint64Value

第二个更好吗?为什么不应该使用NSNumber自动转换?您还应该尝试吗?并作为?使用if或guard语句作为文件操作可能会失败。
塔帕尼

1

其他答案的启示。

extension URL {
    var filesize: Int? {
        let set = Set.init([URLResourceKey.fileSizeKey])
        var filesize: Int?      
        do {
            let values = try self.resourceValues(forKeys: set)
            if let theFileSize = values.fileSize {
                filesize = theFileSize
            }
        }
        catch {
            print("Error: \(error)")
        }
        return filesize
    }

    var filesizeNicelyformatted: String? {
        guard let fileSize = self.filesize else {
            return nil
        }
        return ByteCountFormatter.init().string(fromByteCount: Int64(fileSize))
    }
}

0

这是通过Xcode7在iOS 9的Swift 2.0中编写为快乐方法的紧凑版本:

func sizeForLocalFilePath(filePath:String) -> UInt64 {
    do {
        let fileAttributes = try NSFileManager().attributesOfFileSystemForPath(filePath)
        if let fileSize = fileAttributes[NSFileSystemSize] as? UInt64 {
            return fileSize
        } else {
            print("Failed to get a size attribute from path: \(filePath)")
        }
    } catch {
        print("Failed to get file attributes for local path: \(filePath) with error: \(error)")
    }
    return 0
}

请享用!


这行不通。它正在检查文件系统属性,而不是文件本身的属性。
盖·布鲁克
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.