Author: Tomas Rutkauskas
How to split and join a file using TFileStream
Answer:
1 2 {Procedure Splitfile3 Parameters:4 sourcefilename: name of file to split5 blocksize: maximum size of the parts to create6 7 Call method:8 static9 10 Description:11 12 Will split the passed file into partial files with a size of blocksize bytes (the 13 last one may be smaller). The names of the files are created from the 14 sourcefilename by replacing the extension with a number, .000, .001 etc. The files 15 can be concatenated using the DOS Copy command with the /b switch to regenerate the 16 original file.17 18 Error Conditions:19 I/O errors will raise exceptions.20 21 Created: 03.03.98 by P. Below}22 23 procedure Splitfile(const sourcefilename: string; blocksize: Longint);
24 var25 targetfilename: string;
26 filecounter: Integer;
27 bytesRemaining, bytesToWrite: LongInt;
28 source, target: TFileStream;
29 begin30 ifnot FileExists(sourcefilename) or (blocksize < = 0) then31 Exit;
32 source := TFileStream.Create(sourcefilename, fmOpenRead or fmShareDenyNone);
33 try34 filecounter := 0;
35 bytesRemaining := source.Size;
36 while bytesRemaining > 0 do37 begin38 targetfilename := ChangeFileExt(sourcefilename, Format('.%.3d',
39 [filecounter]));
40 if blocksize < bytesRemaining then41 bytesToWrite := blocksize
42 else43 bytesToWrite := bytesRemaining;
44 target := TFileStream.Create(targetfilename, fmCreate);
45 try46 target.CopyFrom(source, bytesToWrite);
47 bytesRemaining := bytesRemaining - bytesToWrite;
48 Inc(filecounter);
49 finally50 target.Free;
51 end;
52 end;
53 finally54 source.Free;
55 end;
56 end;