zoukankan      html  css  js  c++  java
  • 获取文件夹大小

    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;
    }
  • 相关阅读:
    LinkedList -链表集合
    java包装类,自动装箱,拆箱,以及基本数据类型与字符串的转换
    StringBuilder -字符串缓冲区,节约内层空间变长数组
    System的两常用个静态方法
    StringBuilder
    mysql
    空房间
    数据结构占坑
    sql语句优化
    editPlus快捷键
  • 原文地址:https://www.cnblogs.com/417460188dy/p/3365497.html
Copyright © 2011-2022 走看看