ziputils.pas 6.2 KB

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