unit SwfFormatConvertor;
{
http://www.138soft.com
}
interface
uses
Classes, SysUtils, Forms;
function Exe2Swf(SourceExe, DestinySwf: string): boolean;
function Swf2Exe(SourceSwf, DestinyExe: string): boolean;
function IsSwfExeFile(ExeFileName:String):Boolean;
implementation
{$R Player.RES}
function Exe2Swf(SourceExe, DestinySwf: string): boolean;
var
// 分别处理EXE、SWF文件的文件流
SourceStream, DestinyStream: TFileStream;
// SWF文件的大小
SwfFileSize: Cardinal;
begin
Result := False;
// 打开EXE形式的源文件
SourceStream := TFileStream.Create(SourceExe, fmOpenRead or fmShareExclusive);
try
// 读取文件标志
SourceStream.Seek(- 2 * sizeof(integer), soFromEnd);
SourceStream.ReadBuffer(SwfFileSize, sizeof(integer));
// 判断读到的文件标志是否和FA123456相同
// 借此判断是否是Macromedia官方格式的Flash文件
if SwfFileSize = $FA123456 then
begin
// 读取SWF文件的大小
SourceStream.ReadBuffer(SwfFileSize, sizeof(SwfFileSize));
SourceStream.Seek(- SwfFileSize - 2 * sizeof(integer), soFromEnd);
// 打开目标SWF文件
DestinyStream := TFileStream.Create(DestinySwf, fmCreate);
try
// 从EXE文件流中读取数据
DestinyStream.CopyFrom(SourceStream, SwfFileSize);
Result := True;
finally
// OK,释放文件流
DestinyStream.Free;
end;
end;
finally
// OK,释放文件流
SourceStream.Free;
end;
end;
function Swf2Exe(SourceSwf, DestinyExe: string): boolean;
var
// Source、Destiny分别是swf、exe的文件名
SourceStream, DestinyStream: TFileStream;
flag: Cardinal;
SwfFileSize: integer;
Res: TResourceStream;
begin
Result := False;
// 建立EXE文件
DestinyStream := TFileStream.Create(DestinyExe, fmCreate);
try
// 追加Standalone Player
Res := TResourceStream.Create(Hinstance, 'Player', 'binfile');
try
DestinyStream.CopyFrom(Res, 0);
finally
Res.Free;
end;
// 追加SWF文件
SourceStream := TFileStream.Create(SourceSwf, fmOpenRead or fmShareExclusive);
try
DestinyStream.CopyFrom(SourceStream, 0);
// 追加FA123456标志
flag := $FA123456;
DestinyStream.WriteBuffer(flag, sizeof(integer));
// 追加SWF文件长度
SwfFileSize := SourceStream.Size;
DestinyStream.WriteBuffer(SwfFileSize, sizeof(integer));
Result := True;
finally
SourceStream.Free;
end;
finally
DestinyStream.Free;
end;
end;
function IsSwfExeFile(ExeFileName:String):Boolean;
var
// 处理EXE文件的文件流
SourceStream: TFileStream;
// SWF文件的大小
SwfFileSize: Cardinal;
begin
Result := False;
// 打开EXE形式的源文件
SourceStream := TFileStream.Create(ExeFileName, fmOpenRead or fmShareDenyNone);
try
// 读取文件标志
SourceStream.Seek(- 2 * sizeof(integer), soFromEnd);
SourceStream.ReadBuffer(SwfFileSize, sizeof(integer));
// 判断读到的文件标志是否和FA123456相同
// 借此判断是否是Macromedia官方格式的Flash文件
if SwfFileSize = $FA123456 then
Result:=True;
finally
SourceStream.Free;
end;
end;
end.