断点续传
单独实现文件拷贝可以参考C&C++——大文件拷贝
C++实现
#define EACH_BLOCK (1024 * 8)
typedef long long ll;
void copyContinuation(const string& fileName) {
string copyFileName = fileName;
int pos = copyFileName.rfind(".");
copyFileName.insert(pos, ".bp");
ifstream ifs(fileName, ios::binary);
if (!ifs.is_open()) {
cout << "原文件打开失败" << endl;
return;
}
ifs.seekg(0, ios::end);
ll fileSize = ifs.tellg(); // 文件总大小
ifs.seekg(0, ios::beg);
ofstream ofs(copyFileName, ios::app | ios::binary); // 二进制打开
if (!ofs.is_open()) {
cout << "目标文件打开失败" << endl;
return;
}
ofs.seekp(0, ios::end);
ll copyFileSize = ofs.tellp();
cout << "copyFileSize" << copyFileSize << endl;
if (copyFileSize >= fileSize) {
cout << "拷贝文件已完成" << endl;
return;
}
ifs.seekg(copyFileSize, ios::beg); // 开始拷贝位置
ll cur = ifs.tellg();
char buffer[EACH_BLOCK] = { 0 };
while (!ifs.eof()) {
ifs.read(buffer, sizeof(buffer));
ll len = ifs.gcount();
ofs.write(buffer, len);
ofs.flush();
ll curSize = ofs.tellp();
cout << fixed << setprecision(2) << "文件总大小: " << ((double)fileSize / 1024 / 1024) \
<< "MB;已下载: " << ((double)curSize / 1024 / 1024) << "MB;当前进度: " \
<< ((double)curSize / fileSize) * 100 << "%" << endl;
}
ifs.close();
ofs.close();
}
C语言实现
void copyContinuation() {
FILE* fp = nullptr;
fopen_s(&fp, "D:\\software\\c++\\clion\\CLion-2022.1.2.tar.gz", "rb");
if (fp == nullptr) {
return;
}
// 取原文件的大小
fseek(fp, 0, SEEK_END);
long totalSize = ftell(fp);
rewind(fp);
FILE* destFp = nullptr;
fopen_s(&destFp, "D:\\software\\c++\\clion\\CLion-2022.1.2.bp.tar.gz", "ab+");
if (destFp == nullptr) {
return;
}
// 计算目标文件目前的大小
fseek(destFp, 0, SEEK_END);
long writeSize = ftell(destFp);
// 如果目标文件大于原文件,返回;否则,移动原文件光标移动目标文件大小。
if (totalSize <= writeSize) {
return;
}
else {
fseek(fp, writeSize, SEEK_SET);
}
long currRead = 0;
while (!feof(fp)) {
char buffer[EACH_BLOCK] = { 0 };
fread(buffer, sizeof(char), sizeof(buffer), fp);
currRead = ftell(fp);
int size = currRead % EACH_BLOCK == 0 ? EACH_BLOCK : currRead % EACH_BLOCK;
fwrite(buffer, sizeof(char), size, destFp);
// 刷新缓冲区,即立即写入文件,防止程序异常中断,缓冲区内容无法进入文件中
fflush(destFp);
// 进度
printf_s("总大小:%.2fM 已完成:%.2fM 当前进度:%.2f%%\n",
(float)totalSize / 1024 / 1024,
(float)currRead / 1024 / 1024,
(float)currRead / totalSize * 100);
}
fclose(fp);
fclose(destFp);
}