Spzlib.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:12k
- {*******************************************************************}
- { }
- { Almediadev Visual Component Library }
- { DynamicSkinForm }
- { Version 4.98 }
- { }
- { Copyright (c) 2000-2002 Almediadev }
- { ALL RIGHTS RESERVED }
- { }
- { Home: http://www.almdev.com }
- { Support: support@almdev.com }
- { }
- {*******************************************************************}
- unit spzlib;
- interface
- uses Sysutils, Classes;
- type
- TAlloc = function (AppData: Pointer; Items, Size: Integer): Pointer;
- TFree = procedure (AppData, Block: Pointer);
- TZStreamRec = packed record
- next_in: PChar;
- avail_in: Integer;
- total_in: Integer;
- next_out: PChar;
- avail_out: Integer;
- total_out: Integer;
- msg: PChar;
- internal: Pointer;
- zalloc: TAlloc;
- zfree: TFree;
- AppData: Pointer;
- data_type: Integer;
- adler: Integer;
- reserved: Integer;
- end;
- TCustomZlibStream = class(TStream)
- private
- FStrm: TStream;
- FStrmPos: Integer;
- FOnProgress: TNotifyEvent;
- FZRec: TZStreamRec;
- FBuffer: array [Word] of Char;
- protected
- procedure Progress(Sender: TObject); dynamic;
- property OnProgress: TNotifyEvent read FOnProgress write FOnProgress;
- constructor Create(Strm: TStream);
- end;
- TCompressionLevel = (clNone, clFastest, clDefault, clMax);
- TCompressionStream = class(TCustomZlibStream)
- private
- function GetCompressionRate: Single;
- public
- constructor Create(CompressionLevel: TCompressionLevel; Dest: TStream);
- destructor Destroy; override;
- function Read(var Buffer; Count: Longint): Longint; override;
- function Write(const Buffer; Count: Longint): Longint; override;
- function Seek(Offset: Longint; Origin: Word): Longint; override;
- property CompressionRate: Single read GetCompressionRate;
- property OnProgress;
- end;
- TDecompressionStream = class(TCustomZlibStream)
- public
- constructor Create(Source: TStream);
- destructor Destroy; override;
- function Read(var Buffer; Count: Longint): Longint; override;
- function Write(const Buffer; Count: Longint): Longint; override;
- function Seek(Offset: Longint; Origin: Word): Longint; override;
- property OnProgress;
- end;
- const
- zlib_version = '1.1.4';
- type
- EZlibError = class(Exception);
- ECompressionError = class(EZlibError);
- EDecompressionError = class(EZlibError);
- function adler32(adler: Integer; buf: PChar; len: Integer): Integer;
- procedure CompressStream(Stream, SaveStream: TStream);
- procedure DeCompressStream(Stream, CompressedStream: TStream);
- implementation
- const
- Z_NO_FLUSH = 0;
- Z_PARTIAL_FLUSH = 1;
- Z_SYNC_FLUSH = 2;
- Z_FULL_FLUSH = 3;
- Z_FINISH = 4;
- Z_OK = 0;
- Z_STREAM_END = 1;
- Z_NEED_DICT = 2;
- Z_ERRNO = (-1);
- Z_STREAM_ERROR = (-2);
- Z_DATA_ERROR = (-3);
- Z_MEM_ERROR = (-4);
- Z_BUF_ERROR = (-5);
- Z_VERSION_ERROR = (-6);
- Z_NO_COMPRESSION = 0;
- Z_BEST_SPEED = 1;
- Z_BEST_COMPRESSION = 9;
- Z_DEFAULT_COMPRESSION = (-1);
- Z_FILTERED = 1;
- Z_HUFFMAN_ONLY = 2;
- Z_DEFAULT_STRATEGY = 0;
- Z_BINARY = 0;
- Z_ASCII = 1;
- Z_UNKNOWN = 2;
- Z_DEFLATED = 8;
- _z_errmsg: array[0..9] of PChar = (
- 'need dictionary', // Z_NEED_DICT (2)
- 'stream end', // Z_STREAM_END (1)
- '', // Z_OK (0)
- 'file error', // Z_ERRNO (-1)
- 'stream error', // Z_STREAM_ERROR (-2)
- 'data error', // Z_DATA_ERROR (-3)
- 'insufficient memory', // Z_MEM_ERROR (-4)
- 'buffer error', // Z_BUF_ERROR (-5)
- 'incompatible version', // Z_VERSION_ERROR (-6)
- ''
- );
- {$L deflate.obj}
- {$L inflate.obj}
- {$L inftrees.obj}
- {$L trees.obj}
- {$L adler32.obj}
- {$L infblock.obj}
- {$L infcodes.obj}
- {$L infutil.obj}
- {$L inffast.obj}
- procedure _tr_init; external;
- procedure _tr_tally; external;
- procedure _tr_flush_block; external;
- procedure _tr_align; external;
- procedure _tr_stored_block; external;
- function adler32; external;
- procedure inflate_blocks_new; external;
- procedure inflate_blocks; external;
- procedure inflate_blocks_reset; external;
- procedure inflate_blocks_free; external;
- procedure inflate_set_dictionary; external;
- procedure inflate_trees_bits; external;
- procedure inflate_trees_dynamic; external;
- procedure inflate_trees_fixed; external;
- procedure inflate_codes_new; external;
- procedure inflate_codes; external;
- procedure inflate_codes_free; external;
- procedure _inflate_mask; external;
- procedure inflate_flush; external;
- procedure inflate_fast; external;
- procedure _memset(P: Pointer; B: Byte; count: Integer);cdecl;
- begin
- FillChar(P^, count, B);
- end;
- procedure _memcpy(dest, source: Pointer; count: Integer);cdecl;
- begin
- Move(source^, dest^, count);
- end;
- // deflate compresses data
- function deflateInit_(var strm: TZStreamRec; level: Integer; version: PChar;
- recsize: Integer): Integer; external;
- function deflate(var strm: TZStreamRec; flush: Integer): Integer; external;
- function deflateEnd(var strm: TZStreamRec): Integer; external;
- // inflate decompresses data
- function inflateInit_(var strm: TZStreamRec; version: PChar;
- recsize: Integer): Integer; external;
- function inflate(var strm: TZStreamRec; flush: Integer): Integer; external;
- function inflateEnd(var strm: TZStreamRec): Integer; external;
- function inflateReset(var strm: TZStreamRec): Integer; external;
- function zcalloc(AppData: Pointer; Items, Size: Integer): Pointer;
- begin
- GetMem(Result, Items*Size);
- end;
- procedure zcfree(AppData, Block: Pointer);
- begin
- FreeMem(Block);
- end;
- function CCheck(code: Integer): Integer;
- begin
- Result := code;
- if code < 0 then
- raise ECompressionError.Create('compression error');
- end;
- function DCheck(code: Integer): Integer;
- begin
- Result := code;
- if code < 0 then
- raise EDecompressionError.Create('compression error');
- end;
- constructor TCustomZLibStream.Create(Strm: TStream);
- begin
- inherited Create;
- FStrm := Strm;
- FStrmPos := Strm.Position;
- end;
- procedure TCustomZLibStream.Progress(Sender: TObject);
- begin
- if Assigned(FOnProgress) then FOnProgress(Sender);
- end;
- constructor TCompressionStream.Create(CompressionLevel: TCompressionLevel;
- Dest: TStream);
- const
- Levels: array [TCompressionLevel] of ShortInt =
- (Z_NO_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_BEST_COMPRESSION);
- begin
- inherited Create(Dest);
- FZRec.next_out := FBuffer;
- FZRec.avail_out := sizeof(FBuffer);
- CCheck(deflateInit_(FZRec, Levels[CompressionLevel], zlib_version, sizeof(FZRec)));
- end;
- destructor TCompressionStream.Destroy;
- begin
- FZRec.next_in := nil;
- FZRec.avail_in := 0;
- try
- if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
- while (CCheck(deflate(FZRec, Z_FINISH)) <> Z_STREAM_END)
- and (FZRec.avail_out = 0) do
- begin
- FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
- FZRec.next_out := FBuffer;
- FZRec.avail_out := sizeof(FBuffer);
- end;
- if FZRec.avail_out < sizeof(FBuffer) then
- FStrm.WriteBuffer(FBuffer, sizeof(FBuffer) - FZRec.avail_out);
- finally
- deflateEnd(FZRec);
- end;
- inherited Destroy;
- end;
- function TCompressionStream.Read(var Buffer; Count: Longint): Longint;
- begin
- raise ECompressionError.Create('Invalid stream operation');
- end;
- function TCompressionStream.Write(const Buffer; Count: Longint): Longint;
- begin
- FZRec.next_in := @Buffer;
- FZRec.avail_in := Count;
- if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
- while (FZRec.avail_in > 0) do
- begin
- CCheck(deflate(FZRec, 0));
- if FZRec.avail_out = 0 then
- begin
- FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
- FZRec.next_out := FBuffer;
- FZRec.avail_out := sizeof(FBuffer);
- FStrmPos := FStrm.Position;
- Progress(Self);
- end;
- end;
- Result := Count;
- end;
- function TCompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
- begin
- if (Offset = 0) and (Origin = soFromCurrent) then
- Result := FZRec.total_in
- else
- raise ECompressionError.Create('Invalid stream operation');
- end;
- function TCompressionStream.GetCompressionRate: Single;
- begin
- if FZRec.total_in = 0 then
- Result := 0
- else
- Result := (1.0 - (FZRec.total_out / FZRec.total_in)) * 100.0;
- end;
- constructor TDecompressionStream.Create(Source: TStream);
- begin
- inherited Create(Source);
- FZRec.next_in := FBuffer;
- FZRec.avail_in := 0;
- DCheck(inflateInit_(FZRec, zlib_version, sizeof(FZRec)));
- end;
- destructor TDecompressionStream.Destroy;
- begin
- inflateEnd(FZRec);
- inherited Destroy;
- end;
- function TDecompressionStream.Read(var Buffer; Count: Longint): Longint;
- begin
- FZRec.next_out := @Buffer;
- FZRec.avail_out := Count;
- if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
- while (FZRec.avail_out > 0) do
- begin
- if FZRec.avail_in = 0 then
- begin
- FZRec.avail_in := FStrm.Read(FBuffer, sizeof(FBuffer));
- if FZRec.avail_in = 0 then
- begin
- Result := Count - FZRec.avail_out;
- Exit;
- end;
- FZRec.next_in := FBuffer;
- FStrmPos := FStrm.Position;
- Progress(Self);
- end;
- DCheck(inflate(FZRec, 0));
- end;
- Result := Count;
- end;
- function TDecompressionStream.Write(const Buffer; Count: Longint): Longint;
- begin
- raise EDecompressionError.Create('Invalid stream operation');
- end;
- function TDecompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
- var
- I: Integer;
- Buf: array [0..4095] of Char;
- begin
- if (Offset = 0) and (Origin = soFromBeginning) then
- begin
- DCheck(inflateReset(FZRec));
- FZRec.next_in := FBuffer;
- FZRec.avail_in := 0;
- FStrm.Position := 0;
- FStrmPos := 0;
- end
- else if ( (Offset >= 0) and (Origin = soFromCurrent)) or
- ( ((Offset - FZRec.total_out) > 0) and (Origin = soFromBeginning)) then
- begin
- if Origin = soFromBeginning then Dec(Offset, FZRec.total_out);
- if Offset > 0 then
- begin
- for I := 1 to Offset div sizeof(Buf) do
- ReadBuffer(Buf, sizeof(Buf));
- ReadBuffer(Buf, Offset mod sizeof(Buf));
- end;
- end
- else
- raise EDecompressionError.Create('Invalid stream operation');
- Result := FZRec.total_out;
- end;
- procedure CompressStream(Stream, SaveStream: TStream);
- var
- CompStrm: TCompressionStream;
- begin
- CompStrm := TCompressionStream.Create(clMax, SaveStream);
- try
- Stream.Seek(0, 0);
- CompStrm.CopyFrom(Stream, 0);
- finally
- CompStrm.Free;
- end;
- end;
- procedure DeCompressStream(Stream, CompressedStream: TStream);
- const
- BufSize = 4096;
- var
- Size: LongInt;
- DecompStrm: TDecompressionStream;
- Buf: array[0..BufSize - 1] of Byte;
- begin
- CompressedStream.Seek(0, 0);
- DeCompStrm := TDecompressionStream.Create(CompressedStream);
- try
- while True do
- begin
- Size := DecompStrm.Read(Buf, BufSize);
- if Size <> 0 then Stream.WriteBuffer(Buf, Size) else Break;
- end;
- finally
- DecompStrm.Free;
- end;
- end;
- end.