4 |
5 | @interface NSString (Hash)
6 |
7 | #pragma mark - 散列函数
8 | /**
9 | * 计算MD5散列结果
10 | *
11 | * 终端测试命令:
12 | * @code
13 | * md5 -s "string"
14 | * @endcode
15 | *
16 | * 提示:随着 MD5 碰撞生成器的出现,MD5 算法不应被用于任何软件完整性检查或代码签名的用途。
17 | *
18 | * @return 32个字符的MD5散列字符串
19 | */
20 | - (NSString *)md5String;
21 |
22 | /**
23 | * 计算SHA1散列结果
24 | *
25 | * 终端测试命令:
26 | * @code
27 | * echo -n "string" | openssl sha -sha1
28 | * @endcode
29 | *
30 | * @return 40个字符的SHA1散列字符串
31 | */
32 | - (NSString *)sha1String;
33 |
34 | /**
35 | * 计算SHA256散列结果
36 | *
37 | * 终端测试命令:
38 | * @code
39 | * echo -n "string" | openssl sha -sha256
40 | * @endcode
41 | *
42 | * @return 64个字符的SHA256散列字符串
43 | */
44 | - (NSString *)sha256String;
45 |
46 | /**
47 | * 计算SHA 512散列结果
48 | *
49 | * 终端测试命令:
50 | * @code
51 | * echo -n "string" | openssl sha -sha512
52 | * @endcode
53 | *
54 | * @return 128个字符的SHA 512散列字符串
55 | */
56 | - (NSString *)sha512String;
57 |
58 | #pragma mark - HMAC 散列函数
59 | /**
60 | * 计算HMAC MD5散列结果
61 | *
62 | * 终端测试命令:
63 | * @code
64 | * echo -n "string" | openssl dgst -md5 -hmac "key"
65 | * @endcode
66 | *
67 | * @return 32个字符的HMAC MD5散列字符串
68 | */
69 | - (NSString *)hmacMD5StringWithKey:(NSString *)key;
70 |
71 | /**
72 | * 计算HMAC SHA1散列结果
73 | *
74 | * 终端测试命令:
75 | * @code
76 | * echo -n "string" | openssl sha -sha1 -hmac "key"
77 | * @endcode
78 | *
79 | * @return 40个字符的HMAC SHA1散列字符串
80 | */
81 | - (NSString *)hmacSHA1StringWithKey:(NSString *)key;
82 |
83 | /**
84 | * 计算HMAC SHA256散列结果
85 | *
86 | * 终端测试命令:
87 | * @code
88 | * echo -n "string" | openssl sha -sha256 -hmac "key"
89 | * @endcode
90 | *
91 | * @return 64个字符的HMAC SHA256散列字符串
92 | */
93 | - (NSString *)hmacSHA256StringWithKey:(NSString *)key;
94 |
95 | /**
96 | * 计算HMAC SHA512散列结果
97 | *
98 | * 终端测试命令:
99 | * @code
100 | * echo -n "string" | openssl sha -sha512 -hmac "key"
101 | * @endcode
102 | *
103 | * @return 128个字符的HMAC SHA512散列字符串
104 | */
105 | - (NSString *)hmacSHA512StringWithKey:(NSString *)key;
106 |
107 | #pragma mark - 文件散列函数
108 |
109 | /**
110 | * 计算文件的MD5散列结果
111 | *
112 | * 终端测试命令:
113 | * @code
114 | * md5 file.dat
115 | * @endcode
116 | *
117 | * @return 32个字符的MD5散列字符串
118 | */
119 | - (NSString *)fileMD5Hash;
120 |
121 | /**
122 | * 计算文件的SHA1散列结果
123 | *
124 | * 终端测试命令:
125 | * @code
126 | * openssl sha -sha1 file.dat
127 | * @endcode
128 | *
129 | * @return 40个字符的SHA1散列字符串
130 | */
131 | - (NSString *)fileSHA1Hash;
132 |
133 | /**
134 | * 计算文件的SHA256散列结果
135 | *
136 | * 终端测试命令:
137 | * @code
138 | * openssl sha -sha256 file.dat
139 | * @endcode
140 | *
141 | * @return 64个字符的SHA256散列字符串
142 | */
143 | - (NSString *)fileSHA256Hash;
144 |
145 | /**
146 | * 计算文件的SHA512散列结果
147 | *
148 | * 终端测试命令:
149 | * @code
150 | * openssl sha -sha512 file.dat
151 | * @endcode
152 | *
153 | * @return 128个字符的SHA512散列字符串
154 | */
155 | - (NSString *)fileSHA512Hash;
156 |
157 | @end
158 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/NSString+Hash.m:
--------------------------------------------------------------------------------
1 |
2 | #import "NSString+Hash.h"
3 | #import
4 |
5 | @implementation NSString (Hash)
6 |
7 | #pragma mark - 散列函数
8 | - (NSString *)md5String {
9 | const char *str = self.UTF8String;
10 | uint8_t buffer[CC_MD5_DIGEST_LENGTH];
11 |
12 | CC_MD5(str, (CC_LONG)strlen(str), buffer);
13 |
14 | return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH];
15 | }
16 |
17 | - (NSString *)sha1String {
18 | const char *str = self.UTF8String;
19 | uint8_t buffer[CC_SHA1_DIGEST_LENGTH];
20 |
21 | CC_SHA1(str, (CC_LONG)strlen(str), buffer);
22 |
23 | return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH];
24 | }
25 |
26 | - (NSString *)sha256String {
27 | const char *str = self.UTF8String;
28 | uint8_t buffer[CC_SHA256_DIGEST_LENGTH];
29 |
30 | CC_SHA256(str, (CC_LONG)strlen(str), buffer);
31 |
32 | return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH];
33 | }
34 |
35 | - (NSString *)sha512String {
36 | const char *str = self.UTF8String;
37 | uint8_t buffer[CC_SHA512_DIGEST_LENGTH];
38 |
39 | CC_SHA512(str, (CC_LONG)strlen(str), buffer);
40 |
41 | return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH];
42 | }
43 |
44 | #pragma mark - HMAC 散列函数
45 | - (NSString *)hmacMD5StringWithKey:(NSString *)key {
46 | const char *keyData = key.UTF8String;
47 | const char *strData = self.UTF8String;
48 | uint8_t buffer[CC_MD5_DIGEST_LENGTH];
49 |
50 | CCHmac(kCCHmacAlgMD5, keyData, strlen(keyData), strData, strlen(strData), buffer);
51 |
52 | return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH];
53 | }
54 |
55 | - (NSString *)hmacSHA1StringWithKey:(NSString *)key {
56 | const char *keyData = key.UTF8String;
57 | const char *strData = self.UTF8String;
58 | uint8_t buffer[CC_SHA1_DIGEST_LENGTH];
59 |
60 | CCHmac(kCCHmacAlgSHA1, keyData, strlen(keyData), strData, strlen(strData), buffer);
61 |
62 | return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH];
63 | }
64 |
65 | - (NSString *)hmacSHA256StringWithKey:(NSString *)key {
66 | const char *keyData = key.UTF8String;
67 | const char *strData = self.UTF8String;
68 | uint8_t buffer[CC_SHA256_DIGEST_LENGTH];
69 |
70 | CCHmac(kCCHmacAlgSHA256, keyData, strlen(keyData), strData, strlen(strData), buffer);
71 |
72 | return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH];
73 | }
74 |
75 | - (NSString *)hmacSHA512StringWithKey:(NSString *)key {
76 | const char *keyData = key.UTF8String;
77 | const char *strData = self.UTF8String;
78 | uint8_t buffer[CC_SHA512_DIGEST_LENGTH];
79 |
80 | CCHmac(kCCHmacAlgSHA512, keyData, strlen(keyData), strData, strlen(strData), buffer);
81 |
82 | return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH];
83 | }
84 |
85 | #pragma mark - 文件散列函数
86 |
87 | #define FileHashDefaultChunkSizeForReadingData 4096
88 |
89 | - (NSString *)fileMD5Hash {
90 | NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self];
91 | if (fp == nil) {
92 | return nil;
93 | }
94 |
95 | CC_MD5_CTX hashCtx;
96 | CC_MD5_Init(&hashCtx);
97 |
98 | while (YES) {
99 | @autoreleasepool {
100 | NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData];
101 |
102 | CC_MD5_Update(&hashCtx, data.bytes, (CC_LONG)data.length);
103 |
104 | if (data.length == 0) {
105 | break;
106 | }
107 | }
108 | }
109 | [fp closeFile];
110 |
111 | uint8_t buffer[CC_MD5_DIGEST_LENGTH];
112 | CC_MD5_Final(buffer, &hashCtx);
113 |
114 | return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH];
115 | }
116 |
117 | - (NSString *)fileSHA1Hash {
118 | NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self];
119 | if (fp == nil) {
120 | return nil;
121 | }
122 |
123 | CC_SHA1_CTX hashCtx;
124 | CC_SHA1_Init(&hashCtx);
125 |
126 | while (YES) {
127 | @autoreleasepool {
128 | NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData];
129 |
130 | CC_SHA1_Update(&hashCtx, data.bytes, (CC_LONG)data.length);
131 |
132 | if (data.length == 0) {
133 | break;
134 | }
135 | }
136 | }
137 | [fp closeFile];
138 |
139 | uint8_t buffer[CC_SHA1_DIGEST_LENGTH];
140 | CC_SHA1_Final(buffer, &hashCtx);
141 |
142 | return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH];
143 | }
144 |
145 | - (NSString *)fileSHA256Hash {
146 | NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self];
147 | if (fp == nil) {
148 | return nil;
149 | }
150 |
151 | CC_SHA256_CTX hashCtx;
152 | CC_SHA256_Init(&hashCtx);
153 |
154 | while (YES) {
155 | @autoreleasepool {
156 | NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData];
157 |
158 | CC_SHA256_Update(&hashCtx, data.bytes, (CC_LONG)data.length);
159 |
160 | if (data.length == 0) {
161 | break;
162 | }
163 | }
164 | }
165 | [fp closeFile];
166 |
167 | uint8_t buffer[CC_SHA256_DIGEST_LENGTH];
168 | CC_SHA256_Final(buffer, &hashCtx);
169 |
170 | return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH];
171 | }
172 |
173 | - (NSString *)fileSHA512Hash {
174 | NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self];
175 | if (fp == nil) {
176 | return nil;
177 | }
178 |
179 | CC_SHA512_CTX hashCtx;
180 | CC_SHA512_Init(&hashCtx);
181 |
182 | while (YES) {
183 | @autoreleasepool {
184 | NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData];
185 |
186 | CC_SHA512_Update(&hashCtx, data.bytes, (CC_LONG)data.length);
187 |
188 | if (data.length == 0) {
189 | break;
190 | }
191 | }
192 | }
193 | [fp closeFile];
194 |
195 | uint8_t buffer[CC_SHA512_DIGEST_LENGTH];
196 | CC_SHA512_Final(buffer, &hashCtx);
197 |
198 | return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH];
199 | }
200 |
201 | #pragma mark - 助手方法
202 | /**
203 | * 返回二进制 Bytes 流的字符串表示形式
204 | *
205 | * @param bytes 二进制 Bytes 数组
206 | * @param length 数组长度
207 | *
208 | * @return 字符串表示形式
209 | */
210 | - (NSString *)stringFromBytes:(uint8_t *)bytes length:(int)length {
211 | NSMutableString *strM = [NSMutableString string];
212 |
213 | for (int i = 0; i < length; i++) {
214 | [strM appendFormat:@"%02x", bytes[i]];
215 | }
216 |
217 | return [strM copy];
218 | }
219 |
220 | @end
221 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadCell.h:
--------------------------------------------------------------------------------
1 | //
2 | // TZHDownloadCell.h
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 | #import
9 | #import "TZHFilesDownloadModel.h"
10 |
11 | @interface TZHDownloadCell : UITableViewCell
12 |
13 | @property(nonatomic,strong)TZHFilesDownloadModel *model;
14 | @property (weak, nonatomic) IBOutlet UIProgressView *DownloadProgress;
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadCell.m:
--------------------------------------------------------------------------------
1 | //
2 | // TZHDownloadCell.m
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import "TZHDownloadCell.h"
10 | @interface TZHDownloadCell()
11 |
12 | @property (weak, nonatomic) IBOutlet UILabel *FileNameLabel;
13 |
14 | @end
15 |
16 | @implementation TZHDownloadCell
17 |
18 | - (void)awakeFromNib {
19 | [super awakeFromNib];
20 | // Initialization code
21 | }
22 |
23 | -(void)setModel:(TZHFilesDownloadModel *)model{
24 |
25 | _model = model;
26 | self.FileNameLabel.text = model.FileName;
27 | self.DownloadProgress.progress = model.progress;
28 | }
29 |
30 | @end
31 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadManager.h:
--------------------------------------------------------------------------------
1 | //
2 | // TZHDownloadManager.h
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface TZHDownloadManager : NSObject
12 |
13 | @property(nonatomic,strong)NSString *fileName;
14 |
15 | +(instancetype)shared;
16 |
17 | //异步下载的方法 进度的block
18 | -(void)downloadAudioWithURL:(NSURL *)url andFormat:(NSString *)format progress:(void(^)(float progress))progressBlock complete:(void(^)(NSString *fileSavePath,NSError *error))completeBlock;
19 |
20 | //判断是否正在下载
21 | -(BOOL)isDownloadingAudioWithURL:(NSURL *)url;
22 |
23 |
24 | //取消下载
25 | - (void)cancelDownloadingAudioWithURL:(NSURL *)url andFormat:(NSString *)format complete:(void (^)())completeBlock;
26 |
27 |
28 | //显示文件占内存大小
29 | + (NSString *)getFileCacheSize;
30 | //删除文件
31 |
32 | +(void)deleteFileFromCache;
33 |
34 | @end
35 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadManager.m:
--------------------------------------------------------------------------------
1 | //
2 | // TZHDownloadManager.m
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import "TZHDownloadManager.h"
10 | #import "NSString+Hash.h"
11 |
12 | @interface TZHDownloadManager ()
13 |
14 | @property (nonatomic, strong) NSURLSession *session;
15 | @property(nonatomic,strong)NSString *fileForm;
16 |
17 | @end
18 | @implementation TZHDownloadManager{
19 | //保存下载任务对应的进度block 和 完成的block
20 | NSMutableDictionary *_progressBlocks;
21 | NSMutableDictionary *_completeBlocks;
22 | NSMutableDictionary *_downloadTasks;
23 | }
24 |
25 | static id _instance;
26 |
27 | +(instancetype)shared{
28 |
29 | static dispatch_once_t onceToken;
30 |
31 | dispatch_once(&onceToken, ^{
32 | _instance = [[self alloc] init];
33 | });
34 | return _instance;
35 | }
36 |
37 |
38 | - (instancetype)init {
39 | self = [super init];
40 |
41 | if (self) {
42 | _progressBlocks = [NSMutableDictionary dictionary];
43 | _completeBlocks = [NSMutableDictionary dictionary];
44 | _downloadTasks = [NSMutableDictionary dictionary];
45 | }
46 | return self;
47 | }
48 |
49 | - (NSURLSession *)session {
50 | if (!_session) {
51 | NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
52 |
53 | _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
54 | }
55 | return _session;
56 | }
57 |
58 | - (BOOL)isDownloadingAudioWithURL:(NSURL *)url {
59 |
60 | if (_completeBlocks[url]) {
61 | return YES;
62 | }
63 | return NO;
64 | }
65 |
66 | - (void)cancelDownloadingAudioWithURL:(NSURL *)url andFormat:(NSString *)format complete:(void (^)())completeBlock {
67 |
68 | NSURLSessionDownloadTask *currentTask = _downloadTasks[url];
69 |
70 | // 2.cancel
71 | if (currentTask) {
72 |
73 | [currentTask cancelByProducingResumeData:^(NSData *_Nullable resumeData) {
74 |
75 | [resumeData writeToFile:[self getResumeDataPathWithURL:url andFormat:format] atomically:YES];
76 |
77 | //把取消成功的结果返回
78 | if (completeBlock) {
79 | completeBlock();
80 | }
81 |
82 | _progressBlocks[url] = nil;
83 | _completeBlocks[url] = nil;
84 | _downloadTasks[url] = nil;
85 |
86 | }];
87 | }
88 | }
89 |
90 | //入口
91 | - (void)downloadAudioWithURL:(NSURL *)url andFormat:(NSString *)format progress:(void (^)(float progress))progressBlock complete:(void (^)(NSString *fileSavePath, NSError *error))completeBlock {
92 |
93 | NSFileManager *fileMan = [NSFileManager defaultManager];
94 |
95 | NSLog(@"下载工具中打印格式%@",format);
96 | _fileForm = format;
97 | NSString *fileSavePath = [self getFileSavePathWithURL:url andFormat:format];
98 | if ([fileMan fileExistsAtPath:fileSavePath]) {
99 | NSLog(@"文件已经存在");
100 | if (completeBlock) {
101 | completeBlock(fileSavePath, nil);
102 | }
103 | return;
104 | }
105 |
106 |
107 | if ([self isDownloadingAudioWithURL:url]) {
108 | NSLog(@"正在下载");
109 | return;
110 | }
111 |
112 |
113 |
114 | [_progressBlocks setObject:progressBlock forKey:url];
115 | [_completeBlocks setObject:completeBlock forKey:url];
116 |
117 |
118 | NSString *resumeDataPath = [self getResumeDataPathWithURL:url andFormat:format];
119 |
120 | NSURLSessionDownloadTask *downloadTask;
121 | if ([fileMan fileExistsAtPath:resumeDataPath]) {
122 |
123 | NSData *resumeData = [NSData dataWithContentsOfFile:resumeDataPath];
124 | downloadTask = [self.session downloadTaskWithResumeData:resumeData];
125 | } else {
126 | downloadTask = [self.session downloadTaskWithURL:url];
127 | }
128 |
129 | [_downloadTasks setObject:downloadTask forKey:url];
130 | //开启
131 | [downloadTask resume];
132 | }
133 |
134 |
135 | - (NSString *)getResumeDataPathWithURL:(NSURL *)url andFormat:(NSString *)format{
136 | NSString *tmpPath = NSTemporaryDirectory();
137 |
138 | NSString *fileName = [NSString stringWithFormat:@"%@%@",[url.absoluteString md5String],format];
139 | return [tmpPath stringByAppendingPathComponent:fileName];
140 | }
141 |
142 |
143 | - (NSString *)getFileSavePathWithURL:(NSURL *)url andFormat:(NSString *)format{
144 |
145 | NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
146 |
147 | NSFileManager *fileManager = [NSFileManager defaultManager];
148 | NSString *TZHCachePath = [cachePath stringByAppendingPathComponent:@"TZHDownloadFile"];
149 | [fileManager createDirectoryAtPath:TZHCachePath withIntermediateDirectories:YES attributes:nil error:nil];
150 |
151 | NSString *fileName = [NSString stringWithFormat:@"%@%@",[url.absoluteString md5String],format];
152 | _fileName = fileName;
153 | return [TZHCachePath stringByAppendingPathComponent:fileName];
154 | }
155 |
156 | #pragma mark sessionDelegate
157 |
158 | - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
159 |
160 | NSFileManager *fileMan = [NSFileManager defaultManager];
161 |
162 | NSURL *currentURL = downloadTask.currentRequest.URL;
163 | [fileMan copyItemAtPath:location.path toPath:[self getFileSavePathWithURL:currentURL andFormat:_fileForm] error:NULL];
164 |
165 |
166 | if (_completeBlocks[currentURL]) {
167 | void (^tmpCompBlock)(NSString *filePath, NSError *error) = _completeBlocks[currentURL];
168 | tmpCompBlock([self getFileSavePathWithURL:currentURL andFormat:_fileForm], nil);
169 | }
170 |
171 |
172 | _progressBlocks[currentURL] = nil;
173 | _completeBlocks[currentURL] = nil;
174 | _downloadTasks[currentURL] = nil;
175 | }
176 |
177 | - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
178 |
179 | float progress = totalBytesWritten * 1.0 / totalBytesExpectedToWrite;
180 |
181 |
182 | NSURL *url = downloadTask.currentRequest.URL;
183 | if (_progressBlocks[url]) {
184 |
185 | void (^tmpProBlock)(float) = _progressBlocks[url];
186 | tmpProBlock(progress);
187 | }
188 | }
189 |
190 | + (NSString *)getFileCacheSize{
191 |
192 | NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
193 |
194 | NSString *TZHCachePath = [cachePath stringByAppendingPathComponent:@"TZHDownloadFile"];
195 |
196 | NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:TZHCachePath];
197 |
198 | NSString *filePath = nil;
199 | NSInteger totleSize = 0;
200 |
201 | for (NSString *subPath in subPathArr){
202 |
203 | filePath =[TZHCachePath stringByAppendingPathComponent:subPath];
204 |
205 | BOOL isDirectory = NO;
206 |
207 | BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
208 |
209 | if (!isExist || isDirectory || [filePath containsString:@".DS"]){
210 |
211 | continue;
212 | }
213 |
214 | NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
215 |
216 | NSInteger size = [dict[@"NSFileSize"] integerValue];
217 |
218 |
219 | totleSize += size;
220 | }
221 |
222 |
223 | NSString *totleStr = nil;
224 |
225 | if (totleSize > 1000 * 1000){
226 | totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f];
227 |
228 | }else if (totleSize > 1000){
229 | totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ];
230 |
231 | }else{
232 | totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f];
233 | }
234 |
235 | return totleStr;
236 |
237 | }
238 |
239 | +(void)deleteFileFromCache{
240 |
241 | NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
242 | NSString *TZHCachePath = [cachePath stringByAppendingPathComponent:@"TZHDownloadFile"];
243 |
244 | NSFileManager *fileManager = [NSFileManager defaultManager];
245 | NSArray *array = [fileManager contentsOfDirectoryAtPath:TZHCachePath error:nil];
246 |
247 | for(NSString *fileName in array){
248 |
249 | [fileManager removeItemAtPath:[TZHCachePath stringByAppendingPathComponent:fileName] error:nil];
250 | }
251 |
252 |
253 | }
254 | @end
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // TZHDownloadViewController.m
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface TZHDownloadViewController : UIViewController
12 |
13 | @property(nonatomic,strong)NSArray *filesArray;
14 |
15 | @end
16 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // TZHDownloadViewController.m
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import "TZHDownloadViewController.h"
10 | #import "TZHFilesDownloadModel.h"
11 | #import "TZHDownloadCell.h"
12 | #import "TZHDownloadManager.h"
13 | #import "TZHLoadDownloadFileVC.h"
14 |
15 | @interface TZHDownloadViewController ()
16 |
17 | @property(nonatomic,weak)UITableView *tableView;
18 |
19 | @end
20 | static NSString *cellID = @"cellId";
21 |
22 | @implementation TZHDownloadViewController
23 |
24 | - (void)viewDidLoad {
25 | [super viewDidLoad];
26 |
27 | UIButton *back=[UIButton buttonWithType:UIButtonTypeSystem];
28 | back.contentEdgeInsets = UIEdgeInsetsMake(0,-3*10, 0, 0);
29 |
30 | UIImage *backImage = [UIImage imageNamed:[@"TZHFileManager.bundle" stringByAppendingPathComponent:@"返回"]];
31 | [back setImage:backImage forState:UIControlStateNormal];
32 | back.titleLabel.font = [UIFont systemFontOfSize:17];
33 | [back setTitle:@"返回" forState:UIControlStateNormal];
34 | back.frame=CGRectMake(10,10,65,50);
35 | [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
36 | UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:back];
37 | self.navigationItem.leftBarButtonItem=item;
38 | self.title = @"附件列表";
39 |
40 | UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
41 | [self.view addSubview:tableView];
42 | tableView.delegate = self;
43 | tableView.dataSource = self;
44 | [tableView registerNib:[UINib nibWithNibName:@"FileDownloadCell" bundle:nil] forCellReuseIdentifier:cellID];
45 | _tableView = tableView;
46 | tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
47 | tableView.estimatedRowHeight = 250;
48 | }
49 |
50 | -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
51 |
52 | return self.filesArray.count;
53 | }
54 |
55 | -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
56 |
57 | TZHDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
58 |
59 | TZHFilesDownloadModel *model = self.filesArray[indexPath.row];
60 | cell.model = model;
61 |
62 | cell.selectionStyle = UITableViewCellSelectionStyleNone;
63 | return cell;
64 | }
65 |
66 | -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
67 |
68 | TZHFilesDownloadModel *model = self.filesArray[indexPath.row];
69 |
70 | NSString *encodedString = (NSString *)
71 | CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
72 | (CFStringRef)model.FileURL,
73 | (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
74 | NULL,
75 | kCFStringEncodingUTF8));
76 |
77 | NSURL *url = [NSURL URLWithString:encodedString];
78 |
79 | NSLog(@"打印打印 %@",model.FileURL);
80 |
81 | if([[TZHDownloadManager shared] isDownloadingAudioWithURL:url] ){
82 |
83 | }else{
84 | //下载代码
85 | NSString *format = model.FileName;
86 | NSRange range = [format rangeOfString:@"."];
87 | format = [format substringFromIndex:range.location];
88 |
89 | [[TZHDownloadManager shared] downloadAudioWithURL:url andFormat:format progress:^(float progress) {
90 | TZHDownloadCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
91 | cell.DownloadProgress.hidden = NO;
92 |
93 | model.progress = progress;
94 | cell.model = model;
95 | if(progress == 1){
96 | cell.DownloadProgress.hidden = YES;
97 |
98 | }
99 |
100 | } complete:^(NSString *fileSavePath, NSError *error) {
101 |
102 | NSLog(@"打印存储地址%@",fileSavePath);
103 | TZHLoadDownloadFileVC *webVC = [[TZHLoadDownloadFileVC alloc]init];
104 |
105 | [webVC loadLocalFileWebView:[TZHDownloadManager shared].fileName];
106 | webVC.hidesBottomBarWhenPushed = YES;
107 | [self.navigationController pushViewController:webVC animated:YES];
108 | }];
109 | }
110 |
111 | }
112 |
113 | -(void)back{
114 | [self.navigationController popViewControllerAnimated:YES];
115 | }
116 |
117 |
118 |
119 | @end
120 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileDownload.h:
--------------------------------------------------------------------------------
1 | //
2 | // TZHFileDownload.h
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #ifndef TZHFileDownload_h
10 | #define TZHFileDownload_h
11 |
12 |
13 | #endif /* TZHFileDownload_h */
14 | #import "TZHFilesDownloadModel.h"
15 | #import "TZHDownloadViewController.h"
16 | #import "TZHDownloadManager.h"
17 | #import "TZHDownloadCell.h"
18 | #import "TZHDownloadViewController.h"
19 |
20 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/Root.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | StringsTable
6 | Root
7 | PreferenceSpecifiers
8 |
9 |
10 | Type
11 | PSGroupSpecifier
12 | Title
13 | Group
14 |
15 |
16 | Type
17 | PSTextFieldSpecifier
18 | Title
19 | Name
20 | Key
21 | name_preference
22 | DefaultValue
23 |
24 | IsSecure
25 |
26 | KeyboardType
27 | Alphabet
28 | AutocapitalizationType
29 | None
30 | AutocorrectionType
31 | No
32 |
33 |
34 | Type
35 | PSToggleSwitchSpecifier
36 | Title
37 | Enabled
38 | Key
39 | enabled_preference
40 | DefaultValue
41 |
42 |
43 |
44 | Type
45 | PSSliderSpecifier
46 | Key
47 | slider_preference
48 | DefaultValue
49 | 0.5
50 | MinimumValue
51 | 0
52 | MaximumValue
53 | 1
54 | MinimumValueImage
55 |
56 | MaximumValueImage
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/en.lproj/Root.strings:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/en.lproj/Root.strings
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/返回@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/返回@2x.png
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFilesDownloadModel.h:
--------------------------------------------------------------------------------
1 | //
2 | // TZHFilesDownloadModel.h
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface TZHFilesDownloadModel : NSObject
12 |
13 | @property(nonatomic,strong)NSString *FileName;
14 | @property(nonatomic,strong)NSString *FileURL;
15 | @property(nonatomic,assign)float progress;
16 |
17 |
18 | @end
19 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFilesDownloadModel.m:
--------------------------------------------------------------------------------
1 | //
2 | // TZHFilesDownloadModel.m
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import "TZHFilesDownloadModel.h"
10 |
11 | @implementation TZHFilesDownloadModel
12 |
13 |
14 |
15 | @end
16 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHLoadDownloadFileVC.h:
--------------------------------------------------------------------------------
1 | //
2 | // TZHLoadDownloadFileVC.h
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface TZHLoadDownloadFileVC : UIViewController
12 |
13 | - (void)loadLocalFileWebView:(NSString *)fileName;
14 |
15 | @end
16 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHLoadDownloadFileVC.m:
--------------------------------------------------------------------------------
1 | //
2 | // TZHLoadDownloadFileVC.m
3 | // 陶增辉文件下载预览
4 | //
5 | // Created by taozenghui on 2017/6/28.
6 | // Copyright © 2017年 taozenghui. All rights reserved.
7 | //
8 |
9 | #import "TZHLoadDownloadFileVC.h"
10 |
11 | #define SWIDTH self.view.frame.size.width //屏幕宽度
12 | #define SHEIGHT self.view.frame.size.height //屏幕高度
13 |
14 |
15 | @interface TZHLoadDownloadFileVC ()
16 |
17 | @property(strong, nonatomic)UIWebView *loadWebView;
18 |
19 | @end
20 |
21 | @implementation TZHLoadDownloadFileVC
22 |
23 | - (void)viewDidLoad {
24 | [super viewDidLoad];
25 |
26 | self.title = @"文件详情";
27 | UIButton *back=[UIButton buttonWithType:UIButtonTypeSystem];
28 | back.contentEdgeInsets = UIEdgeInsetsMake(0,-3*10, 0, 0);
29 |
30 | UIImage *backImage = [UIImage imageNamed:[@"TZHFileManager.bundle" stringByAppendingPathComponent:@"返回"]];
31 | [back setImage:backImage forState:UIControlStateNormal];
32 | back.titleLabel.font = [UIFont systemFontOfSize:17];
33 | [back setTitle:@"返回" forState:UIControlStateNormal];
34 | back.frame=CGRectMake(10,10,65,50);
35 | [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
36 | UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:back];
37 | self.navigationItem.leftBarButtonItem=item;
38 |
39 | self.view.backgroundColor = [UIColor whiteColor];
40 | self.automaticallyAdjustsScrollViewInsets = NO;
41 | self.loadWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, SWIDTH, SHEIGHT - 64)];
42 | _loadWebView.backgroundColor = [UIColor whiteColor];
43 | _loadWebView.delegate = self;
44 | _loadWebView.scalesPageToFit = YES;
45 | }
46 |
47 | - (void)loadLocalFileWebView:(NSString *)fileName{
48 |
49 | [self.view addSubview:_loadWebView];
50 |
51 | NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
52 |
53 | NSString *TZHCachePath = [cachePath stringByAppendingPathComponent:@"TZHDownloadFile"];
54 |
55 | NSString *filePath = [TZHCachePath stringByAppendingPathComponent:fileName];
56 | NSURL *url = [NSURL fileURLWithPath:filePath];
57 |
58 | NSURLRequest *request = [NSURLRequest requestWithURL:url];
59 | [_loadWebView loadRequest:request];
60 | }
61 |
62 | -(void)back{
63 | [self.navigationController popViewControllerAnimated:YES];
64 | }
65 | @end
66 |
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/screenshots/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/screenshots/.DS_Store
--------------------------------------------------------------------------------
/TZHFileManagerDemo/TZHFileManagerDemo/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // TZHFileManagerDemo
4 | //
5 | // Created by swkj on 2017/6/30.
6 | // Copyright © 2017年 thirdnet. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------