-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSFileManager.m
More file actions
184 lines (157 loc) · 5.83 KB
/
LSFileManager.m
File metadata and controls
184 lines (157 loc) · 5.83 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// LSFileManager.m
// LSFileManager
//
// Created by StephenChen on 14/12/18.
// Copyright (c) 2014年 Lansion. All rights reserved.
//
#import "LSFileManager.h"
@interface LSFileManager()
@property (nonatomic) NSFileManager* fileManager;
@property (nonatomic) NSString* homeDir;
@property (nonatomic) NSArray* docDir;
@property (nonatomic) NSArray* cacheDir;
@property (nonatomic) NSArray* libDir;
@property (nonatomic) NSString* tmpDir;
@end
static LSFileManager * manager;
@implementation LSFileManager
- (instancetype)init
{
self = [super init];
if (self) {
_fileManager = [[NSFileManager alloc] init];
}
return self;
}
+ (BOOL) setUp {
if (manager == nil) {
manager = [[LSFileManager alloc] init];
manager.homeDir = NSHomeDirectory();
manager.docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
manager.cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
manager.libDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
manager.tmpDir = NSTemporaryDirectory();
}
return YES;
}
+ (NSString*) getDir:(enum LSDirectory) dir {
switch (dir) {
case DOC_DIR:
return [manager.docDir objectAtIndex:0];
case CACHE_DIR:
return [manager.cacheDir objectAtIndex:0];
case LIB_DIR:
return [manager.libDir objectAtIndex:0];
case TMP_DIR:
return manager.tmpDir;
case HOME_DIR:
return manager.homeDir;
default:
return manager.homeDir;
}
return nil;
}
+ (NSArray*) listFolder:(enum LSDirectory) dir path:(NSString*) path {
NSString *docDir;
if (path == nil || (path != nil && [path length] == 0)) {
docDir = [LSFileManager getDir:dir];
} else {
docDir = [NSString stringWithFormat:@"%@/%@", [LSFileManager getDir:dir], path];
}
NSArray* list = [manager.fileManager subpathsAtPath:docDir];
return list;
}
+ (BOOL) deleteFile:(NSString *)filePath under:(enum LSDirectory)dir {
NSString *path = nil;
if (filePath == nil || (filePath != nil && [filePath length] == 0)) {
return NO;
} else {
path = [NSString stringWithFormat:@"%@/%@", [LSFileManager getDir:dir], filePath];
}
// NSLog(@"delete file %@", path);
BOOL re = [manager.fileManager removeItemAtPath:path error:nil];
return re;
}
+ (BOOL) isExisted:(NSString *)filePath under:(enum LSDirectory)dir {
NSString *path = nil;
if (filePath == nil || (filePath != nil && [filePath length] == 0)) {
return NO;
} else {
NSString *dirPath = [LSFileManager getDir:dir];
path = [dirPath stringByAppendingPathComponent:filePath];
}
return [manager.fileManager fileExistsAtPath:path];
}
+ (BOOL) createFolder:(NSString*) folderName under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:folderName];
return [manager.fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
+ (NSString*) writeNSData:(NSData*) data toFile:(NSString*)filePath under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:filePath];
BOOL re = [data writeToFile:path atomically:YES];
if (re) {
return path;
} else {
return nil;
}
}
+ (NSData*) readNSDataFile:(NSString*)filePath under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:filePath];
NSData* data = [NSData dataWithContentsOfFile:path];
return data;
}
+ (NSString*) writeNSArray:(NSArray*) data toFile:(NSString*)filePath under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:filePath];
BOOL re = [data writeToFile:path atomically:YES];
if (re) {
return path;
} else {
return nil;
}
}
+ (NSArray*) readNSArrayFile:(NSString*)filePath under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:filePath];
NSArray* data = [NSArray arrayWithContentsOfFile:path];
return data;
}
+ (NSMutableArray*) readNSMutableArrayFile:(NSString*)filePath under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:filePath];
NSMutableArray* data = [NSMutableArray arrayWithContentsOfFile:path];
return data;
}
+ (NSString*) writeNSDictionary:(NSDictionary*) data toFile:(NSString*)filePath under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:filePath];
BOOL re = [data writeToFile:path atomically:YES];
if (re) {
return path;
} else {
return nil;
}
}
+ (NSDictionary*) readNSDictionaryFile:(NSString*)filePath under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:filePath];
NSDictionary* data = [NSDictionary dictionaryWithContentsOfFile:path];
return data;
}
+ (NSMutableDictionary*) readNSMutableDictionaryFile:(NSString*)filePath under:(enum LSDirectory)dir {
NSString *dirPath = [LSFileManager getDir:dir];
NSString *path = [dirPath stringByAppendingPathComponent:filePath];
NSMutableDictionary* data = [NSMutableDictionary dictionaryWithContentsOfFile:path];
return data;
}
+ (BOOL) checkSetUp {
if (manager == nil) {
// NSLog(@"LS FileManager hasn't been initialized");
}
return manager != nil;
}
@end