How to Split and Merge Files

There are times when you need to split a large file into several smaller ones (for whatever the purpose is).
The following two methods use Delphi TStream objects to break a file into several files with predefined size;
and to merge those files back to the original.

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure SplitFile(FileName : TFileName; FilesByteSize : Integer) ;
// FileName == file to split into several smaller files
// FilesByteSize == the size of files in bytes
var
   fs, ss: TFileStream;
   cnt : integer;
   SplitName: String;
begin
   fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite) ;
   try
     for cnt := 1 to Trunc(fs.Size / FilesByteSize) + 1 do
     begin
       SplitName := ChangeFileExt(FileName, Format('%s%d', ['._',cnt])) ;
       ss := TFileStream.Create(SplitName, fmCreate or fmShareExclusive) ;
       try
         if fs.Size - fs.Position < FilesByteSize then
           FilesByteSize := fs.Size - fs.Position;
         ss.CopyFrom(fs, FilesByteSize) ;
       finally
         ss.Free;
  

posted @ 2006-12-24 18:52  hingman  阅读(170)  评论(0)    收藏  举报