• 获取文件夹大小


    IOS里面没有获取整个目录里面所有文件大小的函数,所以只能遍历然后累加。

    //获取文件大小 
    +(long long)getFileSize:(NSString*)filePath

    {
        return [self _folderSizeAtPath:[filePath cStringUsingEncoding:NSUTF8StringEncoding]];
    }

    + (long long) _folderSizeAtPath: (const char*)folderPath

    {
    long long folderSize = 0;
    DIR* dir = opendir(folderPath);
    if (dir == NULL) return 0;
    struct dirent* child;
    while ((child = readdir(dir))!=NULL)

    {
    if (child->d_type DT_DIR &&,,,,,,,,,,,,,,,,,,,,,, (
    (child->d_name[0] '.' && child->d_name1 0) || // 忽略目录 .
    (child->d_name[0] '.' && child->d_name1 '.' && child->d_name[2] 0) // 忽略目录 ..
    )) continue;

    int folderPathLength = strlen(folderPath);
    char childPath[1024]; // 子文件的路径地址
    stpcpy(childPath, folderPath);
    if (folderPath[folderPathLength-1] != '/'){
    childPath[folderPathLength] = '/';
    folderPathLength++;
    }
    stpcpy(childPath+folderPathLength, child->d_name);
    childPath[folderPathLength + child->d_namlen] = 0;
    if (child->d_type == DT_DIR){ // directory
    folderSize += [self _folderSizeAtPath:childPath]; // 递归调用子目录
    // 把目录本身所占的空间也加上
    struct stat st;
    if(lstat(childPath, &st) == 0) folderSize += st.st_size;
    }else if (child->d_type DT_REG || child->d_type DT_LNK){ // file or link
    struct stat st;
    if(lstat(childPath, &st) == 0) folderSize += st.st_size;
    }
    }
    return folderSize;
    }
  • 相关阅读:
    C,C++,VC++有什么区别
    RF & Microarray
    偏最小二乘法
    各种机器学习方法的优缺点
    纠错输出编码法ECOC
    遗传算法GA
    支持向量机SVM
    神经网络NN
    机器学习的基本概念
    SPI通信协议(SPI总线)学习
  • 原文地址:https://www.cnblogs.com/417460188dy/p/3365497.html
Copyright © 2020-2023  润新知