📄 unit_splitfile.~pas
字号:
unit unit_splitfile;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Label1: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
SpeedButton1: TSpeedButton;
Edit1: TEdit;
Edit2: TEdit;
Label3: TLabel;
Label4: TLabel;
OpenDialog1: TOpenDialog;
procedure SpeedButton1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
splitfilename: string;
savefilelength: integer;
{ Public declarations }
Function isAscii(NomeFile: String): Boolean;
function getdir(filename:string):string;
function splitfile(filename:string; filelength:integer):boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Function TForm1.isAscii(NomeFile: String): Boolean;
const
Sett=2048;
var
i: Integer;
F: file;
a: Boolean;
TotSize, IncSize, ReadSize: Integer;
c: Array[0..Sett] of byte;
begin
If FileExists(NomeFile) then
begin
AssignFile(F, NomeFile);
Reset(F, 1);
TotSize:=FileSize(F);
IncSize:=0;
a:=true;
while (IncSize<TotSize) and (a=true) do
begin
ReadSize:=Sett;
If IncSize+ReadSize>TotSize then ReadSize:=TotSize-IncSize;
IncSize:=IncSize+ReadSize;
BlockRead(F, c, ReadSize);
For i := 0 to ReadSize-1 do // Iterate
If (c[i]<32) and (not (c[i] in [9, 10, 13, 26])) then a:=False;
end; // while
CloseFile(F);
If IOResult<>0 then
begin
Result:=False
end
else
begin
Result:=a;
end;
end;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if self.OpenDialog1.Execute then
begin
splitfilename := trim(self.OpenDialog1.FileName);
end;
if not isAscii(trim(splitfilename)) then
begin
splitfilename := '';
showmessage('指定的文件非文本文件!');
end;
self.Edit1.Text := splitfilename;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if trim(splitfilename) = '' then
begin
showmessage('未指定分割文件路径!');
exit;
end;
if trim(self.Edit2.Text) = '' then
begin
showmessage('未指定生成文件大小!');
exit;
end;
if strtoint(trim(self.Edit2.Text))>1024 then
begin
showmessage('文件大小必须小于1M!');
exit;
end
else
begin
savefilelength := strtoint(trim(self.Edit2.Text));
end;
if splitfile(trim(splitfilename), savefilelength) then
begin
showmessage('分割文件结束!');
exit;
end
else
begin
showmessage('分割文件失败!');
exit;
end;
self.ModalResult := MrOK;
end;
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
if (not ((key in ['0'..'9']) or (key=#8)))
or ((trim(self.Edit2.Text)='') and (key='0')) then
begin
key:=#0;
end;
end;
function TForm1.getdir(filename: string): string;
var
temp:string;
fname:string;
begin
temp := copy(filename, 0, length(filename)-length(ExtractFileName(filename)));
fname := ChangeFileExt(ExtractFileName(filename), '');
if not DirectoryExists(temp+fname) then
begin
if not CreateDir(temp+fname) then
begin
raise Exception.Create('Cannot create '+temp+fname);
result:='';
exit;
end;
end;
result:=temp+fname+'\';
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
self.ModalResult := MrCancel;
self.Close;
end;
function TForm1.splitfile(filename: string; filelength:integer): boolean;
var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PChar;
i: Integer;
BackupName: string;
FileHandle: Integer;
StringLen: Integer;
X: Integer;
Y: Integer;
count:integer;
len:integer;
mFile, mText: TFileStream;
begin
try
count:=0;
len:=1024*filelength;
mFile:=TFileStream.Create(filename, fmShareDenyNone);
while len*count<mFile.Size do
begin
mFile.Position:=len*count;
mText:=TFileStream.Create(getdir(filename)+inttostr(count)+'.txt',fmCreate);
if (mFile.Size-mFile.Position)>len then
begin
mText.CopyFrom(mFile,len);
end
else
begin
mText.CopyFrom(mFile,mFile.Size);
end;
FreeAndNil(mText);
inc(count);
end;
FreeAndNil(mFile);
result:=true;
except
FreeAndNil(mFile);
FreeAndNil(mText);
result:=false;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -