Delphi 实现文件分割合并


本文整理自网络,侵删。

 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;
end;
end;
finally
fs.Free;
end;
end;

Note: a 3 KB file 'myfile.ext' will be split into 'myfile._1', 'myfile._2','myfile._3' if FilesByteSize parameter equals 1024 (1 KB).

procedure MergeFiles(FirstSplitFileName, OutFileName : TFileName) ;
// FirstSplitFileName == the name of the first piece of the split file
// OutFileName == the name of the resulting merged file
var
fs, ss: TFileStream;
cnt: integer;
begin
cnt := 1;
fs := TFileStream.Create(OutFileName, fmCreate or fmShareExclusive) ;
try
while FileExists(FirstSplitFileName) do
begin
ss := TFileStream.Create(FirstSplitFileName, fmOpenRead or fmShareDenyWrite) ;
try
fs.CopyFrom(ss, 0) ;
finally
ss.Free;
end;
Inc(cnt) ;
FirstSplitFileName := ChangeFileExt(FirstSplitFileName, Format('%s%d', ['._',cnt])) ;
end;
finally
fs.Free;
end;
end;

Usage:
SplitFile('c:\mypicture.bmp', 1024) ; //into 1 KB files
...
MergeFiles('c:\mypicture._1','c:\mymergedpicture.bmp') ;

相关阅读 >>

Delphi设计循环播放声音文件程序

Delphi 从数组中任取一个

Delphi 搜索字符串

Delphi 设置系统启动文件夹自身开机自动运行

Delphi strtoint 将“字符型”转换成“整数型”

Delphi 取得开机时间 开机时间总长度(可精确到秒,分钟等)

Delphi 利用sendinput模拟鼠标键盘

Delphi adoconnection断线重连

Delphi调用createprocess创建进程

wmi远程访问问题解决方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...