program pipes APPTYPE CONSOLE uses Windows SysUtils function gzdopen f

 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
program pipes;
{$APPTYPE CONSOLE}
uses
Windows,
SysUtils;
function gzdopen(fd: Cardinal; mode: PAnsiChar): Pointer; cdecl; external 'zlib1.dll';
function gzclose(gzFile: Pointer): Integer; cdecl; external 'zlib1.dll';
function gzwrite(gzFile: Pointer; buf: PAnsiChar; len: Integer): Integer; cdecl; external 'zlib1.dll';
function _open_osfhandle(osfhandle: Cardinal; flags: Integer): Integer; cdecl; external 'msvcrt.dll';
var
hRead: Cardinal;
hWrite: Cardinal;
hGz: Pointer;
gzBuf: array [1..4096] of Char;
nRead, nWrite: Cardinal;
Output: file;
hpipe: array[0..1] of integer;
SomeLongString: String;
begin
SomeLongString := '<?xml and so on';
CreatePipe(hRead, hWrite, nil, 256);
hpipe[0] := _open_osfhandle(hRead, $8000);
hpipe[1] := _open_osfhandle(hWrite, $8000);
hGz := gzdopen(hpipe[1], 'wb');
nWrite := gzwrite(hGz, PAnsiChar(SomeLongString), Length(SomeLongString));
gzclose(hGz);
ReadFile(hRead, gzBuf[1], 4096, nRead, nil);
AssignFile(Output, 'compressed.gz');
Rewrite(Output, 1);
BlockWrite(Output, gzBuf[1], nRead);
CloseFile(Output);
WriteLn(Format('%d bytes written', [nWrite]));
WriteLn(Format('%d bytes read', [nRead]));
end.