用NSFileHandle实现文件的断点续传
来源:互联网 发布:seo教程ppt 编辑:程序博客网 时间:2024/06/08 02:23
断点续传:复制或者下载一个文件到某一个位置停止,下次接着原来的部分下载//复制一个文件中的指定的内容到另外一个文件中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
- 用NSFileHandle实现文件的断点续传
- NSFileHandle简析以及实现超大文件的复制
- 用NSFileHandle实现每次定量读取一个大文件
- 用HttpListener实现文件断点续传
- 用HttpListener实现文件断点续传
- NSFileHandle 的文件操作使用
- NSFileHandle简单的读写文件
- 如何实现文件的断点续传,文件下载
- 用 Chukeh 结合 Delphi 实现大文件上传的断点续传
- 用NSFileHandle写文件,避免过大的内存开销
- java实现文件的断点续传的下载
- 使用HASH算法实现文件的断点续传
- OKHttp实现大文件的断点续传
- 用ASP实现断点续传下载远程文件
- Libcurl实现文件断点续传
- Java实现文件断点续传
- 断点续传实现文件下载
- HttpURLConnection实现文件断点续传
- 使用procdump工具监视已发布的应用程序,如崩溃产生dmp文件进行调试
- windows下,python调用dll例子,展示如何传递字节码流参数到dll接口
- Linux笔记(3)--查看文档的命令
- 解决在Opera浏览器中不能切换中文输入法
- 坐标地址互找
- 用NSFileHandle实现文件的断点续传
- ViewPager的实现过程及其多种方式的动画效果
- Gradle命令入门
- AsyncDisplayKit入门指南
- 摄像头相关知识1
- 国际音标学习心得分享
- DDoS攻防战 (一) : 概述
- 设计模式学习笔记--命令模式command pattern(Java版)
- JMX RMI 访问