Huffman Compression Engine
LHA/LZH is a freeware compression utility and associated file format

Delphi & Pascal (česká wiki)
Přejít na: navigace, hledání
Kategorija: KMP (Programy mladňakoch

Program: Lzh.pas
Subor exe: Lzh.exe
Mušiš mac: Lzhasm.objLz.pasLzo.pasTestlzo.pasTplzh.docLzh.zip

Huffman Compression Engine
LHA/LZH is a freeware compression utility and associated file format. It was created in 1988 by Haruyasu Yoshizaki (Yoshizaki Haruyasu?), and originally named LHarc. A complete rewrite of LHarc, tentatively named LHx, was eventually released as LH. It was then renamed to LHA to avoid conflicting with the then-new MS-DOS 5.0 LH ("load high") command. According to early documentation, LHA is pronounced like La.
LHA/LZH on wikipedia
{ TESTLZO.PAS                                                          }
{ see: http://en.wikipedia.org/wiki/LHA_(file_format)#Canonical_LZH    }
{                                                                      }
{ Datum: 31.07.1993                              http://www.trsek.com  }
 
{$A+,B-,D+,E+,F+,I-,L-,N-,O-,R-,S-,V-}
{$M 2048,60000,60000}
 
program LZOTest;
 
uses
  LZO;
 
type TFileHuff = Object (THuff)
       infile, outfile: file;
       counter: longint;
       constructor Init (infname, outfname: String);
       destructor  Done; virtual;
       function    ReadBuf (var data; size: word): longint; virtual;
       function    WriteBuf (var data; size: word): longint; virtual;
     END;
 
 
constructor TFileHuff.Init (infname, outfname: String);
begin
  THuff.Init;
  Assign (infile, infname);
  Reset (infile, 1);
  if IoResult > 0 then begin
    Writeln ('! Can''t open input file'); Halt (1)
  end;
  Assign (outfile, outfname);
  Rewrite (outfile, 1);
  if IoResult > 0 then begin
    Writeln ('! Can''t open input file'); Halt (1)
  end;
  counter := 0
end;
 
destructor TFileHuff.Done;
begin
  Close (infile);
  if IoResult>0 then begin
    Writeln ('! Error closing input file'); Halt (1)
  end;
  Close (outfile);
  if IoResult>0 then begin
    Writeln ('! Error closing output file'); Halt (1)
  end;
end;
 
function TFileHuff.ReadBuf (var data; size: word): longint;
var rd: word;
begin
  BlockRead (infile, data, size, rd);
  if IOResult > 0 then
    ReadBuf := -1
  else
    ReadBuf := rd;
  if Compressing then begin
    inc (counter, rd);
    Write (counter, #13)
  end;
end;
 
function TFileHuff.WriteBuf (var data; size: word): longint;
var wr: word;
begin
  BlockWrite (outfile, data, size, wr);
  if IOResult > 0 then
    WriteBuf := -1
  else
    WriteBuf := wr;
  if not Compressing then begin
    inc (counter, wr);
    Write (counter, #13)
  end;
end;
 
var
  s: String;
  test: Word;
  RData: Byte;
  FileHuff: TFileHuff;
  fsize,
  compr: longint;
 
begin
  Writeln ('Huffman Compression Engine v', EngineVer);
  if ParamCount <> 3 then begin
    Writeln ('Usage: testlzo e(ncode)|d(ecode) infile outfile');
    Halt (1);
  end;
 
  FileHuff.Init (ParamStr (2), ParamStr (3));
 
  s := ParamStr(1);
  case s[1] of
   'e','E': Begin
              fsize := FileSize (FileHuff.infile);
              Writeln (fsize);
              compr := FileHuff.Compress (fsize);
 
              Writeln ('input:  ', fsize, ' bytes');
              Writeln ('output: ', compr, ' bytes');
              Writeln ('relative output: ', compr*100 div fsize, '%');
            End;
   'd','D': FileHuff.Expand
   else begin
          Writeln ('! Use [D] for Decompression or [E] for Compression');
          Halt (1)
        end;
  end;
  FileHuff.Done;
end.