Not too hard, here's something that should do it: inf: file; outf: file; size: longint; outsize: longint; amt: word; amtRead: word; assignfile (inf, 'input file'); reset (inf, 1); size := fileSize (inf); repeat showMessage (enter floppy in "A") { or "B" or allow them to specify } assignFile (outf, 'A:output file'); rewrite (outf, 1); outsize := diskFree (1); { or 2 if it's the "B" drive } while (outsize > 0) and (size > 0) do begin amt := sizeof(buf); if amt > outsize then amt := outsize; blockRead (inf, buf, amt, amtRead); blockWrite (outf, buf, amtRead); dec (outSize, amtRead); dec (size, amtRead); end; closeFile (outf); until size <= 0; closeFile (inf);This is OTTOMH, syntax hasn't been checked, etc. You may want to add other code to let the user specify the "A" or "B" drive, and/or a naming scheme so that if disks get out of order it's trapped.Re-assembling the files is similar: open outf on the hard disk, ask the user for the first floppy, blockRead/blockWrite from the floppy to the hard disk, then ask the user for the next floppy, etc. until all floppies are read.
界面设计:
创建主窗体 XCutForm,保存应用程序为 Xcut.dpr1.选取文件选取文件就是要弹出一个文件选取对话框给用户一个选择的机会,在Delphi中很容易实现,在Dialog对话框选取项中选取OpenDialog放到窗体中,名称为OpenDialog.编辑OpenDialog的Filter 属性,如下图所示:
2.文件切割大小放置一个PageControl 控件在主窗体中,添加两个 TabSheet ,分别为 Tbs1和Tbs2.
在Tbs1上放一个RadioGroup,名称为rdgCut,编辑RadioGroup控件的属性,如下图所示:给用户一个选取常用文件大小的列表在Tbs2上放一个输入框,让用户可以自己设定文件大小:3.指定切割文件存放位置(目标文件夹)对于选取一个目标文件夹的对话框,在Delphi中没有直接支持,我们可以启用Com扩展打开Windows的标准打开文件夹对话框。为了方便使用,我们把它封装成一个方便使用的单元BrowseFolder(详见程序清单),在调用的时候只需要几行代码Procedure
var
dlg: TBrowseForFolderDialog;
DestPath:string;
begin
dlg := TBrowseForFolderDialog.Create;
dlg.Title := '请选取目标文件夹:';
if dlg.Execute then
begin
DestPath := dlg.Path;
end;
dlg.Free;
end;4.开始切割根据上边步骤的选择,确定了待切割的原始文件,切割大小和存放位置,下边就可以开始了。
首先我们用一个“文件流”以只读方式打开原始文件
var
fs:TFileStream;
begin
fs:=TfileStream.create(“文件名称”,fmOpenRead)
……………然后我们创建一个“内存流”
var
ms:TmemoryStream;
begin
ms:=TmemoryStream.create;
……………
根据文件切割的大小计算字节数,从“文件流”中向“内存流”中拷贝指定的大小,将内存流存储到目标文件夹下,文件扩展名累计创建(方便合并)分析切割大小:
case rdgCut.ItemIndex of
0: filesize := 2000 * 1024; //2000 K
1: filesize := 1440 * 1024; //1440 K
2: filesize := 1200 * 1024; //1200 K
3: filesize := 1000 * 1024; //1000 K
4: filesize := 720 * 1024; //720 K
5: filesize := 500 * 1024; //500 K
6: filesize := 300 * 1024; //300 K
7: filesize := 200 * 1024; //200 K
else filesize := 100 * 1024; //100 K
在切割过程中应该显示一个进度,让用户了解进度,放置一个ProgressBar控件到主窗体中,设置属性。在切割开始时根据文件大小设定最大最小值和步长,在切割过程中不断根据实际情况修改当前位置,给用户一个运动的状态。代码如下所示:过程化显示切割进程fs := TFileStream.Create(filename, fmopenread);
ms := TMemoryStream.Create;
FileCount := 1;prbCut.Min := 0;
prbCut.Max := (fs.size div filesize) + 1;
prbCut.step := 1;while fs.Position < fs.Size - 1 do
begin
ms.position := 0;
ms.size := 0;if fs.size - fs.Position < FileSize
then FileSize := fs.size - fs.position;ms.CopyFrom(fs, FileSize);
application.ProcessMessages;prbCut.StepIt;
ms.SaveToFile(DestPath + '\' + extractfilename(filename) + '.' +
inttostr(FileCount));
memCut.lines.add('正在生成第 ' + inttostr(FileCount) + ' 个文件!');
inc(FileCount);end;
5.创建自动合并文件的批处理命令在 Dos 中有一条命令可以完成文件的合并
Copy file1 /b +file2 /b file3我们只要生成一个批处理命令文件,就可以在需要的时候完成合并工作,代码如下所示:assignfile(bat, DestPath + '\' +
changefileext(ExtractFileName(filename), '.bat'));
rewrite(bat);
writeln(bat, 'echo off');
writeln(bat,'echo 谢谢使用文件分割 XCut 1.0 .....');
writeln(bat, 'echo .');
writeln(bat,'echo 正在准备生成文件'+extractfilename(filename));
writeln(bat, 'echo .');
writeln(bat,' echo 您可以按 CTRL + Z 键 中止,其他键继续!');writeln(bat,'pause');
for i := 1 to FileCount + 1 do
begin
if i = 1
then writeln(bat, 'copy ' + extractfilename(FileName) + '.1/b ' +
extractfilename(FileName))
else writeln(bat, 'copy ' + extractfilename(FileName) + '/b +' +
extractfilename(FileName) + '.' + inttostr(i) + '/b');end;
memCut.lines.add('正在生成合并命令文件: ' +
changefileext(extractfilename(FileName), '.bat'));closefile(bat);memCut.lines.add('切割成功完成!');
Application.MessageBox('切割成功完成!', '恭喜', MB_OK + MB_ICONINFORMATION);6.运行结果显示
我记得有这样的代码,如果你需要,我可以帮你找找!
[email protected]
outf: file;
size: longint;
outsize: longint;
amt: word;
amtRead: word; assignfile (inf, 'input file');
reset (inf, 1);
size := fileSize (inf);
repeat
showMessage (enter floppy in "A") { or "B" or allow them to specify }
assignFile (outf, 'A:output file');
rewrite (outf, 1);
outsize := diskFree (1); { or 2 if it's the "B" drive }
while (outsize > 0) and (size > 0) do begin
amt := sizeof(buf);
if amt > outsize then amt := outsize;
blockRead (inf, buf, amt, amtRead);
blockWrite (outf, buf, amtRead);
dec (outSize, amtRead);
dec (size, amtRead);
end;
closeFile (outf);
until size <= 0;
closeFile (inf);This is OTTOMH, syntax hasn't been checked, etc. You may want to add other
code to let the user specify the "A" or "B" drive, and/or a naming scheme so
that if disks get out of order it's trapped.Re-assembling the files is similar: open outf on the hard disk, ask the user
for the first floppy, blockRead/blockWrite from the floppy to the hard disk,
then ask the user for the next floppy, etc. until all floppies are read.