iOS 计算文件夹中包含所有文件的大小
文件大小:
计算输出大小:
方法一:
classfunc fileSize(path:NSString!)->Float64{
let fileManager =NSFileManager()
var size:Float64 =0.0
do {
let arr =try fileManager.contentsOfDirectoryAtPath(pathas String)
if arr.count ==0 {return size}//当一个文件夹没文件的时候
for indexin 0...arr.count-1 {
let fullPath = path.stringByAppendingPathComponent(arr[index])
var isDir:ObjCBool =false // isDir判断是否为文件夹
if fileManager.fileExistsAtPath(fullPath, isDirectory: &isDir) && !isDir { //不是文件夹计算出大小
let fileAttributeDic =try! fileManager.attributesOfItemAtPath(fulllPath)
size += (fileAttributeDic[NSFileSize]?.doubleValue)!
}else {
size += FileManagerTool.fileSize(fullPath)//递归计算其大小
}
}
return size
}catch {
return size
}
}
let fileManager =NSFileManager()
var size:Float64 =0.0
let arr = fileManager.subpathsAtPath(pathas String)
if arr!.count ==0 {return size}//当一个文件夹没文件的时候
for indexin 0...arr!.count-1 {
let fullPath = path.stringByAppendingPathComponent(arr![index])
var isDir:ObjCBool =false // isDir判断是否为文件夹
if fileManager.fileExistsAtPath(fullPath, isDirectory: &isDir) && !isDir {//不是文件夹计算出大小
let fileAttributeDic =try! fileManager.attributesOfItemAtPath(fullPath)
size += (fileAttributeDic[NSFileSize]?.doubleValue)!
}
}
return size
}