delphi文件捆绑器


本文整理自网络,侵删。

 将文件1,文件2捆绑到下面这个ade_ivy.exe的后面,并把两个文件的大小附加到后面,程序也不复杂,有点难度的是对文件大小的处理,就是添加到ade_ivy.exe后面8个字符。
ade_ivy运行时先读取两个文件的大小,并从自身分离出file1.exe,file2.exe,运行之,程序简单,大鸟飞过,菜鸟拿去玩玩吧。
program ade_ivy;
uses
Windows,SysUtils,Classes,Dialogs;

var
file1_size,file2_size,file_size:Integer;
//全局变量
f: file;
str_len:string;
each_size: array[1..2] of char;
str: array[1..100] of char;
Setstr: string;
len, i: integer;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function GetFileSize(const FileName: String): LongInt;
var SearchRec: TSearchRec;
begin
if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
Result := SearchRec.Size
else
Result := -1;
end;

procedure ExtractFile(start:Integer;FileSize:Integer;SaveFile:string);
var
sStream, dStream: TFileStream;
begin
sStream := TFileStream.Create(ParamStr(0), fmOpenRead or fmShareDenyNone);
dStream := TFileStream.Create(SaveFile, fmCreate);
sStream.Seek(start, 0);
dStream.CopyFrom(sStream, FileSize);
sStream.Free;
dStream.Free;
end;
function Trim(const S: string): string;
var
I, L: Integer;
begin
L := Length(S);
I := 1;
while (I <= L) and (S[I] <= ' ') do Inc(I);
if I > L then Result := '' else
begin
while S[L] <= ' ' do Dec(L);
Result := Copy(S, I, L - I + 1);
end;
end;
begin
//========================read setting string=================================
FileMode := 0;
assignfile(f,paramstr(0));
Reset(f, 1);
{get_size_of_file2}
seek(f, filesize(f) - 4);
BlockRead(f, each_size, 4);
str_len:='';
for i := 1 to 4 do
str_len:=str_len+inttostr(ord(each_size[i]));
file2_size:=StrToInt(str_len);
{get_size_of_file1}
seek(f, filesize(f) - 8);
BlockRead(f, each_size, 4);
str_len:='';
for i := 1 to 4 do
str_len:=str_len+inttostr(ord(each_size[i]));
file1_size:=StrToInt(str_len);
{get_file2}
file_size:=GetFileSize(paramstr(0));
ExtractFile(file_size-file1_size-file2_size-8,file1_size,'./file1.exe');
ExtractFile(file_size-file2_size-8,file2_size,'./file2.exe');
{get_file1}
WinExec('./file1.exe',SW_SHOW);
WinExec('./file2.exe',SW_SHOW);
end.
//生成器代码
unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, WinSkinData;

type
TLenStr=array[1..8]of Char;
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
OpenDialog2: TOpenDialog;
SaveDialog1: TSaveDialog;
Label1: TLabel;
Label2: TLabel;
SkinData1: TSkinData;
procedure Button5Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Res: TResourceStream;
fileMemStream,Source:TMemoryStream;
implementation
{$R fu.res}
{$R *.dfm}
function GetLenStr(size1:Integer;size2:Integer):TLenStr ;
var
s1,s2:string;
i1,i2,i:Integer;
tempstr,teststr:string;
begin
s1:=IntToStr(size1);
s2:=IntToStr(size2);
i1:=Length(s1);
i2:=Length(s2);
tempstr:='';
for i:=1 to 8-i1 do
tempstr:=tempstr+'0';
s1:=tempstr+s1;
tempstr:='';
for i:=1 to 8-i2 do
tempstr:=tempstr+'0';
s2:=tempstr+s2;
teststr:=Copy(s1,0,2);
Result[1]:=Char(StrToInt(teststr));
teststr:=Copy(s1,3,2);
Result[2]:=Char(StrToInt(teststr));
teststr:=Copy(s1,5,2);
Result[3]:=Char(StrToInt(teststr));
teststr:=Copy(s1,7,2);
Result[4]:=Char(StrToInt(teststr));
teststr:=Copy(s2,0,2);
Result[5]:=Char(StrToInt(teststr));
teststr:=Copy(s2,3,2);
Result[6]:=Char(StrToInt(teststr));
teststr:=Copy(s2,5,2);
Result[7]:=Char(StrToInt(teststr));
teststr:=Copy(s2,7,2);
Result[8]:=Char(StrToInt(teststr));
//ShowMessage('s1:'+s1+'---s2:'+s2);
//ShowMessage(Result);
end;
function GetFileSize(const FileName: String): LongInt;
var SearchRec: TSearchRec;
begin
if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
Result := SearchRec.Size
else
Result := -1;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
ShowMessage('文件捆绑,自己的思路,和网上的不知是否相同');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text:=OpenDialog1.FileName;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog2.Execute then
Edit2.Text:=OpenDialog2.FileName;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
FStream1, FStream2: TFileStream;
f:file;
fileHandle:Integer;
buf:TLenStr;
NumRead,Numwritten:word;
begin
if (Edit1.Text='')or(Edit2.Text='')then ShowMessage('请选择文件')
else
begin
Res := TResourceStream.Create(Hinstance, 'fuwuduan', RT_RCDATA);
source:= TMemoryStream.Create;
source.LoadFromStream(res);
Source.Seek(Source.Size,0);//定位到尾部
{addFile1}
FStream1 := TFileStream.Create(OpenDialog1.FileName, fmOpenRead or fmShareExclusive);
FStream2 := TFileStream.Create(OpenDialog2.FileName, fmOpenRead or fmShareExclusive);
Source.CopyFrom(FStream1,FStream1.Size);
Source.CopyFrom(FStream2,FStream2.Size);
Source.SaveToFile('./fuck.exe');
FileHandle := FileOpen('./fuck.exe', fmOpenWrite);
buf:=getLenStr(FStream1.Size,FStream2.Size);
FileSeek(fileHandle, GetFileSize('./fuck.exe'),0);
FileWrite(fileHandle,buf,8);
FileClose(fileHandle);
FStream1.Free;
FStream2.Free;
Source.Free;
ShowMessage('文件fuck.exe生成完毕');
end;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
buf:TLenStr;
begin
buf:=getLenStr(12345678,56789);
end;

end.

相关阅读 >>

Delphi downloadtomemory

使用Delphi启动和关闭外部应用程序

Delphi if语法,弹出提示框,不等于写法,判断是否为空

Delphi 获取网卡信息(支持多网卡)

Delphi操作excel说明

Delphi byte、pbyte、tbytes的转换

Delphi firedac获取自增长字段值

Delphi 利用sendinput模拟鼠标键盘

Delphi替换字符串中的单引号

Delphi 之 工具栏组件(ttoolbar)

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



打赏

取消

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

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

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

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

评论

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