作業

#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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章