用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;
}

這個斷點續傳是鄙人的個人理解,如果有不正確的地方請網友幫忙指出,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章