所有檔案本質為二進位位元組流。分割:依指定大小 (KB) 切塊寫出 `檔名.001`、`.002`... 合併:依序讀入各分割檔寫回目標檔。
int FileHandle; // 檔案控制碼
int FileLength; // 來源檔大小
int NumParts; // 分割數
int BytesRead; // 已讀位元組
char *pszBuffer; // 緩衝區指標
int BufSize; // 緩衝區大小 (分割大小 * 1024)
void __fastcall TForm1::FileListBox1Change(TObject *Sender)
{
Edit1->Text = FileListBox1->FileName;
Edit3->Text = ChangeFileExt(FileListBox1->FileName, ".000"); // 目標前綴
BufSize = ComboBox1->Text.ToInt() * 1024;
FileHandle = FileOpen(Edit1->Text, fmOpenRead);
FileLength = FileSeek(FileHandle, 0, 2); // 移至末尾取得大小
FileClose(FileHandle);
NumParts = FileLength / BufSize + 1;
Edit4->Text = "分割成 " + IntToStr(NumParts) + " 個檔案";
ListBox1->Clear();
for (int i=1; i<=NumParts; i++)
ListBox1->Items->Add(ChangeFileExt(Edit3->Text, "." + IntToStr(i).Format("%0.3d")));
}
void __fastcall TForm1::BtnSplitClick(TObject *Sender)
{
FileHandle = FileOpen(Edit1->Text, fmOpenRead);
pszBuffer = new char[BufSize];
ProgressBar1->Max = NumParts;
for (int i=1; i<=NumParts; i++) {
BytesRead = FileRead(FileHandle, pszBuffer, BufSize);
int hOut = FileCreate(ListBox1->Items->Strings[i-1]);
FileWrite(hOut, pszBuffer, BytesRead);
FileClose(hOut);
ProgressBar1->Position = i;
Application->ProcessMessages(); // 保持 UI 響應
}
delete[] pszBuffer;
FileClose(FileHandle);
ShowMessage("分割完成!");
}
反向操作:依序 `FileOpen` 各分割檔 → `FileRead`/`FileWrite` 至目標檔 → 進度更新。