Created
December 3, 2013 05:39
-
-
Save demonnico/7764414 to your computer and use it in GitHub Desktop.
get totally files size in directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@impelementaton NSString(Utils) | |
+ (uint64_t)sizeAtPath:(NSString *)filePath diskMode:(BOOL)diskMode | |
{ | |
uint64_t totalSize = 0; | |
NSMutableArray *searchPaths = [NSMutableArray arrayWithObject:filePath]; | |
while ([searchPaths count] > 0) | |
{ | |
@autoreleasepool | |
{ | |
NSString *fullPath = [searchPaths objectAtIndex:0]; | |
[searchPaths removeObjectAtIndex:0]; | |
struct stat fileStat; | |
if (lstat([fullPath fileSystemRepresentation], &fileStat) == 0) | |
{ | |
if (fileStat.st_mode & S_IFDIR) | |
{ | |
NSArray *childSubPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:nil]; | |
for (NSString *childItem in childSubPaths) | |
{ | |
NSString *childPath = [fullPath stringByAppendingPathComponent:childItem]; | |
[searchPaths insertObject:childPath atIndex:0]; | |
} | |
}else | |
{ | |
if (diskMode) | |
totalSize += fileStat.st_blocks*512; | |
else | |
totalSize += fileStat.st_size; | |
} | |
} | |
} | |
} | |
return totalSize; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment