ziputils.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. {$push}{$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. {$pop}
  175. if IOresult<>0 then
  176. begin
  177. FreeMem(fp, SizeOf(file));
  178. fp := NIL;
  179. end;
  180. fopen := fp;
  181. end;
  182. procedure fclose(fp : FILEptr);
  183. begin
  184. if Assigned(fp) then
  185. begin
  186. {$push}{$i-}
  187. system.close(fp^);
  188. {$pop}
  189. if IOresult=0 then;
  190. FreeMem(fp, SizeOf(file));
  191. end;
  192. end;
  193. function fread(buf : pointer;
  194. recSize : LongInt;
  195. recCount : LongInt;
  196. fp : FILEptr) : LongInt;
  197. var
  198. totalSize, readcount : LongInt;
  199. begin
  200. if Assigned(buf) then
  201. begin
  202. totalSize := recCount * LongInt(recSize);
  203. {$push}{$i-}
  204. system.BlockRead(fp^, buf^, totalSize, readcount);
  205. if (readcount <> totalSize) then
  206. fread := readcount div recSize
  207. else
  208. fread := recCount;
  209. {$pop}
  210. end
  211. else
  212. fread := 0;
  213. end;
  214. function fwrite(buf : pointer;
  215. recSize : LongInt;
  216. recCount : LongInt;
  217. fp : FILEptr) : LongInt;
  218. var
  219. totalSize, written : LongInt;
  220. begin
  221. if Assigned(buf) then
  222. begin
  223. totalSize := recCount * LongInt(recSize);
  224. {$push}{$i-}
  225. system.BlockWrite(fp^, buf^, totalSize, written);
  226. if (written <> totalSize) then
  227. fwrite := written div recSize
  228. else
  229. fwrite := recCount;
  230. {$pop}
  231. end
  232. else
  233. fwrite := 0;
  234. end;
  235. function fseek(fp : FILEptr;
  236. recPos : LongInt;
  237. mode : seek_mode) : longint;
  238. begin
  239. {$push}{$i-}
  240. case mode of
  241. SEEK_SET : system.Seek(fp^, recPos);
  242. SEEK_CUR : system.Seek(fp^, FilePos(fp^)+recPos);
  243. SEEK_END : system.Seek(fp^, FileSize(fp^)-1-recPos); { ?? check }
  244. end;
  245. {$pop}
  246. fseek := IOresult; { = 0 for success }
  247. end;
  248. function ftell(fp : FILEptr) : LongInt;
  249. begin
  250. ftell := FilePos(fp^);
  251. end;
  252. function feof(fp : FILEptr) : LongInt;
  253. begin
  254. feof := 0;
  255. if Assigned(fp) then
  256. if eof(fp^) then
  257. feof := 1
  258. else
  259. feof := 0;
  260. end;
  261. {$endif}
  262. { ---------------------------------------------------------------- }
  263. end.