home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / ARCUNIT.ZIP / ARCHIVE.PAS
Encoding:
Pascal/Delphi Source File  |  1989-11-05  |  5.4 KB  |  175 lines

  1. {$I-,V-,S-,R-,D-}
  2.      {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  3.      {                         Glowing Spark Software                     }
  4.      {                            Name: Archive                           }
  5.      { Author: Moshe Boochbut                                Version: 1.0 }
  6.      { Last update: 12-Sep-1988                                           }
  7.      {____________________________________________________________________}
  8. Unit Archive;
  9.  
  10. INTERFACE
  11.  
  12. Const
  13.   StoreMathod = 1;                   {The store mathod code         }
  14.   ArchiveMark = $1A;                 {The Archive mark (^Z)         }
  15.  
  16. Type
  17.   Asciiz     = Array [1..13] Of Byte;
  18.   ArcInfo    = Record
  19.                  ArcMark      : Byte;       {The Arc Mark, always 1A (Hex)      }
  20.                  CompressType : Byte;       {Compressing mathod code (0=Stored) }
  21.                  FileName     : Asciiz;     {The file name in the arc (asciiz)  }
  22.                  CompressSize : LongInt;    {The space file occupy (in Bytes)   }
  23.                  FileDate,                  {DOS Coded date                     }
  24.                  FileTime,                  {    and time                       }
  25.                  CRCword      : Word;       {CRC check word                     }
  26.                  RealSize,                  {The real file size after unarcing  }
  27.                  FoundAt,                   {The file position in archive       }
  28.                  NextFileAt   : LongInt;    {The position of next file          }
  29.                  FileNameStr  : String[12]; {The file name in String format     }
  30.                  FullName     : String[70]; {Archive name (by ArcFindFirst)     }
  31.                end; {Record}
  32.  
  33. var
  34.   ArcOK          : Boolean;
  35.  
  36.  
  37. IMPLEMENTATION
  38.  
  39. function LenAsc(A : Asciiz) : Word;
  40.   InLine
  41.    ($5F/                     {pop  di       ;get pointer to ASCIIZ}
  42.     $07/                     {pop  es       ; into es:di}
  43.     $89/$FB/                 {mov  bx,di    ;store initial offset}
  44.     $B9/$FF/$FF/             {mov  cx,$FFFF ;check maximum length}
  45.     $B0/$00/                 {mov  al,0     ;look for null}
  46.     $FC/                     {cld           ;forward direction}
  47.     $F2/                     {repne}
  48.     $AE/                     {scasb         ;scan while equal}
  49.     $29/$DF/                 {sub  di,bx    ;get the number of bytes scanned}
  50.     $89/$F8/                 {mov  ax,di    ;return in ax}
  51.     $48);                    {dec  ax       ;null doesn't count}
  52.  
  53. function PadCh (S : String; Ch : Char; n : Byte): String;
  54. var
  55.   St   : String;
  56.   i    : Byte;
  57. begin
  58.   St := S;
  59.   for i := Length (S) to n do
  60.     St := St + Ch;
  61.   PadCh := St;
  62. end; {PadCh}
  63.  
  64. function Asc2Str(var A : Asciiz) : string;
  65. var
  66.    S : string;
  67.    Len : byte;
  68. begin
  69.    Len := lo(LenAsc(A));
  70.    S[0] := Char(Len);
  71.    Move(A, S[1], Len);
  72.    Asc2Str := S;
  73. end; {Asc2Str}
  74.  
  75.  
  76.  
  77. procedure ListArc;
  78. var
  79.   ArcFile       : File;
  80.   NumRead       : Integer;
  81. begin
  82.   Assign (ArcFile, ArchiveInfo.FullName);
  83.   Reset (ArcFile, 1);
  84.   FoundAt := 0;
  85.   NextFileAt := 0;
  86.   while not eof(ArcFile) do
  87.   begin
  88.     Seek (ArcFile, ArchiveInfo.NextFileAt);
  89.     BlockRead (ArcFile, ArchiveInfo, 25, NumRead);
  90.     if (ArchiveInfo.ArcMark = ArchiveMark) and (NumRead = 25) Then
  91.     with ArchiveInfo do
  92.     begin
  93.       FoundAt := FilePos (ArcFile) - 25;
  94.       FileNameStr := Asc2Str (ArchiveInfo.FileName);
  95.       if CompressType = StoreMathod Then
  96.         RealSize := CompressSize
  97.       else 
  98.       begin
  99.         BlockRead (ArcFile, RealSize, 4, NumRead);
  100.         Inc (NextFileAt, 4);
  101.       end;
  102.       Inc (NextFileAt, CompressSize + 25);
  103.     end; {with}
  104.       Close (ArcFile);
  105.     end; {if IOResult}
  106.   end; {if IOResult}
  107. end; {ArcGetNext}
  108.  
  109.  
  110. procedure ArcGetFirst (ArchiveName :String; var ArchiveInfo : ArcInfo);
  111. begin
  112.   with ArchiveInfo do
  113.   begin
  114.   end; {with}
  115.   ArcGetNext (ArchiveInfo);
  116. end; {ArcGetFirst}
  117.  
  118.  
  119. procedure ArcCopy (SrcArc : ArcInfo; DstFileName : String);
  120. Const
  121.   EndOfArc : Word = $001A;
  122.  
  123. var
  124.   ArcFile,
  125.   DstFile       : File;
  126.   NumWritten,
  127.   NumRead,
  128.   NumToRead     : Word;
  129.   LeftToCopy    : LongInt;
  130.   CopyBuffer    : Array [0..4096] Of Byte;
  131.  
  132. begin
  133.   ArcOK := False;
  134.   Assign (ArcFile, SrcArc.FullName);
  135.   Reset (ArcFile, 1);
  136.   if IOResult = 0 Then
  137.   begin
  138.     Assign (DstFile, DstFileName);
  139.     Reset (DstFile, 1);
  140.     if IoResult <> 0 Then
  141.       Rewrite (DstFile, 1)
  142.     Else
  143.       Seek (DstFile, FileSize (DstFile) - 2);
  144.     if IOResult = 0 Then
  145.     begin
  146.       Seek (Arcfile, SrcArc.FoundAt);
  147.       if IOResult = 0 Then
  148.       begin
  149.         LeftToCopy := SrcArc.NextFileAt - SrcArc.FoundAt;
  150.         Repeat
  151.           if LeftToCopy > SizeOf (CopyBuffer) Then
  152.             NumToRead := SizeOf (CopyBuffer)
  153.           Else
  154.             NumToRead := LeftToCopy;
  155.           Dec (LeftToCopy, NumToRead);
  156.           BlockRead (ArcFile, CopyBuffer, NumToRead, NumRead);
  157.           BlockWrite (DstFile, CopyBuffer, NumRead, NumWritten);
  158.         Until (NumToRead <> NumWritten) Or (LeftToCopy = 0);
  159.         if LeftToCopy = 0 Then
  160.         begin
  161.           BlockWrite (DstFile, EndOfArc, 2, NumWritten);
  162.           if NumWritten = 2 Then
  163.             ArcOK := True;
  164.         end; {if LeftToCopy}
  165.         Close (ArcFile);
  166.         Close (DstFile);
  167.       end; {if IoResult}
  168.     end; {if IoResult}
  169.   end; {if IoResult}
  170. end; {ArcCopy}
  171.  
  172. begin
  173.   ArcOk := True;
  174. end.
  175.