-
Notifications
You must be signed in to change notification settings - Fork 14
/
FilePreparer.dpr
104 lines (86 loc) · 2.38 KB
/
FilePreparer.dpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
program FilePreparer;
{$WEAKLINKRTTI ON}
{$IFDEF DEBUG}
{$APPTYPE CONSOLE}
{$ENDIF}
{$R *.res}
uses
Winapi.Windows,
System.SysUtils, System.Classes, System.ZLib;
{$SETPEFlAGS
IMAGE_FILE_DEBUG_STRIPPED or
IMAGE_FILE_LINE_NUMS_STRIPPED or
IMAGE_FILE_LOCAL_SYMS_STRIPPED or
IMAGE_FILE_RELOCS_STRIPPED}
const
CompressionExt = '.gzip';
ParamDir = '-d';
function CompressFile(Dir: string; FileName: string): Boolean;
var
InStream: TFileStream;
OutStream: TFileStream;
CompressionStream: TCompressionStream;
CompressionFileName: string;
begin
Result := True;
try
CompressionFileName := ChangeFilePath(FileName, Dir);
CompressionFileName := ChangeFileExt(CompressionFileName, CompressionExt);
InStream := TFileStream.Create(FileName, fmOpenRead);
try
OutStream := TFileStream.Create(CompressionFileName, fmCreate or fmOpenWrite or fmShareDenyWrite);
try
OutStream.Position := 0;
CompressionStream := TCompressionStream.Create(clMax, OutStream);
try
InStream.Position := 0;
CompressionStream.CopyFrom(InStream, InStream.Size);
finally
CompressionStream.Free;
end;
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
except
Result := False;
end;
end;
procedure ShowHelp;
begin
MessageBox(0, 'CmdLine: [-d "Target Directory"] "File 1"[ "File 2" "File 3" ...]', 'Using', MB_ICONINFORMATION or MB_OK);
end;
procedure PrepareFail(Msg: string);
begin
MessageBox(0, LPCTSTR(Msg), 'Error', MB_ICONERROR or MB_OK);
end;
var
TargetDir: string;
FileName: string;
I: Integer;
begin
if ParamCount < 1 then begin
ShowHelp;
ExitProcess(1);
end;
if ParamStr(1) = ParamDir then begin
if ParamCount < 2 then ExitProcess(1);
TargetDir := ParamStr(2);
if not ForceDirectories(TargetDir) then ExitProcess(1);
I := 3;
end else
I := 1;
while I <= ParamCount do begin
FileName := ParamStr(I);
if IsRelativePath(FileName) then
FileName := ExpandFileName(FileName);
if not DirectoryExists(TargetDir) then
TargetDir := ExtractFileDir(FileName);
if not FileExists(FileName) then ExitProcess(1);
if not CompressFile(TargetDir, FileName) then ExitProcess(1);
Inc(I);
end;
ExitProcess(0);
end.