- (void)fileArrayWithFilePath:(NSString *)filePath {
if ([self isDirectory:filePath]) {
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:nil];
for (NSInteger i = 0; i < array.count; i ++) {
if([self isDirectory:[filePath stringByAppendingPathComponent:array[i]]]) {
[self.folderArray addObject:[filePath stringByAppendingPathComponent:array[i]]];
} else {
[self.fileArray addObject:[filePath stringByAppendingPathComponent:array[i]]];
}
}
while (self.folderArray.count > 0) {
NSString *firstObject = [self.folderArray firstObject];
NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:firstObject error:nil];
for (NSInteger i = 0; i < arr.count; i ++) {
if([self isDirectory:[firstObject stringByAppendingPathComponent:arr[i]]]) {
[self.folderArray addObject:[firstObject stringByAppendingPathComponent:arr[i]]];
} else {
[self.fileArray addObject:[firstObject stringByAppendingPathComponent:arr[i]]];
}
}
[self.folderArray removeObject:firstObject];
}
for (NSInteger i = 0; i < self.fileArray.count ; i ++) {
NSString *str = self.fileArray[i];
[self.fileData addObject:[[str componentsSeparatedByString:@"/"] lastObject]];
}
} else {//文件
[self.fileData addObject:[[filePath componentsSeparatedByString:@"/"] lastObject]];
}
}
- (BOOL)isDirectory:(NSString *)filePath {
BOOL isDirectory = NO;
[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
return isDirectory;
}
来源:oschina
链接:https://my.oschina.net/gwlCode/blog/4289062