This repository has been archived by the owner on Sep 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
NSFileManager+CSS.m
80 lines (63 loc) · 1.97 KB
/
NSFileManager+CSS.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// NSFileManager+CSS.m
// CocoaSlideShow
//
// Created by Nicolas Seriot on 17.08.07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "NSFileManager+CSS.h"
@implementation NSFileManager (CSS)
- (BOOL)isDirectory:(NSString *)path {
BOOL isDir;
[self fileExistsAtPath:path isDirectory:&isDir];
return isDir;
}
- (NSArray *)directoryContentFullPaths:(NSString*)dirPath recursive:(BOOL)isRecursive {
if(![self isDirectory:dirPath]) {
return nil;
}
NSError *error = nil;
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:&error];
if(dirContents == nil) {
NSLog(@"-- cannot get contentsOfDirectoryAtPath:%@ error%@", dirPath, error);
return nil;
}
NSMutableArray *fullPaths = [[NSMutableArray alloc] init];
NSString *name;
NSString *currentPath;
for (name in dirContents) {
currentPath = [dirPath stringByAppendingPathComponent:name];
if([self isDirectory:currentPath]) {
if(isRecursive) {
[fullPaths arrayByAddingObjectsFromArray:[self directoryContentFullPaths:currentPath recursive:YES]];
} else {
continue;
}
}
[fullPaths addObject:[dirPath stringByAppendingPathComponent:name]];
}
return fullPaths;
}
- (NSString *)prettyFileSize:(NSString *)path {
// NSDictionary *fileAttributes = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES];
NSError *error = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
if(fileAttributes == nil) {
NSLog(@"-- can't get attributesOfItemAtPath:%@ error:%@", path, error);
return nil;
}
float fileSize = (float)[fileAttributes fileSize];
NSString *unit = @"bytes";
if(fileSize > 1024) {
fileSize /= 1024;
unit = @"KB";
}
if(fileSize > 1024) {
fileSize /= 1024;
unit = @"MB";
return [NSString stringWithFormat:@"%0.1f %@", fileSize, unit];
} else {
return [NSString stringWithFormat:@"%d %@", (int)fileSize, unit];
}
}
@end