【Objective-c 学习笔记】大文件拷贝

CopyBigFile类:

#import <Foundation/Foundation.h>

@interface ZygCopyBigFile : NSObject

@property (nonatomic,assign) float rate;
@property (nonatomic,assign) int copyLength;

- (id)initWithCopyLength:(int)length;
- (BOOL)copyFile:(NSString *)sourceFile toPath:(NSString *)targetFile;

//private
//- (void)copyErrorLog:(NSString *)errorStr;
//- (void)setRateWithInt:(int)a andInt:(int)b;

@end

#import "ZygCopyBigFile.h"


@implementation ZygCopyBigFile

@synthesize rate,copyLength;

- (id)initWithCopyLength:(int)length
{
    if (self = [super init]) {
        self.rate = 0.00f;
        self.copyLength = length;
    }
    return self;
}


- (BOOL)copyFile:(NSString *)sourceFile toPath:(NSString *)targetFile
{
    //file is existed?
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:sourceFile]) {
        [self copyErrorLog:@"sourceFile is not existed! Please checked first"];
        return NO;
    }
    if (![fm fileExistsAtPath:targetFile]) {
        BOOL y = [fm createFileAtPath:targetFile contents:nil attributes:nil];
        if (!y) {
            [self copyErrorLog:@"create targetFile error"];
            return NO;
        }
    } 
    
    
    @try {
        NSDictionary *sourceFileInfo = [fm attributesOfItemAtPath:sourceFile error:nil];
        NSNumber *sourceFileLength = [sourceFileInfo objectForKey:NSFileSize];
        
        NSFileHandle *sourceHandle = [NSFileHandle fileHandleForReadingAtPath:sourceFile];
        NSFileHandle *targetHandle = [NSFileHandle fileHandleForWritingAtPath:targetFile];
        [targetHandle truncateFileAtOffset:0];//truncate targetfile for init it
        
        BOOL isEnd = YES;
        int totalLength = [sourceFileLength intValue];
        int readLength = 0;
        while (isEnd) {
            NSData *tmpData = nil;
            int l = totalLength - readLength;
          
            if (l < self.copyLength) {
                tmpData = [sourceHandle readDataToEndOfFile];
                readLength = totalLength;
                isEnd = NO;
            } else {
                tmpData = [sourceHandle readDataOfLength:copyLength];
                readLength += self.copyLength;
                [sourceHandle seekToFileOffset:readLength];
            }
            [targetHandle seekToEndOfFile];
            [targetHandle writeData:tmpData];
            
            [self setRateWithInt:readLength andInt:totalLength];
        }
        
        [sourceHandle closeFile];
        [sourceHandle closeFile];

    }
    @catch (NSException *exception) {
        NSString *err = [NSString stringWithFormat:@"error:%@", [exception reason]];
        [self copyErrorLog:err];
        return NO;
    }
    @finally {}
    
    return YES;
}

- (void)copyErrorLog:(NSString *)errorStr
{
    NSLog(@"%@", errorStr);
    return;
}

- (void)setRateWithInt:(int)a andInt:(int)b
{
    float aa = a;
    float bb = b;
    self.rate = aa / bb;
//    NSLog(@"a:%d b:%d rate:%.2f",a,b, self.rate);
}
@end

主函数:

#import <Foundation/Foundation.h>

#import "ZygCopyBigFile.h"
//#import "ZygObserver.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"bigfile.zip"];

        NSString *tarfile = [NSHomeDirectory() stringByAppendingPathComponent:@"targetBigfile.zip"];
        
        
        ZygCopyBigFile *copyFile = [[ZygCopyBigFile alloc] initWithCopyLength:1000000];
        
        //ZygObserver *observer = nil;
        //observer = [[ZygObserver alloc] initWithObject:copyFile];
        
        [copyFile copyFile:file toPath:tarfile];
    
    }
  
    return 0;
}


可选的一个观察者类:

#import <Foundation/Foundation.h>

@interface ZygObserver : NSObject

@property (nonatomic,retain) id theObject;

- (id)initWithObject:(id)anObject;

@end

#import "ZygObserver.h"

@implementation ZygObserver

@synthesize theObject;

- (id)initWithObject:(id)anObject
{
    if (self = [super init]) {
        self.theObject = anObject;
        [self.theObject addObserver:self 
                         forKeyPath:@"rate" 
                            options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
                            context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(void *)context 
{
    
    NSNumber *num = [change objectForKey:@"new"];
    float rate = [num floatValue];
    rate *= 100;
    NSString *persen = [NSString stringWithFormat:@"complate %.2f%@", rate, @"%"];

    NSLog(@"%@", persen);
}
- (void)dealloc
{
    [self.theObject removeObserver:self forKeyPath:@"rate"];
}
@end


PS:

1.objc的类型转换有空看一下。

2.整型除以整型得到的是?



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值