用NSFileHandle实现文件的断点续传

断点续传:复制或者下载一个文件到某一个位置停止,下次接着原来的部分下载


//复制一个文件中的指定的内容到另外一个文件中
void copyFileContentToAnotherFileUnderControl(){
    NSString *homePath = NSHomeDirectory();

    NSString *srcPath = [homePath stringByAppendingPathComponent:@"src.txt"];
    NSString *descPath = [homePath stringByAppendingPathComponent:@"desc.txt"];

    NSString *srcContent = @"abcdefg hijklmn opq rst uvw xyz a!";
    NSData *srcData = [srcContent dataUsingEncoding:NSUTF8StringEncoding];

    //创建文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];

    //创建原文件以及添加文件中的内容
    BOOL srcSuccess = [fileManager createFileAtPath:srcPath contents:srcData attributes:nil];
    if (srcSuccess) {
        NSLog(@"Source File Create Success!");
    } else {
        NSLog(@"Source File Create Failure!");
    }

    //创建目标文件
    BOOL success = [fileManager createFileAtPath:descPath contents:nil attributes:nil];
    if (success) {
        NSLog(@"Desert File Create Success!");
    } else {
        NSLog(@"Desert File Create Failure!");
    }
    //打开原文件和目标文件
    NSFileHandle *srcHandle = [NSFileHandle fileHandleForReadingAtPath:srcPath];
    NSFileHandle *descHandle = [NSFileHandle fileHandleForUpdatingAtPath:descPath];

    //获取全部内容的长度
    //NSUInteger length = [srcHandle availableData].length;

    //读取并且传递指定长度的内容
    NSData *content = [srcHandle readDataOfLength:5];
    [descHandle writeData:content];

    [srcHandle closeFile];
    [descHandle closeFile];
}

//实现断点续传
/**
 原理:
 1、比较目标文件和原文件的大小,如果目标文件比原文件小,则需要继续复制文件中的内容
 2、读取原文件中剩余的内容,传入复制到目标文件的最后
 */
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *homePath = NSHomeDirectory();

        NSString *srcPath = [homePath stringByAppendingPathComponent:@"src.txt"];
        NSString *descPath = [homePath stringByAppendingPathComponent:@"desc.txt"];

        //打开原文件和目标文件
        NSFileHandle *srcHandle = nil;
        NSFileHandle *descHandle = nil;
        srcHandle = [NSFileHandle fileHandleForReadingAtPath:srcPath];
        descHandle = [NSFileHandle fileHandleForReadingAtPath:descPath];

        if (descHandle) {//如果有目标文件,则复制剩下的内容
            NSLog(@"---目标文件已经生成过---");
            //获取原文件内容的长度
            NSUInteger srcLength = [srcHandle availableData].length;
            //获取目标文件内容的长度
            NSUInteger descLength = [descHandle availableData].length;
            if (descLength < srcLength) {//如果目标文件比原文件内容少,则说明还没有复制完成,继续进行复制操作
                //由打开供读取变成打开供更新
                descHandle = [NSFileHandle fileHandleForUpdatingAtPath:descPath];
                //设置要读取的偏移量为目标文件的长度,就是要从什么位置开始读取
                [srcHandle seekToFileOffset:descLength];
                //读取文件的内容(因为前面设置好了偏移量,因此这里读取数据的内容为偏移量之后到末尾的内容)
                NSData *remainContent = [srcHandle readDataToEndOfFile];

                //偏移量设置为目标文件的最后,要不然会覆盖原来的内容
                [descHandle seekToEndOfFile];
                //写数据
                [descHandle writeData:remainContent];

            }
        } else {//如果没有目标文件,则创建目标文件,并且复制指定的内容到里面去
            NSLog(@"===还没有生成目标文件===");

            //调用复制部分内容的方法
            copyFileContentToAnotherFileUnderControl();
        }

        [srcHandle closeFile];
        [descHandle closeFile];
    }
    return 0;
}

这个断点续传是鄙人的个人理解,如果有不正确的地方请网友帮忙指出,谢谢!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值