压缩使用SSZipArchive
默认压缩加密,密码为生成的时间格式
主要代码:
单个文件或文件夹使用,传入@"Library/Caches/WLogs"或@"Library/Caches/WLogs/xxxx.log"
+ (nullable NSString *)zipLogWithFilePath:(nullable NSString *)filePath
多个文件或文件夹使用:传入[@"Documents/WLogs", @"Library/Caches/WLogs", @"Library/Caches/WLogs/xxxx.log"]
+ (nullable NSString *)zipLogWithFilePath:(nonnull NSArray *)filePaths
头文件:WLLogUploadManager.h
#import
NS_ASSUME_NONNULL_BEGIN
@interface WLLogUploadManager : NSObject
/// 上传zip文件
/// @param filePath 日志文件(夹)路径:nil上传自己写入的日志
+ (void)uploadFileWithPath:(nullable NSString *)filePath;
@end
NS_ASSUME_NONNULL_END
实现文件:WLLogUploadManager.m
#import "WLLogUploadManager.h"
#import //【压缩】
@implementation WLLogUploadManager
#pragma mark - 上传
/// 上传zip文件
/// @param filePath 日志文件(夹)路径:nil上传自己写入的日志
+ (void)uploadFileWithPath:(nullable NSString *)filePath {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *zipPath = nil;
//不存在,默认上传自己打印的日志
if (!filePath || ![filePath isKindOfClass:NSString.class] || filePath.length <= 0) {
zipPath = self.zipMyLog;
} else {
zipPath = [self zipLogWithFilePath:filePath];
}
if (zipPath) {
long long fileSize = [self fileSizeAtPath:zipPath]; //计算文件大小
NSLog(@"[uploadLog] uploadLogWithPath fileSize = [%lld]", fileSize);
//【上传文件】
[[WLUploadManager shareInstance] uploadFileWithPath:zipPath finishBlock:^(NSError *error, NSString *url) {
NSLog(@"[uploadLog] uploadLogWithPath finish url = [%@], error = [%@]", url, error);
NSFileManager *fileManager = NSFileManager.defaultManager;
if ([fileManager fileExistsAtPath:zipPath]) {
[fileManager removeItemAtPath:zipPath error:nil];
}
}];
}
});
}
#pragma mark - 压缩
/// 压缩自己打印的日志
+ (nullable NSString *)zipMyLog {
NSString *sourceFilePath = @"Library/Caches/WLogs"; //WLogs日志文件夹
NSString *destinationPath = [self zipLogWithFilePath:sourceFilePath];
return destinationPath;
}
/// 根据文件(夹)路径压缩成zip文件
/// @param filePath 文件(夹)路径:@"Library/Caches/WLogs"或@"Library/Caches/WLogs/xxxx.log"
/// @return 返回压缩成功的zip路径:系统目录/tmp/xxx 或为 nil
+ (nullable NSString *)zipLogWithFilePath:(nullable NSString *)filePath {
NSLog(@"[uploadLog] zipLogWithFilePath = [%@]", filePath);
NSString *destinationPath = nil;
if (!filePath || ![filePaths isKindOfClass:NSString.class] || filePath.length == 0) {
return destinationPath;
}
NSString *homeDirectory = NSHomeDirectory();
NSString *sourceFilePath = [homeDirectory stringByAppendingPathComponent:filePath];
NSLog(@"[uploadLog] zipLogWithFilePath sourceFilePath = [%@]", sourceFilePath);
BOOL isDirectory = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath:sourceFilePath isDirectory:&isDirectory]) {
NSString *tempDirectory = NSTemporaryDirectory(); //压缩日志存放Temp目录下
NSString *dateString = [self getCurrentTimeString]; //格式当前时间
destinationPath = [tempDirectory stringByAppendingFormat:@"iOS_%@.zip", dateString]; //最终压缩文件,tmp/iOS_当前时间戳.zip
BOOL success = NO;
if (isDirectory) {
success = [SSZipArchive createZipFileAtPath:destinationPath withContentsOfDirectory:sourceFilePath withPassword:dateString]; //【压缩】文件夹
} else {
success = [SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[sourceFilePath] withPassword:dateString]; //【压缩】文件
}
if (!success) {
destinationPath = nil;
}
}
NSLog(@"[uploadLog] zipLogWithFilePath destinationPath = [%@]", destinationPath);
return destinationPath;
}
/// 根据文件(夹)路径压缩成zip文件
/// @param filePaths 多个文件(夹)路径:[@"Documents/WLogs", @"Library/Caches/WLogs", @"Library/Caches/WLogs/xxxx.log"]
/// @return 返回压缩成功的zip路径:系统目录/tmp/xxx 或为 nil(不会删除压缩好的zip)
+ (nullable NSString *)zipLogWithFilePath:(nonnull NSArray *)filePaths {
NSLog(@"[uploadLog] 开始 zipLogWithFilePath = [%@]", filePaths);
NSString *destinationPath = nil;
if (!filePaths || ![filePaths isKindOfClass:NSArray.class] || filePaths.count == 0) {
return destinationPath;
}
NSString *tempDirectory = NSTemporaryDirectory(); //压缩日志存放Temp目录下
//NSString *dateString = [self getCurrentTimeString]; //格式当前时间
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
destinationPath = [tempDirectory stringByAppendingFormat:@"iOS_%@.zip", dateString]; //最终压缩文件成品,tmp/iOS_当前时间戳.zip
NSString *zipPath = [tempDirectory stringByAppendingPathComponent:dateString]; //临时文件,压缩此文件的内容
NSLog(@"[uploadLog] zipPath = [%@]", zipPath);
NSFileManager *fileManager = [NSFileManager defaultManager];
//创建临时文件
BOOL isExistDic = [fileManager fileExistsAtPath:zipPath];
if (!isExistDic){
[fileManager createDirectoryAtPath:zipPath withIntermediateDirectories:YES attributes:nil error:nil];
}
BOOL needZip = NO;
for (NSString *filePath in filePaths) {
if (filePath && [filePath isKindOfClass:NSString.class]) {
NSString *sourceFilePath = [NSHomeDirectory() stringByAppendingPathComponent:filePath];
NSLog(@"[uploadLog] sourceFilePath = [%@]", sourceFilePath);
if ([fileManager fileExistsAtPath:sourceFilePath isDirectory:nil]) {
NSString *toPath = [zipPath stringByAppendingPathComponent:filePath.lastPathComponent];
NSLog(@"[uploadLog] copy toPath = [%@]", toPath);
if ([fileManager copyItemAtPath:sourceFilePath toPath:toPath error:nil]) {
needZip = YES;
}
}
}
}
NSLog(@"[uploadLog] zipLogWithFilePath needZip=[%d] zipPath = [%@]", needZip, zipPath);
BOOL success = NO;
if (needZip) {
success = [PASCSSZipArchive createZipFileAtPath:destinationPath withContentsOfDirectory:zipPath withPassword:dateString]; //【压缩】文件夹
//删除临时文件
if ([fileManager fileExistsAtPath:zipPath]) {
[fileManager removeItemAtPath:zipPath error:nil];
}
}
if (!success) {
destinationPath = nil;
}
NSLog(@"[uploadLog] 结束 zipLogWithFilePath destinationPath = [%@]", destinationPath);
return destinationPath;
}
#pragma mark - 其他
/// 计算文件大小
/// @param filePath 文件路径
/// @return 返回大小或0,单位:字节
+ (long long)fileSizeAtPath:(nullable NSString *)filePath {
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
/// 获取当前时间
/// @return 返回格式为yyyyMMddHHmmss的字符串
+ (nonnull NSString *)getCurrentTimeString {
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
return dateString;
}
@end