2005-10-07 17:42:18 +02:00
|
|
|
|
unit UnitPackSystem;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses SysUtils, Classes, Zlib;
|
|
|
|
|
|
2007-10-10 23:48:44 +02:00
|
|
|
|
procedure CompressFiles(Files: TStrings; const Filename: String);
|
|
|
|
|
function DecompressStream(Stream: TMemoryStream; DestDirectory: String; const Source: Boolean): Boolean;
|
2006-11-30 23:53:20 +01:00
|
|
|
|
function AttachToFile(const AFileName: string; MemoryStream: TMemoryStream; Version: String): Boolean;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
function LoadFromFile(const AFileName: string; MemoryStream: TMemoryStream): Boolean;
|
2007-10-10 23:48:44 +02:00
|
|
|
|
function Unpack(const Source: Boolean): Boolean;
|
2006-11-30 23:53:20 +01:00
|
|
|
|
function GetVersion: String;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
2006-11-30 23:53:20 +01:00
|
|
|
|
uses UnitfrmMain;
|
|
|
|
|
|
2005-10-07 17:42:18 +02:00
|
|
|
|
procedure CompressFiles(Files : TStrings; const Filename : String);
|
|
|
|
|
var
|
|
|
|
|
infile, outfile, tmpFile : TFileStream;
|
|
|
|
|
compr : TCompressionStream;
|
|
|
|
|
i,l : Integer;
|
|
|
|
|
s : String;
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
if Files.Count > 0 then
|
|
|
|
|
begin
|
|
|
|
|
outFile := TFileStream.Create(Filename,fmCreate);
|
|
|
|
|
try
|
|
|
|
|
{ the number of files }
|
|
|
|
|
l := Files.Count;
|
|
|
|
|
outfile.Write(l,SizeOf(l));
|
|
|
|
|
for i := 0 to Files.Count-1 do
|
|
|
|
|
begin
|
|
|
|
|
infile := TFileStream.Create(Files[i],fmOpenRead);
|
|
|
|
|
try
|
|
|
|
|
{ the original filename }
|
|
|
|
|
s := ExtractFilename(Files[i]);
|
|
|
|
|
l := Length(s);
|
|
|
|
|
outfile.Write(l,SizeOf(l));
|
|
|
|
|
outfile.Write(s[1],l);
|
|
|
|
|
{ the original filesize }
|
|
|
|
|
l := infile.Size;
|
|
|
|
|
outfile.Write(l,SizeOf(l));
|
|
|
|
|
{ compress and store the file temporary}
|
|
|
|
|
tmpFile := TFileStream.Create('tmp',fmCreate);
|
|
|
|
|
compr := TCompressionStream.Create(clMax,tmpfile);
|
|
|
|
|
try
|
|
|
|
|
compr.CopyFrom(infile,l);
|
|
|
|
|
finally
|
|
|
|
|
compr.Free;
|
|
|
|
|
tmpFile.Free;
|
|
|
|
|
end;
|
|
|
|
|
{ append the compressed file to the destination file }
|
|
|
|
|
tmpFile := TFileStream.Create('tmp',fmOpenRead);
|
|
|
|
|
try
|
2007-10-16 20:43:23 +02:00
|
|
|
|
l := tmpFile.Size;
|
|
|
|
|
outfile.WriteBuffer(l, SizeOf(l));
|
2005-10-07 17:42:18 +02:00
|
|
|
|
outfile.CopyFrom(tmpFile,0);
|
|
|
|
|
finally
|
|
|
|
|
tmpFile.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
infile.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
outfile.Free;
|
|
|
|
|
end;
|
|
|
|
|
DeleteFile('tmp');
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2007-10-10 23:48:44 +02:00
|
|
|
|
|
|
|
|
|
function DecompressStream(Stream : TMemoryStream; DestDirectory : String; const Source: Boolean): Boolean;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
var
|
|
|
|
|
dest,s : String;
|
|
|
|
|
decompr : TDecompressionStream;
|
|
|
|
|
outfile : TFilestream;
|
2007-10-16 20:43:23 +02:00
|
|
|
|
i,l,lr,c : Integer;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
begin
|
|
|
|
|
// IncludeTrailingPathDelimiter (D6/D7 only)
|
|
|
|
|
dest := IncludeTrailingPathDelimiter(DestDirectory);
|
|
|
|
|
|
|
|
|
|
Result := False;
|
|
|
|
|
try
|
|
|
|
|
{ number of files }
|
|
|
|
|
Stream.Read(c,SizeOf(c));
|
2007-10-10 23:48:44 +02:00
|
|
|
|
for i := 1 to c do begin
|
2005-10-07 17:42:18 +02:00
|
|
|
|
{ read filename }
|
|
|
|
|
Stream.Read(l,SizeOf(l));
|
|
|
|
|
SetLength(s,l);
|
|
|
|
|
Stream.Read(s[1],l);
|
2007-10-16 20:43:23 +02:00
|
|
|
|
Stream.Read(l,SizeOf(l));
|
|
|
|
|
Stream.Read(lr,SizeOf(lr));
|
2007-10-10 23:48:44 +02:00
|
|
|
|
{ check if this is the right file }
|
2007-10-16 20:43:23 +02:00
|
|
|
|
if (s = 'hl2launch.exe') or ((Pos('.source', s) <> 0) and (Source)) or ((Pos('.orangebox', s) <> 0) and (not Source)) then begin
|
2007-10-10 23:48:44 +02:00
|
|
|
|
{ remove extension and read filesize }
|
2007-10-16 20:43:23 +02:00
|
|
|
|
if (s <> 'hl2launch.exe') then
|
|
|
|
|
s := ChangeFileExt(s, '');
|
2007-10-10 23:48:44 +02:00
|
|
|
|
{ decompress the files and store it }
|
|
|
|
|
s := dest+s; //include the path
|
|
|
|
|
outfile := TFileStream.Create(s,fmCreate);
|
2007-10-16 20:43:23 +02:00
|
|
|
|
decompr := TDecompressionStream.Create(Stream);
|
2007-10-10 23:48:44 +02:00
|
|
|
|
try
|
|
|
|
|
outfile.CopyFrom(decompr,l);
|
|
|
|
|
finally
|
|
|
|
|
outfile.Free;
|
|
|
|
|
decompr.Free;
|
|
|
|
|
end;
|
2007-10-16 20:43:23 +02:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
Stream.Position := Stream.Position + lr;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
Result := True;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2006-11-30 23:53:20 +01:00
|
|
|
|
function AttachToFile(const AFileName: string; MemoryStream: TMemoryStream; Version: String): Boolean;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
var
|
|
|
|
|
aStream: TFileStream;
|
|
|
|
|
iSize: Integer;
|
|
|
|
|
begin
|
|
|
|
|
Result := False;
|
|
|
|
|
if not FileExists(AFileName) then
|
|
|
|
|
Exit;
|
2007-10-16 20:43:23 +02:00
|
|
|
|
|
2005-10-07 17:42:18 +02:00
|
|
|
|
try
|
|
|
|
|
aStream := TFileStream.Create(AFileName, fmOpenWrite or fmShareDenyWrite);
|
|
|
|
|
MemoryStream.Seek(0, soFromBeginning);
|
|
|
|
|
// seek to end of File
|
|
|
|
|
// ans Ende der Datei Seeken
|
|
|
|
|
aStream.Seek(0, soFromEnd);
|
|
|
|
|
// copy data from MemoryStream
|
|
|
|
|
// Daten vom MemoryStream kopieren
|
|
|
|
|
aStream.CopyFrom(MemoryStream, 0);
|
|
|
|
|
// save Stream-Size
|
|
|
|
|
// die Streamgr<67><72>e speichern
|
|
|
|
|
iSize := MemoryStream.Size + SizeOf(Integer);
|
|
|
|
|
aStream.Write(iSize, SizeOf(iSize));
|
2006-11-30 23:53:20 +01:00
|
|
|
|
// save version number+length
|
|
|
|
|
iSize := aStream.Position;
|
|
|
|
|
aStream.Write(Version[1], Length(Version));
|
|
|
|
|
aStream.Write(iSize, SizeOf(iSize));
|
2005-10-07 17:42:18 +02:00
|
|
|
|
finally
|
|
|
|
|
aStream.Free;
|
|
|
|
|
end;
|
|
|
|
|
Result := True;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function LoadFromFile(const AFileName: string; MemoryStream: TMemoryStream): Boolean;
|
|
|
|
|
var
|
2006-11-30 23:53:20 +01:00
|
|
|
|
aStream: TMemoryStream;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
iSize: Integer;
|
2006-11-30 23:53:20 +01:00
|
|
|
|
EndPos: Integer;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := False;
|
|
|
|
|
if not FileExists(AFileName) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
try
|
2006-11-30 23:53:20 +01:00
|
|
|
|
aStream := TMemoryStream.Create;
|
|
|
|
|
aStream.LoadFromFile(AFileName);
|
|
|
|
|
// drop version part
|
|
|
|
|
aStream.Seek(-SizeOf(Integer), soFromEnd);
|
|
|
|
|
aStream.Read(EndPos, SizeOf(Integer));
|
|
|
|
|
aStream.SetSize(EndPos);
|
2005-10-07 17:42:18 +02:00
|
|
|
|
// seek to position where Stream-Size is saved
|
|
|
|
|
// zur Position seeken wo Streamgr<67><72>e gespeichert
|
|
|
|
|
aStream.Seek(-SizeOf(Integer), soFromEnd);
|
|
|
|
|
aStream.Read(iSize, SizeOf(iSize));
|
|
|
|
|
if iSize > aStream.Size then
|
|
|
|
|
begin
|
|
|
|
|
aStream.Free;
|
|
|
|
|
Exit;
|
|
|
|
|
end;
|
|
|
|
|
// seek to position where data is saved
|
|
|
|
|
// zur Position seeken an der die Daten abgelegt sind
|
|
|
|
|
aStream.Seek(-iSize, soFromEnd);
|
|
|
|
|
MemoryStream.SetSize(iSize - SizeOf(Integer));
|
|
|
|
|
MemoryStream.CopyFrom(aStream, iSize - SizeOf(iSize));
|
|
|
|
|
MemoryStream.Seek(0, soFromBeginning);
|
|
|
|
|
finally
|
|
|
|
|
aStream.Free;
|
|
|
|
|
end;
|
|
|
|
|
Result := True;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
{ Unpack function }
|
|
|
|
|
|
2007-10-10 23:48:44 +02:00
|
|
|
|
function Unpack(const Source: Boolean): Boolean;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
var eStream: TMemoryStream;
|
|
|
|
|
begin
|
|
|
|
|
eStream := TMemoryStream.Create;
|
|
|
|
|
try
|
2006-11-30 23:53:20 +01:00
|
|
|
|
// Get ZIP
|
|
|
|
|
LoadFromFile(ParamStr(0), eStream);
|
2007-10-10 23:48:44 +02:00
|
|
|
|
DecompressStream(eStream, ExtractFilePath(ParamStr(0)), Source); // Unpack files
|
2006-06-28 00:39:42 +02:00
|
|
|
|
|
|
|
|
|
Result := True;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
except
|
2006-06-28 00:39:42 +02:00
|
|
|
|
Result := False;
|
2005-10-07 17:42:18 +02:00
|
|
|
|
end;
|
|
|
|
|
eStream.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2006-11-30 23:53:20 +01:00
|
|
|
|
function GetVersion: String;
|
|
|
|
|
var FileStream: TFileStream;
|
|
|
|
|
EndPos, Size: Integer;
|
|
|
|
|
Version: String;
|
|
|
|
|
begin
|
|
|
|
|
FileStream := TFileStream.Create(ParamStr(0), fmOpenRead or fmShareDenyWrite);
|
|
|
|
|
FileStream.Seek(-SizeOf(Integer), soFromEnd);
|
|
|
|
|
FileStream.Read(EndPos, SizeOf(EndPos));
|
|
|
|
|
FileStream.Position := EndPos;
|
|
|
|
|
Size := FileStream.Size - EndPos - SizeOf(Integer);
|
|
|
|
|
SetString(Result, nil, Size);
|
|
|
|
|
FileStream.Read(Pointer(Result)^, Size); // YAMS
|
|
|
|
|
FileStream.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2005-10-07 17:42:18 +02:00
|
|
|
|
end.
|