-
Notifications
You must be signed in to change notification settings - Fork 2
/
Compress.dpr
292 lines (264 loc) · 7.45 KB
/
Compress.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
program Compress;
{$I DefineType.pas}
{$APPTYPE CONSOLE}
uses
{$IFDEF StreamType}
Classes,
{$ENDIF}
{$IFDEF MSWINDOWS}Windows, {$ENDIF}
SysUtils, FileBuffer, CompressUnit, Math, ProgressUnit;
Const
Version='2.4.4';
ArcExt='.z';
SignatureSize=3;
Signature: Array [1..4] of Byte=($50, $41, $47, $21);
{$IFDEF FPC}
fpcVersion={$I %FPCVERSION%};
{$ENDIF}
Var
k: Byte;
SignatureOnly, Raw:Boolean;
i: Word;
Time, ArcFileSize, DataFileSize: Cardinal;
InFileName, OutFileName, OutFileName2: String;
Function UpTime: Cardinal;
Begin
Result:={$IFDEF MSWINDOWS}{$IFDEF FPC}GetTickCount64{$ELSE}GetTickCount{$ENDIF}
{$ELSE}
((StrToInt(FormatDateTime('H', Time))*3600+StrToInt(FormatDateTime('N', Time))*60+
StrToInt(FormatDateTime('S', Time)))*1000)+StrToInt(FormatDateTime('Z', Time));
{$ENDIF};
End;
Procedure WriteInfo(Time: Cardinal; ArcSize, FSize: Int64);
var
sp: Real;
Begin
WriteLn;
WriteLn;
If Time<2500 then
WriteLn('Time: '+IntToStr(Time)+' ms.')
Else If (Time>2500)and(Time<120000) then
WriteLn('Time: '+IntToStr(Time div 1000)+' s. '+IntToStr(Time mod 1000)+' ms.')
Else If (Time>=120000)and(Time<3600000) then
WriteLn('Time: '+IntToStr(Time div 60000)+' m. '+IntToStr((Time mod 60000)div 1000)+' s.')
Else
WriteLn('Time: '+IntToStr(Time div 3600000)+' h. '+IntToStr((Time mod 3600000)div 60000)+' m.');
sp:=(FSize/1024)/(Time/1000);
If sp<2500 then
WriteLn('Speed: '+FloatToStr(SimpleRoundTo(sp, -2))+' Kibytes/s')
Else
WriteLn('Speed: '+FloatToStr(SimpleRoundTo(sp/1024, -2))+' Mibytes/s');
WriteLn('Ratio: '+FloatToStr(SimpleRoundTo(((ArcSize/FSize)*1000)/1000, -3)));
If ArcSize<100000 then
WriteLn('Arc Size: '+IntToStr(ArcSize)+' bytes.')
Else If (ArcSize>100000)and(ArcSize<1200000) then
WriteLn('Arc Size: '+IntToStr(ArcSize div 1024)+' Kib.')
Else If (ArcSize>1200000)and(ArcSize<1073741824) then
WriteLn('Arc Size: '+IntToStr(ArcSize div(1024*1024))+' Mib.')
Else If ArcSize>1073741824 then
WriteLn('Arc Size: '+IntToStr(ArcSize div(1024*1024*1024))+' Gib. '+
IntToStr(ArcSize mod(1024*1024*1024)div(1024*1024))+' Mib.');
End;
// ====================Main program===========================
{$R *.res}
begin
If (ParamStr(1)='/?')or(ParamStr(1)='') then
Begin
{$IFDEF FPC}
WriteLn('FPC v.'+fpcVersion);
{$ENDIF}
WriteLn('Compress v.'+Version);
WriteLn(ExtractFileName(ParamStr(0))+' c|d InputFile.ext [OutputFile.ext]');
WriteLn(' c compress');
WriteLn(' d decompress');
WriteLn;
WriteLn(' switches:');
WriteLn(' -b VALUE max bits (16 default)');
WriteLn(' -t test mode');
WriteLn(' -o output file name');
WriteLn(' -p without progress bar');
WriteLn(' -r raw mode');
WriteLn(' -s write signature only');
ReadLn;
Halt;
End;
WriteMode:=1;
MaxWordSize:=16;
SignatureOnly:=False;
Raw:=False;
For i:=1 to ParamCount do
Begin
If ParamStr(i)='-b' then
MaxWordSize:=StrToInt(ParamStr(i+1));
If ParamStr(i)='-t' then
WriteMode:=0;
If ParamStr(i)='-o' then
OutFileName:=ParamStr(i+1);
If ParamStr(i)='-p' then
NoProgress:=True;
If ParamStr(i)='-s' then
SignatureOnly:=True;
If ParamStr(i)='-r' then
Raw:=True;
End;
InFileName:=ParamStr(2);
If LowerCase(ParamStr(1))='c' then
Begin
If FileExists(InFileName) then
Begin
WriteLn;
WriteLn('Compressing... '+InFileName);
{$IFDEF StreamType}
DataFile:=TFileStream.Create(InFileName, fmOpenRead or fmShareDenyWrite);
{$ENDIF}
{$IFDEF FileType}
AssignFile(DataFile, InFileName);
ReSet(DataFile);
{$ENDIF}
If WriteMode=1 then
Begin
If not Raw then
For i:=1 to SignatureSize do
BitWrite(ArcFile, Signature[i], 8);
OutFileName2:=ExtractFileName(InFileName);
If not Raw then
If not SignatureOnly then
Begin
For i:=1 to Length(OutFileName2) do
BitWrite(ArcFile, Ord(OutFileName2[i]), 8);
BitWrite(ArcFile, $2A, 8);
End;
If OutFileName='' then
OutFileName:=ChangeFileExt(ExtractFileName(InFileName), ArcExt);
{$IFDEF StreamType}
ArcFile:=TFileStream.Create(OutFileName, fmCreate);
{$ENDIF}
{$IFDEF FileType}
AssignFile(ArcFile, OutFileName);
ReWrite(ArcFile);
{$ENDIF}
End;
Time:=UpTime;
CompressProc(DataFile, ArcFile);
Time:=UpTime-Time+1;
ArcFileSize:=GetFSize(ArcFile);
DataFileSize:=GetFSize(DataFile);
WriteInfo(Time, ArcFileSize, DataFileSize);
{$IFDEF StreamType}
DataFile.Free;
{$ENDIF}
{$IFDEF FileType}
CloseFile(DataFile);
{$ENDIF}
If WriteMode=1 then
{$IFDEF StreamType}
ArcFile.Free;
{$ENDIF}
{$IFDEF FileType}
CloseFile(ArcFile);
{$ENDIF}
End;
End;
If LowerCase(ParamStr(1))='d' then
Begin
If Pos(ArcExt, InFileName)=0 then
InFileName:=InFileName+ArcExt;
If FileExists(InFileName) then
Begin
{$IFDEF StreamType}
ArcFile:=TFileStream.Create(InFileName, 0);
FSize:=GetFSize(ArcFile);
{$ENDIF}
{$IFDEF FileType}
AssignFile(ArcFile, InFileName);
ReSet(ArcFile);
FSize:=FileSize(ArcFile);
{$ENDIF}
If not Raw then
Begin
k:=0;
For i:=1 to SignatureSize do
If BitRead(ArcFile, 8)=Signature[i] then
Inc(k);
End
Else
k:=SignatureSize;
If k=SignatureSize then
Begin
If not Raw then
If not SignatureOnly then
Begin
OutFileName2:='';
repeat
k:=BitRead(ArcFile, 8);
OutFileName2:=OutFileName2+Chr(k);
Until k=$2A;
Delete(OutFileName2, Length(OutFileName2), 1);
End;
If OutFileName='' then
OutFileName:=OutFileName2;
If WriteMode=1 then
Begin
{$IFDEF StreamType}
DataFile:=TFileStream.Create(OutFileName, fmCreate);
{$ENDIF}
{$IFDEF FileType}
AssignFile(DataFile, OutFileName);
ReWrite(DataFile);
{$ENDIF}
End;
DeCompressProc(ArcFile, DataFile);
{$IFDEF StreamType}
ArcFile.Free;
{$ENDIF}
{$IFDEF FileType}
CloseFile(ArcFile);
{$ENDIF}
End;
If WriteMode=1 then
{$IFDEF StreamType}
DataFile.Free;
{$ENDIF}
{$IFDEF FileType}
CloseFile(DataFile);
{$ENDIF}
End;
End;
If LowerCase(ParamStr(1))='l' then
Begin
If Pos(ArcExt, InFileName)=0 then
InFileName:=InFileName+ArcExt;
If FileExists(InFileName) then
Begin
{$IFDEF StreamType}
ArcFile:=TFileStream.Create(InFileName, 0);
FSize:=GetFSize(ArcFile);
{$ENDIF}
{$IFDEF FileType}
AssignFile(ArcFile, InFileName);
ReSet(ArcFile);
FSize:=FileSize(ArcFile);
{$ENDIF}
k:=0;
For i:=1 to SignatureSize do
If BitRead(ArcFile, 8)=Signature[i] then
Inc(k);
If k=SignatureSize then
Begin
OutFileName:='';
Repeat
k:=BitRead(ArcFile, 8);
OutFileName:=OutFileName+Chr(k);
Until k=$2A;
Delete(OutFileName, Length(OutFileName), 1);
WriteLn('File name: '+OutFileName);
End;
{$IFDEF StreamType}
ArcFile.Free;
{$ENDIF}
{$IFDEF FileType}
CloseFile(ArcFile);
{$ENDIF}
End;
End;
end.