home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_prog / stream11.arj / COMPRESS.PAS next >
Pascal/Delphi Source File  |  1992-03-27  |  2KB  |  71 lines

  1. program Compress;
  2.  
  3. { Program to demonstrate use of TLZWFilter }
  4.  
  5. uses
  6.   objects,streams;
  7.  
  8. procedure SyntaxExit(s:string);
  9. begin
  10.   writeln;
  11.   writeln(s);
  12.   writeln;
  13.   writeln('Usage:  COMPRESS Sourcefile Destfile [/X]');
  14.   writeln(' will compress the source file to the destination');
  15.   writeln(' file, or if /X flag is used, will expand source to destination.');
  16.   halt(99);
  17. end;
  18.  
  19. var
  20.   Source : PStream;   { We don't know in advance which will be compressed }
  21.   Dest   : PStream;
  22.  
  23. begin
  24.   Case ParamCount of
  25.     2 : begin
  26.           Source := New(PBufStream, init(Paramstr(1), stOpenRead, 2048));
  27.  
  28.           Dest   := New(PLZWFilter, init(New(PBufStream,
  29.                                              init(Paramstr(2),
  30.                                                   stCreate, 2048)),
  31.                                          stOpenWrite));
  32.           Write('Compressing ',Paramstr(1),' (',Source^.GetSize,
  33.                 ' bytes) to ',Paramstr(2));
  34.         end;
  35.     3 : begin
  36.           if (Paramstr(3) <> '/X') and (Paramstr(3) <> '/x') then
  37.             SyntaxExit('Unrecognized option '+Paramstr(3));
  38.           Source := New(PLZWFilter, init(New(PBufStream,
  39.                                              init(Paramstr(1),
  40.                                                   stOpenRead, 2048)),
  41.                                          stOpenRead));
  42.           Dest   := New(PBufStream, init(Paramstr(2), stCreate, 2048));
  43.           Write('Expanding ',Paramstr(1),' (',
  44.                 PLZWFilter(Source)^.Base^.GetSize,' bytes) to ',
  45.                 Paramstr(2));
  46.         end;
  47.     else
  48.       SyntaxExit('Two or three parameters required.');
  49.   end;
  50.  
  51.   if (Source = nil) or (Source^.status <> stOk) then
  52.     SyntaxExit('Unable to open file '+ParamStr(1)+' for reading.');
  53.  
  54.   if (Dest = nil) or (Dest^.status <> stOk) then
  55.     SyntaxExit('Unable to create file '+Paramstr(2)+'.');
  56.  
  57.   Dest^.CopyFrom(Source^, Source^.GetSize);
  58.   if Dest^.status <> stOK then
  59.     SyntaxExit('File error during compression/expansion.');
  60.  
  61.   Case ParamCount of
  62.     2 : begin
  63.           Dest^.Flush;
  64.           Writeln(' (',PLZWFilter(Dest)^.Base^.GetSize,' bytes).');
  65.         end;
  66.     3 : Writeln(' (',Dest^.GetSize,' bytes).');
  67.   end;
  68.  
  69.   Dispose(Source, done);
  70.   Dispose(Dest, done);
  71. end.