| 1 |
{ |
| 2 |
$Id: zstream.pp,v 1.6 2005/02/14 17:13:15 peter Exp $ |
| 3 |
This file is part of the Free Pascal run time library. |
| 4 |
Copyright (c) 1999-2000 by the Free Pascal development team |
| 5 |
|
| 6 |
Implementation of compression streams. |
| 7 |
|
| 8 |
This file is adapted from the FPC RTL source code, as such |
| 9 |
the license and copyright information of FPC RTL applies here. |
| 10 |
That said, the license of FPC RTL happens to be *exactly* |
| 11 |
the same as used by the "Castle Game Engine": LGPL (version 2.1) |
| 12 |
with "static linking exception" (with exactly the same wording |
| 13 |
of the "static linking exception"). |
| 14 |
See the file COPYING.txt, included in this distribution, for details about |
| 15 |
the copyright of "Castle Game Engine". |
| 16 |
See http://www.freepascal.org/faq.var#general-license about the copyright |
| 17 |
of FPC RTL. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 22 |
|
| 23 |
**********************************************************************} |
| 24 |
|
| 25 |
{ Streams using zlib compression. |
| 26 |
|
| 27 |
This is adjusted from FPC sources unit fcl/inc/zstream.pp, |
| 28 |
- to use our CastleInternalGzio unit. |
| 29 |
- it is cut down to TGZFileStream, the only class useful for reading/writing gz files. |
| 30 |
- works on Delphi too. } |
| 31 |
unit CastleInternalZStream; |
| 32 |
|
| 33 |
{$I castleconf.inc} |
| 34 |
|
| 35 |
interface |
| 36 |
|
| 37 |
uses |
| 38 |
Sysutils, Classes |
| 39 |
{$ifdef FPC} , CastleInternalGzio {$else}, Zlib {$endif}; |
| 40 |
|
| 41 |
type |
| 42 |
EZlibError = class(EStreamError); |
| 43 |
|
| 44 |
TGZFileStream = |
| 45 |
{$ifdef FPC} |
| 46 |
class(TOwnerStream) |
| 47 |
private |
| 48 |
FWriteMode : boolean; |
| 49 |
FFIle : gzfile; |
| 50 |
public |
| 51 |
Constructor Create(const Stream: TStream; const AWriteMode: boolean); |
| 52 |
Destructor Destroy;override; |
| 53 |
Function Read(Var Buffer; Count : longint): longint;override; |
| 54 |
function Write(const Buffer; Count: Longint): Longint; override; |
| 55 |
function Seek(Offset: Longint; Origin: Word): Longint; override; |
| 56 |
{$else} |
| 57 |
class(TDecompressionStream) |
| 58 |
constructor Create(const Stream: TStream; const AWriteMode: boolean); |
| 59 |
{$endif} |
| 60 |
end; |
| 61 |
|
| 62 |
implementation |
| 63 |
|
| 64 |
{$ifdef FPC} |
| 65 |
|
| 66 |
Const |
| 67 |
SReadOnlyStream = 'Decompression streams are read-only'; |
| 68 |
SWriteOnlyStream = 'Compression streams are write-only'; |
| 69 |
SSeekError = 'Compression stream seek error'; |
| 70 |
|
| 71 |
// TGZFileStream |
| 72 |
|
| 73 |
Constructor TGZFileStream.Create(const Stream: TStream; const AWriteMode: boolean); |
| 74 |
begin |
| 75 |
inherited Create(Stream); |
| 76 |
SourceOwner := true; |
| 77 |
FWriteMode := AWriteMode; |
| 78 |
FFile:=gzopen (Stream, AWriteMode); |
| 79 |
end; |
| 80 |
|
| 81 |
Destructor TGZFileStream.Destroy; |
| 82 |
begin |
| 83 |
If FFile <> nil then |
| 84 |
gzclose(FFile); |
| 85 |
Inherited Destroy; |
| 86 |
end; |
| 87 |
|
| 88 |
Function TGZFileStream.Read(Var Buffer; Count : longint): longint; |
| 89 |
begin |
| 90 |
If FWriteMode then |
| 91 |
Raise ezliberror.create(SWriteOnlyStream); |
| 92 |
Result:=gzRead(FFile,@Buffer,Count); |
| 93 |
if Result < 0 then |
| 94 |
raise EZlibError.Create('Gzip decompression error: ' + gzerror(FFile)); |
| 95 |
end; |
| 96 |
|
| 97 |
function TGZFileStream.Write(const Buffer; Count: Longint): Longint; |
| 98 |
begin |
| 99 |
If not FWriteMode then |
| 100 |
Raise EzlibError.Create(SReadonlyStream); |
| 101 |
Result:=gzWrite(FFile,@Buffer,Count); |
| 102 |
if Result < 0 then |
| 103 |
raise EZlibError.Create('Gzip compression error: ' + gzerror(FFile)); |
| 104 |
end; |
| 105 |
|
| 106 |
function TGZFileStream.Seek(Offset: Longint; Origin: Word): Longint; |
| 107 |
begin |
| 108 |
Result:=gzseek(FFile,Offset,Origin); |
| 109 |
If Result=-1 then |
| 110 |
Raise eZlibError.Create(SSeekError); |
| 111 |
end; |
| 112 |
|
| 113 |
{$else} |
| 114 |
|
| 115 |
constructor TGZFileStream.Create(const Stream: TStream; const AWriteMode: boolean); |
| 116 |
begin |
| 117 |
if AWriteMode then |
| 118 |
raise EZlibError.Create('Writing gzip data on Delphi not implemented yet'); |
| 119 |
|
| 120 |
inherited Create(Stream, |
| 121 |
{ Simple way to read gzip-compressed data with Delphi, |
| 122 |
see https://stackoverflow.com/questions/31221429/delphi-xe-8-how-to-decompress-a-gzip-file . |
| 123 |
31 bit wide window means that we require GZIP header (and not just Zlib header). |
| 124 |
} |
| 125 |
15 + 16, |
| 126 |
{ OwnsStream = true, to behave just like TGZFileStream that descends |
| 127 |
from TOwnerStream. } |
| 128 |
true); |
| 129 |
end; |
| 130 |
|
| 131 |
{$endif} |
| 132 |
|
| 133 |
end. |