Unit SplitFl;
interface
procedure SplitFile(const pFileName :AnsiString; const pSplitSize :LongInt);
implementation
uses
Classes, SysUtils, Dialogs, Windows;
function Smaller(const a,b:LongInt) :LongInt;
begin
if(a < b)then
begin
Result := a;
end else
if(b > 0)then
begin
Result := b
end else Result := 0;
end;
procedure SplitFile(const pFileName :AnsiString; const pSplitSize :LongInt);
var
vInpFlHandle :Integer;
vOutFlHandle :Integer;
vInpBytesLft :Integer;
vOutBytesLft :Integer;
vBufferSize :Integer;
vBytesDone :Integer;
vBuffer
ointer;
vCtr :Integer;
begin
//Use one of the following options to open the file.
//vInpFlHandle := Integer(CreateFile(PChar(pFileName),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
FILE_FLAG_SEQUENTIAL_SCAN));
vInpFlHandle := FileOpen(pFileName,0);
vInpBytesLft := FileSeek(vInpFlHandle,0,2);
if(vInpBytesLft > pSplitSize)then
begin
vBufferSize := Smaller(GetHeapStatus.TotalUncommitted,pSplitSize);
GetMem(vBuffer,vBufferSize);
FileSeek(vInpFlHandle,0,0);
vCtr := 0;
while(vInpBytesLft > 0)do
begin
Inc(vCtr);
//Use one of the following options to open the file.
//vOutFlHandle := Integer(CreateFile(PChar(pFileName + '.' + FormatFloat('000', vCtr)),GENERIC_READ or GENERIC_WRITE,0,nil,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,FILE_FLAG_SEQUENTIAL_SCAN));
vOutFlHandle := FileCreate(pFileName + '.' + FormatFloat('000', vCtr));
vOutBytesLft := Smaller(vInpBytesLft,pSplitSize);
while(vOutBytesLft > 0)do
begin
vBytesDone := FileRead(vInpFlHandle,vBuffer^,Smaller(vOutBytesLft,vBufferSize));
FileWrite(vOutFlHandle,vBuffer^,vBytesDone);
Dec(vInpBytesLft,vBytesDone);
Dec(vOutBytesLft,vBytesDone);
end;
FileClose(vOutFlHandle);
end;
FreeMem(vBuffer);
end else MessageDlg('File too small to split!',mtInformation,[mbOk],0);
FileClose(vInpFlHandle);
end;
end.