home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / unity / d5 / JRZIP.ZIP / Zlib / ZIPUTILS.PAS < prev    next >
Pascal/Delphi Source File  |  2000-05-01  |  7KB  |  334 lines

  1. Unit ziputils;
  2.  
  3. { ziputils.pas - IO on .zip files using zlib
  4.   - definitions, declarations and routines used by both
  5.     zip.pas and unzip.pas
  6.     The file IO is implemented here.
  7.  
  8.   based on work by Gilles Vollant
  9.  
  10.   March 23th, 2000,
  11.   Copyright (C) 2000 Jacques Nomssi Nzali }
  12.  
  13. interface
  14.  
  15. {$undef UseStream}
  16. {$ifdef WIN32}
  17.   {$define Delphi}
  18.   {$ifdef UseStream}
  19.     {$define Streams}
  20.   {$endif}
  21. {$endif}
  22.  
  23. uses
  24.   {$ifdef Delphi}
  25.   classes, SysUtils,
  26.   {$endif}
  27.   zutil;
  28.  
  29. { -------------------------------------------------------------- }
  30. {$ifdef Streams}
  31. type
  32.   FILEptr = TFileStream;
  33. {$else}
  34. type
  35.   FILEptr = ^file;
  36. {$endif}
  37. type
  38.   seek_mode = (SEEK_SET, SEEK_CUR, SEEK_END);
  39.   open_mode = (fopenread, fopenwrite, fappendwrite);
  40.  
  41. function fopen(filename : PChar; mode : open_mode) : FILEptr;
  42.  
  43. procedure fclose(fp : FILEptr);
  44.  
  45. function fseek(fp : FILEptr; recPos : uInt; mode : seek_mode) : int;
  46.  
  47. function fread(buf : voidp; recSize : uInt;
  48.                recCount : uInt; fp : FILEptr) : uInt;
  49.  
  50. function fwrite(buf : voidp;  recSize : uInt;
  51.                 recCount : uInt; fp : FILEptr) : uInt;
  52.  
  53. function ftell(fp : FILEptr) : uInt;  { ZIP }
  54.  
  55. function feof(fp : FILEptr) : uInt;   { MiniZIP }
  56.  
  57. { ------------------------------------------------------------------- }
  58.  
  59. type
  60.   zipFile = voidp;
  61.   unzFile = voidp;
  62. type
  63.   z_off_t = long;
  64.  
  65. { tm_zip contain date/time info }
  66. type
  67.   tm_zip = record
  68.      tm_sec : uInt;            { seconds after the minute - [0,59] }
  69.      tm_min : uInt;            { minutes after the hour - [0,59] }
  70.      tm_hour : uInt;           { hours since midnight - [0,23] }
  71.      tm_mday : uInt;           { day of the month - [1,31] }
  72.      tm_mon : uInt;            { months since January - [0,11] }
  73.      tm_year : uInt;           { years - [1980..2044] }
  74.   end;
  75.  
  76.  tm_unz = tm_zip;
  77.  
  78. const
  79.   Z_BUFSIZE = (16384);
  80.   Z_MAXFILENAMEINZIP = (256);
  81.  
  82. const
  83.   CENTRALHEADERMAGIC = $02014b50;
  84.  
  85. const
  86.   SIZECENTRALDIRITEM = $2e;
  87.   SIZEZIPLOCALHEADER = $1e;
  88.  
  89. function ALLOC(size : int) : voidp;
  90.  
  91. procedure TRYFREE(p : voidp);
  92.  
  93. const
  94.   Paszip_copyright : PChar = ' Paszip Copyright 2000 Jacques Nomssi Nzali ';
  95.  
  96. implementation
  97.  
  98. function ALLOC(size : int) : voidp;
  99. begin
  100.   ALLOC := zcalloc (NIL, size, 1);
  101. end;
  102.  
  103. procedure TRYFREE(p : voidp);
  104. begin
  105.   if Assigned(p) then
  106.     zcfree(NIL, p);
  107. end;
  108.  
  109. {$ifdef Streams}
  110. { ---------------------------------------------------------------- }
  111.  
  112. function fopen(filename : PChar; mode : open_mode) : FILEptr;
  113. var
  114.   fp : FILEptr;
  115. begin
  116.   fp := NIL;
  117.   try
  118.     Case mode of
  119.     fopenread: fp := TFileStream.Create(filename, fmOpenRead);
  120.     fopenwrite: fp := TFileStream.Create(filename, fmCreate);
  121.     fappendwrite :
  122.       begin
  123.         fp := TFileStream.Create(filename, fmOpenReadWrite);
  124.         fp.Seek(soFromEnd, 0);
  125.       end;
  126.     end;
  127.   except
  128.     on EFOpenError do
  129.       fp := NIL;
  130.   end;
  131.   fopen := fp;
  132. end;
  133.  
  134. procedure fclose(fp : FILEptr);
  135. begin
  136.   fp.Free;
  137. end;
  138.  
  139. function fread(buf : voidp;
  140.                recSize : uInt;
  141.                recCount : uInt;
  142.                fp : FILEptr) : uInt;
  143. var
  144.   totalSize, readcount : uInt;
  145. begin
  146.   if Assigned(buf) then
  147.   begin
  148.     totalSize := recCount * uInt(recSize);
  149.     readCount := fp.Read(buf^, totalSize);
  150.     if (readcount <> totalSize) then
  151.       fread := readcount div recSize
  152.     else
  153.       fread := recCount;
  154.   end
  155.   else
  156.     fread := 0;
  157. end;
  158.  
  159. function fwrite(buf : voidp;
  160.                 recSize : uInt;
  161.                 recCount : uInt;
  162.                 fp : FILEptr) : uInt;
  163. var
  164.   totalSize, written : uInt;
  165. begin
  166.   if Assigned(buf) then
  167.   begin
  168.     totalSize := recCount * uInt(recSize);
  169.     written := fp.Write(buf^, totalSize);
  170.     if (written <> totalSize) then
  171.       fwrite := written div recSize
  172.     else
  173.       fwrite := recCount;
  174.   end
  175.   else
  176.     fwrite := 0;
  177. end;
  178.  
  179. function fseek(fp : FILEptr;
  180.                recPos : uInt;
  181.                mode : seek_mode) : int;
  182. const
  183.   fsmode : array[seek_mode] of Word
  184.     = (soFromBeginning, soFromCurrent, soFromEnd);
  185. begin
  186.   fp.Seek(recPos, fsmode[mode]);
  187.   fseek := 0; { = 0 for success }
  188. end;
  189.  
  190. function ftell(fp : FILEptr) : uInt;
  191. begin
  192.   ftell := fp.Position;
  193. end;
  194.  
  195. function feof(fp : FILEptr) : uInt;
  196. begin
  197.   feof := 0;
  198.   if Assigned(fp) then
  199.     if fp.Position = fp.Size then
  200.       feof := 1
  201.     else
  202.       feof := 0;
  203. end;
  204.  
  205. {$else}
  206. { ---------------------------------------------------------------- }
  207.  
  208. function fopen(filename : PChar; mode : open_mode) : FILEptr;
  209. var
  210.   fp : FILEptr;
  211.   OldFileMode : byte;
  212. begin
  213.   fp := NIL;
  214.   OldFileMode := FileMode;
  215.  
  216.   GetMem(fp, SizeOf(file));
  217.   Assign(fp^, filename);
  218.   {$i-}
  219.   Case mode of
  220.   fopenread:
  221.     begin
  222.       FileMode := 0;
  223.       Reset(fp^, 1);
  224.     end;
  225.   fopenwrite:
  226.     begin
  227.       FileMode := 1;
  228.       ReWrite(fp^, 1);
  229.     end;
  230.   fappendwrite :
  231.     begin
  232.       FileMode := 2;
  233.       Reset(fp^, 1);
  234.       Seek(fp^, FileSize(fp^));
  235.     end;
  236.   end;
  237.   FileMode := OldFileMode;
  238.   if IOresult<>0 then
  239.   begin
  240.     FreeMem(fp, SizeOf(file));
  241.     fp := NIL;
  242.   end;
  243.  
  244.   fopen := fp;
  245. end;
  246.  
  247. procedure fclose(fp : FILEptr);
  248. begin
  249.   if Assigned(fp) then
  250.   begin
  251.     {$i-}
  252.     system.close(fp^);
  253.     if IOresult=0 then;
  254.     FreeMem(fp, SizeOf(file));
  255.   end;
  256. end;
  257.  
  258. function fread(buf : voidp;
  259.                recSize : uInt;
  260.                recCount : uInt;
  261.                fp : FILEptr) : uInt;
  262. var
  263.   totalSize, readcount : uInt;
  264. begin
  265.   if Assigned(buf) then
  266.   begin
  267.     totalSize := recCount * uInt(recSize);
  268.     {$i-}
  269.     system.BlockRead(fp^, buf^, totalSize, readcount);
  270.     if (readcount <> totalSize) then
  271.       fread := readcount div recSize
  272.     else
  273.       fread := recCount;
  274.   end
  275.   else
  276.     fread := 0;
  277. end;
  278.  
  279. function fwrite(buf : voidp;
  280.                 recSize : uInt;
  281.                 recCount : uInt;
  282.                 fp : FILEptr) : uInt;
  283. var
  284.   totalSize, written : uInt;
  285. begin
  286.   if Assigned(buf) then
  287.   begin
  288.     totalSize := recCount * uInt(recSize);
  289.     {$i-}
  290.     system.BlockWrite(fp^, buf^, totalSize, written);
  291.     if (written <> totalSize) then
  292.       fwrite := written div recSize
  293.     else
  294.       fwrite := recCount;
  295.   end
  296.   else
  297.     fwrite := 0;
  298. end;
  299.  
  300. function fseek(fp : FILEptr;
  301.                recPos : uInt;
  302.                mode : seek_mode) : int;
  303. begin
  304.   {$i-}
  305.   case mode of
  306.     SEEK_SET : system.Seek(fp^, recPos);
  307.     {$WARNINGS OFF}
  308.     SEEK_CUR : system.Seek(fp^, FilePos(fp^)+recPos);
  309.     SEEK_END : system.Seek(fp^, FileSize(fp^)-1-recPos); { ?? check }
  310.     {$WARNINGS ON}
  311.   end;
  312.   fseek := IOresult; { = 0 for success }
  313. end;
  314.  
  315. function ftell(fp : FILEptr) : uInt;
  316. begin
  317.   ftell := FilePos(fp^);
  318. end;
  319.  
  320. function feof(fp : FILEptr) : uInt;
  321. begin
  322.   feof := 0;
  323.   if Assigned(fp) then
  324.     if eof(fp^) then
  325.       feof := 1
  326.     else
  327.       feof := 0;
  328. end;
  329.  
  330. {$endif}
  331. { ---------------------------------------------------------------- }
  332.  
  333. end.
  334.