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.整型除以整型得到的是?