作业
来源:互联网 发布:php清除cookie 编辑:程序博客网 时间:2024/05/16 04:06
#import <Foundation/Foundation.h>#define ImgsDirectory @"Imgs"#define DocumentsPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]typedef enum { SuccessType, FailedDownloadType, FailedWriteType, AlreadyDownloadType,}DownloadType;typedef enum { SucsessType, FailedType, NoFileType,}RemoveType;@interface Download : NSObject+(DownloadType)downloadImageInDirectoryWithUrlString:(NSString *)urlString;+(NSString *)calculateSizeWithDirectoryName:(NSString *)directoryName;+(BOOL)removeAllFileWithDirectoryName:(NSString *)directoryName;+(RemoveType)removeFileWithFileName:(NSString *)fileName inDirectoryName:(NSString *)directoryName;@end
#import "Download.h"@interface Download ()-(NSString *)createDirectoryWithDirectoryName:(NSString *)DirectoryName;-(NSString *)createFileWithFileName:(NSString *)fileName WithDirectoryName:(NSString *)DirectoryName;@end@implementation Download#pragma mark --extension---(NSString *)createDirectoryWithDirectoryName:(NSString *)DirectoryName{ //拼接文件夹路径 NSString *directoryPath = [DocumentsPath stringByAppendingPathComponent:DirectoryName]; NSFileManager *fm = [NSFileManager defaultManager]; if (![fm fileExistsAtPath:directoryPath]) { [fm createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil]; } return directoryPath;}-(NSString *)createFileWithFileName:(NSString *)fileName WithDirectoryName:(NSString *)DirectoryName{ NSString *filePath = [DocumentsPath stringByAppendingFormat:@"/%@/%@",DirectoryName,fileName]; return filePath;}+(DownloadType)downloadImageInDirectoryWithUrlString:(NSString *)urlString{ DownloadType type = FailedDownloadType; //取到 fileName NSString *fileName = [urlString lastPathComponent]; //拿到文件夹 NSString *imgsDir = [[Download new] createDirectoryWithDirectoryName:ImgsDirectory]; //找到这个文件夹下面的所有文件,查询次文件夹下是否存在当前已经下载的图片 NSArray *fileArrays = [[NSFileManager defaultManager] subpathsAtPath:imgsDir]; for (NSString *element in fileArrays) { if ([element isEqualToString:fileName]) { type = AlreadyDownloadType; break; } } if (type != AlreadyDownloadType) { urlString = [urlString stringByAddingPercentEscapesUsingEncoding:4]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; if (data.length == 0) { type = FailedDownloadType; }else{ NSString *filePath = [[Download new] createFileWithFileName:fileName WithDirectoryName:ImgsDirectory]; BOOL flag = [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil]; if (flag) { type = SuccessType; }else{ type = FailedWriteType; } } } return type;}+(NSString *)calculateSizeWithDirectoryName:(NSString *)directoryName{ NSString *directoryPath = [DocumentsPath stringByAppendingPathComponent:directoryName]; double count = 0.0; for (NSString *ele in [[NSFileManager defaultManager] subpathsAtPath:directoryPath]) { NSString *filePath = [directoryPath stringByAppendingPathComponent:ele]; NSData *data = [NSData dataWithContentsOfFile:filePath]; count += data.length; } return [NSString stringWithFormat:@"%.2f M",count/1024/1024];}+(BOOL)removeAllFileWithDirectoryName:(NSString *)directoryName{ NSString *directoryPath = [DocumentsPath stringByAppendingPathComponent:directoryName]; NSFileManager *fm = [NSFileManager defaultManager]; for (NSString *ele in [fm subpathsAtPath:directoryPath]) { NSString *filePath = [directoryPath stringByAppendingPathComponent:ele]; [fm removeItemAtPath:filePath error:nil]; } if ([fm subpathsAtPath:directoryPath].count == 0) { return YES; }else{ return NO; }}+(RemoveType)removeFileWithFileName:(NSString *)fileName inDirectoryName:(NSString *)directoryName{ RemoveType type = FailedType; NSString *directoryPath = [DocumentsPath stringByAppendingPathComponent:directoryName]; NSFileManager *fm = [NSFileManager defaultManager]; BOOL isExist = NO; for (NSString *ele in [fm subpathsAtPath:directoryPath]) { if ([ele isEqualToString:fileName]) { isExist = YES; NSString *filePath = [directoryPath stringByAppendingPathComponent:ele]; BOOL flag = [fm removeItemAtPath:filePath error:nil]; if (flag) { type = SucsessType; }else{ type = FailedType; } } } if (!isExist) { type = NoFileType; } return type;}@end
#import "ViewController.h"#import "Download.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"rootPath = %@",NSHomeDirectory()); DownloadType type = [Download downloadImageInDirectoryWithUrlString:@"http://s0.hao123img.com/res/r/image/2015-08-12/e307fb459de2f8acbf9d4f32b85755dc.jpg"]; switch (type) { case SuccessType: { NSLog(@"下载成功"); } break; case FailedWriteType: { NSLog(@"写入失败"); } break; case FailedDownloadType: { NSLog(@"无网络"); } break; case AlreadyDownloadType:{ NSLog(@"已经下载"); } break; default: break; } RemoveType removeType = [Download removeFileWithFileName:@"e307fb459de2f8acbf9d4f32b85755dc.jpg" inDirectoryName:ImgsDirectory]; switch (removeType) { case NoFileType: { NSLog(@"没有此图片"); } break; case SucsessType:{ NSLog(@"删除成功"); } break; case FailedType:{ NSLog(@"删除失败"); } break; default: break; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0