| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581 |
- {$IFNDEF FPC_DOTTEDUNITS}
- unit Unzip;
- {$ENDIF FPC_DOTTEDUNITS}
- {$mode tp}
- { ----------------------------------------------------------------- }
- { unzip.c -- IO on .zip files using zlib
- Version 0.15 beta, Mar 19th, 1998,
- unzip.h -- IO for uncompress .zip files using zlib
- Version 0.15 beta, Mar 19th, 1998,
- Copyright (C) 1998 Gilles Vollant <[email protected]>
- http://www.winimage.com/zLibDll/zip.htm
- This unzip package allow extract file from .ZIP file, compatible
- with PKZip 2.04g, WinZip, InfoZip tools and compatible.
- Encryption and multi volume ZipFile (span) are not supported.
- Old compressions used by old PKZip 1.x are not supported
- Pascal tranlastion
- Copyright (C) 2000 by Jacques Nomssi Nzali
- For conditions of distribution and use, see copyright notice in readme.txt }
- interface
- {$ifdef WIN32}
- {$define Delphi}
- {$endif}
- {$IFDEF FPC_DOTTEDUNITS}
- uses
- //zutil,
- System.ZLib.Zbase,
- //Api.Z,
- System.ZLib.Ziputils;
- {$ELSE FPC_DOTTEDUNITS}
- uses
- //zutil,
- zbase,
- //zLib,
- ziputils;
- {$ENDIF FPC_DOTTEDUNITS}
- const
- UNZ_OK = (0);
- UNZ_END_OF_LIST_OF_FILE = (-100);
- UNZ_ERRNO = (Z_ERRNO);
- UNZ_EOF = (0);
- UNZ_PARAMERROR = (-102);
- UNZ_BADZIPFILE = (-103);
- UNZ_INTERNALERROR = (-104);
- UNZ_CRCERROR = (-105);
- (*
- { tm_unz contain date/time info }
- type
- tm_unz = record
- tm_sec : integer; { seconds after the minute - [0,59] }
- tm_min : integer; { minutes after the hour - [0,59] }
- tm_hour : integer; { hours since midnight - [0,23] }
- tm_mday : integer; { day of the month - [1,31] }
- tm_mon : integer; { months since January - [0,11] }
- tm_year : integer; { years - [1980..2044] }
- end;
- *)
- { unz_global_info structure contain global data about the ZIPfile
- These data comes from the end of central dir }
- type
- unz_global_info = record
- number_entry: longint; { total number of entries in
- the central dir on this disk }
- size_comment: longint; { size of the global comment of the zipfile }
- end;
- { unz_file_info contain information about a file in the zipfile }
- type
- unz_file_info = record
- version: longint; { version made by 2 bytes }
- version_needed: longint; { version needed to extract 2 bytes }
- flag: longint; { general purpose bit flag 2 bytes }
- compression_method: longint; { compression method 2 bytes }
- dosDate: longint; { last mod file date in Dos fmt 4 bytes }
- crc: longint; { crc-32 4 bytes }
- compressed_size: longint; { compressed size 4 bytes }
- uncompressed_size: longint; { uncompressed size 4 bytes }
- size_filename: longint; { filename length 2 bytes }
- size_file_extra: longint; { extra field length 2 bytes }
- size_file_comment: longint; { file comment length 2 bytes }
- disk_num_start: longint; { disk number start 2 bytes }
- internal_fa: longint; { internal file attributes 2 bytes }
- external_fa: longint; { external file attributes 4 bytes }
- tmu_date: tm_unz;
- end;
- unz_file_info_ptr = ^unz_file_info;
- function unzStringFileNameCompare(const fileName1: PAnsiChar; const fileName2: PAnsiChar; iCaseSensitivity: longint): longint;
- { Compare two filename (fileName1,fileName2).
- If iCaseSenisivity = 1 (1=true),
- comparision is case sensitive (like strcmp)
- If iCaseSenisivity = 2 (0=false),
- comparision is not case sensitive (like strcmpi or strcasecmp)
- If iCaseSenisivity = 0, case sensitivity is defaut of your
- operating system like 1 on Unix, 2 on Windows)
- }
- function unzOpen(const path: PAnsiChar): unzFile;
- { Open a Zip file. path contain the full pathname (by example,
- on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer
- "zlib/zlib111.zip".
- If the zipfile cannot be opened (file don't exist or in not valid), the
- return value is NIL.
- Else, the return value is a unzFile Handle, usable with other function
- of this unzip package.
- }
- function unzClose(afile: unzFile): longint;
- { Close a ZipFile opened with unzipOpen.
- If there are files inside the .Zip opened with unzOpenCurrentFile()
- (see later), these files MUST be closed with unzipCloseCurrentFile()
- before a call unzipClose.
- return UNZ_OK if there is no problem. }
- function unzGetGlobalInfo(afile: unzFile; var pglobal_info: unz_global_info): longint;
- { Write info about the ZipFile in the *pglobal_info structure.
- No preparation of the structure is needed
- return UNZ_OK if there is no problem. }
- function unzGetGlobalComment(afile: unzFile; szComment: PAnsiChar; uSizeBuf: longint): longint;
- { Get the global comment string of the ZipFile, in the szComment buffer.
- uSizeBuf is the size of the szComment buffer.
- return the number of byte copied or an error code <0 }
- {***************************************************************************}
- { Unzip package allow you browse the directory of the zipfile }
- function unzGoToFirstFile(afile: unzFile): longint;
- { Set the current file of the zipfile to the first file.
- return UNZ_OK if there is no problem }
- function unzGoToNextFile(afile: unzFile): longint;
- { Set the current file of the zipfile to the next file.
- return UNZ_OK if there is no problem
- return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. }
- function unzLocateFile(afile: unzFile; const szFileName: PAnsiChar; iCaseSensitivity: longint): longint; { ZEXPORT }
- { Try locate the file szFileName in the zipfile.
- For the iCaseSensitivity signification, see unzStringFileNameCompare
- return value :
- UNZ_OK if the file is found. It becomes the current file.
- UNZ_END_OF_LIST_OF_FILE if the file is not found }
- function unzGetCurrentFileInfo(afile: unzFile; pfile_info: unz_file_info_ptr; szFileName: PAnsiChar; fileNameBufferSize: longint; extraField: pointer; extraFieldBufferSize: longint; szComment: PAnsiChar; commentBufferSize: longint): longint; { ZEXPORT }
- { Get Info about the current file
- if pfile_info<>NIL, the pfile_info^ structure will contain somes
- info about the current file
- if szFileName<>NIL, the filemane string will be copied in szFileName
- (fileNameBufferSize is the size of the buffer)
- if extraField<>NIL, the extra field information will be copied in
- extraField (extraFieldBufferSize is the size of the buffer).
- This is the Central-header version of the extra field
- if szComment<>NIL, the comment string of the file will be copied in
- szComment (commentBufferSize is the size of the buffer) }
- {***************************************************************************}
- {* for reading the content of the current zipfile, you can open it, read data
- from it, and close it (you can close it before reading all the file) }
- function unzOpenCurrentFile(afile: unzFile): longint; { ZEXPORT }
- { Open for reading data the current file in the zipfile.
- If there is no error, the return value is UNZ_OK. }
- function unzCloseCurrentFile(afile: unzFile): longint; { ZEXPORT }
- { Close the file in zip opened with unzOpenCurrentFile
- Return UNZ_CRCERROR if all the file was read but the CRC is not good }
- function unzReadCurrentFile(afile: unzFile; buf: pointer; len: cardinal): longint; { ZEXPORT }
- { Read bytes from the current file (opened by unzOpenCurrentFile)
- buf contain buffer where data must be copied
- len the size of buf.
- return the number of byte copied if somes bytes are copied
- return 0 if the end of file was reached
- return <0 with error code if there is an error
- (UNZ_ERRNO for IO error, or zLib error for uncompress error) }
- function unztell(afile: unzFile): z_off_t;
- { Give the current position in uncompressed data }
- function unzeof(afile: unzFile): longint;
- { return 1 if the end of file was reached, 0 elsewhere
- ! checks for valid params }
- function unzGetLocalExtrafield(afile: unzFile; buf: pointer; len: cardinal): longint;
- { Read extra field from the current file (opened by unzOpenCurrentFile)
- This is the local-header version of the extra field (sometimes, there is
- more info in the local-header version than in the central-header)
- if buf=NIL, it return the size of the local extra field
- if buf<>NIL, len is the size of the buffer, the extra header is copied in
- buf.
- the return value is the number of bytes copied in buf, or (if <0)
- the error code }
- { ----------------------------------------------------------------- }
- implementation
- {$IFDEF FPC_DOTTEDUNITS}
- uses
- {$ifdef Delphi}
- SysUtils,
- {$else}
- System.Strings,
- {$endif}
- System.ZLib.Zinflate, System.Hash.Crc;
- {$ELSE FPC_DOTTEDUNITS}
- uses
- {$ifdef Delphi}
- SysUtils,
- {$else}
- strings,
- {$endif}
- zInflate, crc;
- {$ENDIF FPC_DOTTEDUNITS}
- {$ifdef unix and not def (CASESENSITIVITYDEFAULT_YES) and \
- !defined(CASESENSITIVITYDEFAULT_NO)}
- {$define CASESENSITIVITYDEFAULT_NO}
- {$endif}
- const
- UNZ_BUFSIZE = Z_BUFSIZE;
- UNZ_MAXFILENAMEINZIP = Z_MAXFILENAMEINZIP;
- const
- unz_copyright: PAnsiChar = ' unzip 0.15 Copyright 1998 Gilles Vollant ';
- { unz_file_info_internal contain internal info about a file in zipfile }
- type
- unz_file_info_internal = record
- offset_curfile: longint; { relative offset of local header 4 bytes }
- end;
- unz_file_info_internal_ptr = ^unz_file_info_internal;
- { file_in_zip_read_info_s contain internal information about a file
- in zipfile, when reading and decompress it }
- type
- file_in_zip_read_info_s = record
- read_buffer: PAnsiChar; { internal buffer for compressed data }
- stream: z_stream; { zLib stream structure for inflate }
- pos_in_zipfile: longint; { position in byte on the zipfile, for fseek}
- stream_initialised: boolean; { flag set if stream structure is initialised}
- offset_local_extrafield: longint; { offset of the local extra field }
- size_local_extrafield: smallint; { size of the local extra field }
- pos_local_extrafield: longint; { position in the local extra field in read}
- crc32: longint; { crc32 of all data uncompressed }
- crc32_wait: longint; { crc32 we must obtain after decompress all }
- rest_read_compressed: longint; { number of byte to be decompressed }
- rest_read_uncompressed: longint; {number of byte to be obtained after decomp}
- afile: FILEptr; { io structure of the zipfile }
- compression_method: longint; { compression method (0=store) }
- byte_before_the_zipfile: longint; { byte before the zipfile, (>0 for sfx) }
- end;
- file_in_zip_read_info_s_ptr = ^file_in_zip_read_info_s;
- { unz_s contain internal information about the zipfile }
- type
- unz_s = record
- afile: FILEptr; { io structore of the zipfile }
- gi: unz_global_info; { public global information }
- byte_before_the_zipfile: longint; { byte before the zipfile, (>0 for sfx)}
- num_file: longint; { number of the current file in the zipfile}
- pos_in_central_dir: longint; { pos of the current file in the central dir}
- current_file_ok: boolean; { flag about the usability of the current file}
- central_pos: longint; { position of the beginning of the central dir}
- size_central_dir: longint; { size of the central directory }
- offset_central_dir: longint; { offset of start of central directory with
- respect to the starting disk number }
- cur_file_info: unz_file_info; { public info about the current file in zip}
- cur_file_info_internal: unz_file_info_internal; { private info about it}
- pfile_in_zip_read: file_in_zip_read_info_s_ptr; { structure about the current
- file if we are decompressing it }
- end;
- unz_s_ptr = ^unz_s;
- { ===========================================================================
- Read a byte from a gz_stream; update next_in and avail_in. Return EOF
- for end of file.
- IN assertion: the stream s has been sucessfully opened for reading. }
- function unzlocal_getByte(fin: FILEptr; var pi: longint): longint;
- var
- c: byte;
- err: longint;
- begin
- err := fread(@c, 1, 1, fin);
- if (err = 1) then
- begin
- pi := longint(c);
- unzlocal_getByte := UNZ_OK;
- {exit;}
- end
- else
- if feof(fin) = 1 then {if ferror(fin) then}
- unzlocal_getByte := UNZ_ERRNO
- else
- unzlocal_getByte := UNZ_EOF{exit;};
- end;
- { ===========================================================================
- Reads a long in LSB order from the given gz_stream. Sets }
- function unzlocal_getShort(fin: FILEptr; var pX: longint): longint;
- var
- x: longint;
- i: longint;
- err: longint;
- begin
- err := unzlocal_getByte(fin, i);
- x := longint(i);
- if (err = UNZ_OK) then
- err := unzlocal_getByte(fin, i);
- Inc(x, longint(i) shl 8);
- if (err = UNZ_OK) then
- pX := x
- else
- pX := 0;
- unzlocal_getShort := err;
- end;
- function unzlocal_getLong(fin: FILEptr; var pX: longint): longint;
- var
- x: longint;
- i: longint;
- err: longint;
- begin
- err := unzlocal_getByte(fin, i);
- x := longint(i);
- if (err = UNZ_OK) then
- err := unzlocal_getByte(fin, i);
- Inc(x, longint(i) shl 8);
- if (err = UNZ_OK) then
- err := unzlocal_getByte(fin, i);
- Inc(x, longint(i) shl 16);
- if (err = UNZ_OK) then
- err := unzlocal_getByte(fin, i);
- Inc(x, longint(i) shl 24);
- if (err = UNZ_OK) then
- pX := x
- else
- pX := 0;
- unzlocal_getLong := err;
- end;
- { My own strcmpi / strcasecmp }
- function strcmpcasenosensitive_internal(fileName1: PAnsiChar; fileName2: PAnsiChar): longint;
- var
- c1, c2: AnsiChar;
- begin
- repeat
- c1 := fileName1^;
- Inc(fileName1);
- c2 := fileName2^;
- Inc(fileName2);
- if (c1 >= 'a') and (c1 <= 'z') then
- Dec(c1, $20);
- if (c2 >= 'a') and (c2 <= 'z') then
- Dec(c2, $20);
- if (c1 = #0) then
- begin
- if c2 = #0 then
- strcmpcasenosensitive_internal := 0
- else
- strcmpcasenosensitive_internal := -1;
- exit;
- end;
- if (c2 = #0) then
- begin
- strcmpcasenosensitive_internal := 1;
- exit;
- end;
- if (c1 < c2) then
- begin
- strcmpcasenosensitive_internal := -1;
- exit;
- end;
- if (c1 > c2) then
- begin
- strcmpcasenosensitive_internal := 1;
- exit;
- end;
- until False;
- end;
- const
- CASESENSITIVITYDEFAULTVALUE = 2;
- function unzStringFileNameCompare(const fileName1: PAnsiChar; const fileName2: PAnsiChar; iCaseSensitivity: longint): longint; { ZEXPORT }
- { Compare two filename (fileName1,fileName2).
- If iCaseSenisivity = 1 (1=true),
- comparision is case sensitive (like strcmp)
- If iCaseSenisivity = 2 (0=false),
- comparision is not case sensitive (like strcmpi or strcasecmp)
- If iCaseSenisivity = 0, case sensitivity is defaut of your
- operating system like 1 on Unix, 2 on Windows)
- }
- begin
- if (iCaseSensitivity = 0) then
- iCaseSensitivity := CASESENSITIVITYDEFAULTVALUE;
- if (iCaseSensitivity = 1) then
- begin
- unzStringFileNameCompare := strComp(fileName1, fileName2);
- exit;
- end;
- unzStringFileNameCompare := strcmpcasenosensitive_internal(fileName1, fileName2);
- end;
- const
- BUFREADCOMMENT = $400;
- { Locate the Central directory of a zipfile (at the end, just before
- the global comment) }
- function unzlocal_SearchCentralDir(fin: FILEptr): longint;
- var
- buf: Pbytearray;
- uSizeFile: longint;
- uBackRead: longint;
- uMaxBack: longint;
- uPosFound: longint;
- var
- uReadSize, uReadPos: longint;
- i: longint;
- begin
- uMaxBack := $ffff; { maximum size of global comment }
- uPosFound := 0;
- if (fseek(fin, 0, SEEK_END) <> 0) then
- begin
- unzlocal_SearchCentralDir := 0;
- exit;
- end;
- uSizeFile := ftell(fin);
- if (uMaxBack > uSizeFile) then
- uMaxBack := uSizeFile;
- buf := Pbytearray(AllocMem(BUFREADCOMMENT + 4));
- if (buf = nil) then
- begin
- unzlocal_SearchCentralDir := 0;
- exit;
- end;
- uBackRead := 4;
- while (uBackRead < uMaxBack) do
- begin
- if (uBackRead + BUFREADCOMMENT > uMaxBack) then
- uBackRead := uMaxBack
- else
- Inc(uBackRead, BUFREADCOMMENT);
- uReadPos := uSizeFile - uBackRead;
- if ((BUFREADCOMMENT + 4) < (uSizeFile - uReadPos)) then
- uReadSize := (BUFREADCOMMENT + 4)
- else
- uReadSize := (uSizeFile - uReadPos);
- if fseek(fin, uReadPos, SEEK_SET) <> 0 then
- break;
- if fread(buf, uReadSize, 1, fin) <> 1 then
- break;
- i := uReadSize - 3;
- while (i > 0) do
- begin
- Dec(i);
- if (buf^[i] = $50) and (buf^[i + 1] = $4b) and { ENDHEADERMAGIC }
- (buf^[i + 2] = $05) and (buf^[i + 3] = $06) then
- begin
- uPosFound := uReadPos + i;
- break;
- end;
- end;
- if (uPosFound <> 0) then
- break;
- end;
- FreeMem(buf);
- unzlocal_SearchCentralDir := uPosFound;
- end;
- { Open a Zip file. path contain the full pathname (by example,
- on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer
- "zlib/zlib111.zip".
- If the zipfile cannot be opened (file don't exist or in not valid), the
- return value is NIL.
- Else, the return value is a unzFile Handle, usable with other function
- of this unzip package.
- }
- function unzOpen(const path: PAnsiChar): unzFile; { ZEXPORT }
- var
- us: unz_s;
- s: unz_s_ptr;
- central_pos, uL: longint;
- fin: FILEptr;
- number_disk: longint; { number of the current dist, used for spaning ZIP,
- unsupported, always 0 }
- number_disk_with_CD: longint; { number the the disk with central dir,
- used for spaning ZIP, unsupported, always 0 }
- number_entry_CD: longint; { total number of entries in the central dir
- (same than number_entry on nospan) }
- err: longint;
- begin
- err := UNZ_OK;
- if (unz_copyright[0] <> ' ') then
- begin
- unzOpen := nil;
- exit;
- end;
- fin := fopen(path, fopenread);
- if (fin = nil) then
- begin
- unzOpen := nil;
- exit;
- end;
- central_pos := unzlocal_SearchCentralDir(fin);
- if (central_pos = 0) then
- err := UNZ_ERRNO;
- if (fseek(fin, central_pos, SEEK_SET) <> 0) then
- err := UNZ_ERRNO;
- { the signature, already checked }
- if (unzlocal_getLong(fin, uL) <> UNZ_OK) then
- err := UNZ_ERRNO;
- { number of this disk }
- if (unzlocal_getShort(fin, number_disk) <> UNZ_OK) then
- err := UNZ_ERRNO;
- { number of the disk with the start of the central directory }
- if (unzlocal_getShort(fin, number_disk_with_CD) <> UNZ_OK) then
- err := UNZ_ERRNO;
- { total number of entries in the central dir on this disk }
- if (unzlocal_getShort(fin, us.gi.number_entry) <> UNZ_OK) then
- err := UNZ_ERRNO;
- { total number of entries in the central dir }
- if (unzlocal_getShort(fin, number_entry_CD) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if ((number_entry_CD <> us.gi.number_entry) or
- (number_disk_with_CD <> 0) or
- (number_disk <> 0)) then
- err := UNZ_BADZIPFILE;
- { size of the central directory }
- if (unzlocal_getLong(fin, us.size_central_dir) <> UNZ_OK) then
- err := UNZ_ERRNO;
- { offset of start of central directory with respect to the
- starting disk number }
- if (unzlocal_getLong(fin, us.offset_central_dir) <> UNZ_OK) then
- err := UNZ_ERRNO;
- { zipfile comment length }
- if (unzlocal_getShort(fin, us.gi.size_comment) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if ((central_pos < us.offset_central_dir + us.size_central_dir) and
- (err = UNZ_OK)) then
- err := UNZ_BADZIPFILE;
- if (err <> UNZ_OK) then
- begin
- fclose(fin);
- unzOpen := nil;
- exit;
- end;
- us.afile := fin;
- us.byte_before_the_zipfile := central_pos -
- (us.offset_central_dir + us.size_central_dir);
- us.central_pos := central_pos;
- us.pfile_in_zip_read := nil;
- s := unz_s_ptr(AllocMem(sizeof(unz_s)));
- s^ := us;
- unzGoToFirstFile(unzFile(s));
- unzOpen := unzFile(s);
- end;
- { Close a ZipFile opened with unzipOpen.
- If there are files inside the .Zip opened with unzOpenCurrentFile()
- (see later), these files MUST be closed with unzipCloseCurrentFile()
- before a call unzipClose.
- return UNZ_OK if there is no problem. }
- function unzClose(afile: unzFile): longint; { ZEXPORT }
- var
- s: unz_s_ptr;
- begin
- if (afile = nil) then
- begin
- unzClose := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- if (s^.pfile_in_zip_read <> nil) then
- unzCloseCurrentFile(afile);
- fclose(s^.afile);
- FreeMem(s);
- unzClose := UNZ_OK;
- end;
- { Write info about the ZipFile in the pglobal_info structure.
- No preparation of the structure is needed
- return UNZ_OK if there is no problem. }
- function unzGetGlobalInfo(afile: unzFile; var pglobal_info: unz_global_info): longint; { ZEXPORT }
- var
- s: unz_s_ptr;
- begin
- if (afile = nil) then
- begin
- unzGetGlobalInfo := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- pglobal_info := s^.gi;
- unzGetGlobalInfo := UNZ_OK;
- end;
- { Translate date/time from Dos format to tm_unz (more easily readable) }
- procedure unzlocal_DosDateToTmuDate(ulDosDate: longint; var ptm: tm_unz);
- var
- uDate: longint;
- begin
- uDate := longint(ulDosDate shr 16);
- ptm.tm_mday := integer(uDate and $1f);
- ptm.tm_mon := integer((((uDate) and $1E0) div $20) - 1);
- ptm.tm_year := integer(((uDate and $0FE00) div $0200) + 1980);
- ptm.tm_hour := integer((ulDosDate and $F800) div $800);
- ptm.tm_min := integer((ulDosDate and $7E0) div $20);
- ptm.tm_sec := integer(2 * (ulDosDate and $1f));
- end;
- { Get Info about the current file in the zipfile, with internal only info }
- function unzlocal_GetCurrentFileInfoInternal(afile: unzFile; pfile_info: unz_file_info_ptr; pfile_info_internal: unz_file_info_internal_ptr; szFileName: PAnsiChar; fileNameBufferSize: longint; extraField: pointer; extraFieldBufferSize: longint; szComment: PAnsiChar; commentBufferSize: longint): longint;
- var
- s: unz_s_ptr;
- file_info: unz_file_info;
- file_info_internal: unz_file_info_internal;
- err: longint;
- uMagic: longint;
- lSeek: longint;
- var
- uSizeRead: longint;
- begin
- err := UNZ_OK;
- lSeek := 0;
- if (afile = nil) then
- begin
- unzlocal_GetCurrentFileInfoInternal := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- if (fseek(s^.afile,
- s^.pos_in_central_dir + s^.byte_before_the_zipfile, SEEK_SET) <> 0) then
- err := UNZ_ERRNO;
- { we check the magic }
- if (err = UNZ_OK) then
- if (unzlocal_getLong(s^.afile, uMagic) <> UNZ_OK) then
- err := UNZ_ERRNO
- else
- if (uMagic <> CENTRALHEADERMAGIC) then
- err := UNZ_BADZIPFILE;
- if (unzlocal_getShort(s^.afile, file_info.version) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, file_info.version_needed) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, file_info.flag) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, file_info.compression_method) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getLong(s^.afile, file_info.dosDate) <> UNZ_OK) then
- err := UNZ_ERRNO;
- unzlocal_DosDateToTmuDate(file_info.dosDate, file_info.tmu_date);
- if (unzlocal_getLong(s^.afile, file_info.crc) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getLong(s^.afile, file_info.compressed_size) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getLong(s^.afile, file_info.uncompressed_size) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, file_info.size_filename) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, file_info.size_file_extra) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, file_info.size_file_comment) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, file_info.disk_num_start) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, file_info.internal_fa) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getLong(s^.afile, file_info.external_fa) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getLong(s^.afile, file_info_internal.offset_curfile) <> UNZ_OK) then
- err := UNZ_ERRNO;
- Inc(lSeek, file_info.size_filename);
- if ((err = UNZ_OK) and (szFileName <> nil)) then
- begin
- if (file_info.size_filename < fileNameBufferSize) then
- begin
- (szFileName +file_info.size_filename)^ := #0;
- uSizeRead := file_info.size_filename;
- end
- else
- uSizeRead := fileNameBufferSize;
- if (file_info.size_filename > 0) and (fileNameBufferSize > 0) then
- if fread(szFileName, uSizeRead, 1, s^.afile) <> 1 then
- err := UNZ_ERRNO;
- Dec(lSeek, uSizeRead);
- end;
- if ((err = UNZ_OK) and (extraField <> nil)) then
- begin
- if (file_info.size_file_extra < extraFieldBufferSize) then
- uSizeRead := file_info.size_file_extra
- else
- uSizeRead := extraFieldBufferSize;
- if (lSeek <> 0) then
- if (fseek(s^.afile, lSeek, SEEK_CUR) = 0) then
- lSeek := 0
- else
- err := UNZ_ERRNO;
- if ((file_info.size_file_extra > 0) and (extraFieldBufferSize > 0)) then
- if fread(extraField, uSizeRead, 1, s^.afile) <> 1 then
- err := UNZ_ERRNO;
- Inc(lSeek, file_info.size_file_extra - uSizeRead);
- end
- else
- Inc(lSeek, file_info.size_file_extra);
- if ((err = UNZ_OK) and (szComment <> nil)) then
- begin
- if (file_info.size_file_comment < commentBufferSize) then
- begin
- (szComment +file_info.size_file_comment)^ := #0;
- uSizeRead := file_info.size_file_comment;
- end
- else
- uSizeRead := commentBufferSize;
- if (lSeek <> 0) then
- if (fseek(s^.afile, lSeek, SEEK_CUR) = 0) then
- lSeek := 0
- else
- err := UNZ_ERRNO;
- if ((file_info.size_file_comment > 0) and (commentBufferSize > 0)) then
- if fread(szComment, uSizeRead, 1, s^.afile) <> 1 then
- err := UNZ_ERRNO;
- Inc(lSeek, file_info.size_file_comment - uSizeRead);
- end
- else
- Inc(lSeek, file_info.size_file_comment);
- if ((err = UNZ_OK) and (pfile_info <> nil)) then
- pfile_info^ := file_info;
- if ((err = UNZ_OK) and (pfile_info_internal <> nil)) then
- pfile_info_internal^ := file_info_internal;
- unzlocal_GetCurrentFileInfoInternal := err;
- end;
- { Write info about the ZipFile in the *pglobal_info structure.
- No preparation of the structure is needed
- return UNZ_OK if there is no problem. }
- function unzGetCurrentFileInfo(afile: unzFile; pfile_info: unz_file_info_ptr; szFileName: PAnsiChar; fileNameBufferSize: longint; extraField: pointer; extraFieldBufferSize: longint; szComment: PAnsiChar; commentBufferSize: longint): longint; { ZEXPORT }
- { Get Info about the current file
- if pfile_info<>NIL, the pfile_info^ structure will contain somes
- info about the current file
- if szFileName<>NIL, the filemane string will be copied in szFileName
- (fileNameBufferSize is the size of the buffer)
- if extraField<>NIL, the extra field information will be copied in
- extraField (extraFieldBufferSize is the size of the buffer).
- This is the Central-header version of the extra field
- if szComment<>NIL, the comment string of the file will be copied in
- szComment (commentBufferSize is the size of the buffer) }
- begin
- unzGetCurrentFileInfo := unzlocal_GetCurrentFileInfoInternal(afile,
- pfile_info, nil, szFileName, fileNameBufferSize, extraField,
- extraFieldBufferSize, szComment, commentBufferSize);
- end;
- { Set the current file of the zipfile to the first file.
- return UNZ_OK if there is no problem }
- function unzGoToFirstFile(afile: unzFile): longint; { ZEXPORT }
- var
- err: longint;
- s: unz_s_ptr;
- begin
- if (afile = nil) then
- begin
- unzGoToFirstFile := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- s^.pos_in_central_dir := s^.offset_central_dir;
- s^.num_file := 0;
- err := unzlocal_GetCurrentFileInfoInternal(afile, @s^.cur_file_info, @s^.cur_file_info_internal, nil, 0, nil, 0, nil, 0);
- s^.current_file_ok := (err = UNZ_OK);
- unzGoToFirstFile := err;
- end;
- { Set the current file of the zipfile to the next file.
- return UNZ_OK if there is no problem
- return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. }
- function unzGoToNextFile(afile: unzFile): longint; { ZEXPORT }
- var
- s: unz_s_ptr;
- err: longint;
- begin
- if (afile = nil) then
- begin
- unzGoToNextFile := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- if not s^.current_file_ok then
- begin
- unzGoToNextFile := UNZ_END_OF_LIST_OF_FILE;
- exit;
- end;
- if (s^.num_file + 1 = s^.gi.number_entry) then
- begin
- unzGoToNextFile := UNZ_END_OF_LIST_OF_FILE;
- exit;
- end;
- Inc(s^.pos_in_central_dir,
- SIZECENTRALDIRITEM + s^.cur_file_info.size_filename +
- s^.cur_file_info.size_file_extra + s^.cur_file_info.size_file_comment);
- Inc(s^.num_file);
- err := unzlocal_GetCurrentFileInfoInternal(afile, @s^.cur_file_info, @s^.cur_file_info_internal, nil, 0, nil, 0, nil, 0);
- s^.current_file_ok := (err = UNZ_OK);
- unzGoToNextFile := err;
- end;
- { Try locate the file szFileName in the zipfile.
- For the iCaseSensitivity signification, see unzStringFileNameCompare
- return value :
- UNZ_OK if the file is found. It becomes the current file.
- UNZ_END_OF_LIST_OF_FILE if the file is not found }
- function unzLocateFile(afile: unzFile; const szFileName: PAnsiChar; iCaseSensitivity: longint): longint; { ZEXPORT }
- var
- s: unz_s_ptr;
- err: longint;
- num_fileSaved: longint;
- pos_in_central_dirSaved: longint;
- var
- szCurrentFileName: array[0..UNZ_MAXFILENAMEINZIP + 1 - 1] of AnsiChar;
- begin
- if (afile = nil) then
- begin
- unzLocateFile := UNZ_PARAMERROR;
- exit;
- end;
- if (strlen(szFileName) >= UNZ_MAXFILENAMEINZIP) then
- begin
- unzLocateFile := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- if (not s^.current_file_ok) then
- begin
- unzLocateFile := UNZ_END_OF_LIST_OF_FILE;
- exit;
- end;
- num_fileSaved := s^.num_file;
- pos_in_central_dirSaved := s^.pos_in_central_dir;
- err := unzGoToFirstFile(afile);
- while (err = UNZ_OK) do
- begin
- unzGetCurrentFileInfo(afile, nil,
- szCurrentFileName, sizeof(szCurrentFileName) - 1, nil, 0, nil, 0);
- if (unzStringFileNameCompare(szCurrentFileName,
- szFileName, iCaseSensitivity) = 0) then
- begin
- unzLocateFile := UNZ_OK;
- exit;
- end;
- err := unzGoToNextFile(afile);
- end;
- s^.num_file := num_fileSaved;
- s^.pos_in_central_dir := pos_in_central_dirSaved;
- unzLocateFile := err;
- end;
- { Read the local header of the current zipfile
- Check the coherency of the local header and info in the end of central
- directory about this file
- store in *piSizeVar the size of extra info in local header
- (filename and size of extra field data) }
- function unzlocal_CheckCurrentFileCoherencyHeader(s: unz_s_ptr; var piSizeVar: longint; var poffset_local_extrafield: longint; var psize_local_extrafield: integer): longint;
- var
- uMagic, uData, uFlags: longint;
- size_filename: longint;
- size_extra_field: longint;
- err: longint;
- begin
- err := UNZ_OK;
- piSizeVar := 0;
- poffset_local_extrafield := 0;
- psize_local_extrafield := 0;
- if (fseek(s^.afile, s^.cur_file_info_internal.offset_curfile +
- s^.byte_before_the_zipfile, SEEK_SET) <> 0) then
- begin
- unzlocal_CheckCurrentFileCoherencyHeader := UNZ_ERRNO;
- exit;
- end;
- if (err = UNZ_OK) then
- if (unzlocal_getLong(s^.afile, uMagic) <> UNZ_OK) then
- err := UNZ_ERRNO
- else
- if (uMagic <> $04034b50) then
- err := UNZ_BADZIPFILE;
- if (unzlocal_getShort(s^.afile, uData) <> UNZ_OK) then
- err := UNZ_ERRNO;
- {
- else
- if ((err=UNZ_OK) and (uData<>s^.cur_file_info.wVersion)) then
- err := UNZ_BADZIPFILE;
- }
- if (unzlocal_getShort(s^.afile, uFlags) <> UNZ_OK) then
- err := UNZ_ERRNO;
- if (unzlocal_getShort(s^.afile, uData) <> UNZ_OK) then
- err := UNZ_ERRNO
- else
- if ((err = UNZ_OK) and (uData <> s^.cur_file_info.compression_method)) then
- err := UNZ_BADZIPFILE;
- if ((err = UNZ_OK) and (s^.cur_file_info.compression_method <> 0) and
- (s^.cur_file_info.compression_method <> Z_DEFLATED)) then
- err := UNZ_BADZIPFILE;
- if (unzlocal_getLong(s^.afile, uData) <> UNZ_OK) then { date/time }
- err := UNZ_ERRNO;
- if (unzlocal_getLong(s^.afile, uData) <> UNZ_OK) then { crc }
- err := UNZ_ERRNO
- else
- if ((err = UNZ_OK) and (uData <> s^.cur_file_info.crc) and
- ((uFlags and 8) = 0)) then
- err := UNZ_BADZIPFILE;
- if (unzlocal_getLong(s^.afile, uData) <> UNZ_OK) then { size compr }
- err := UNZ_ERRNO
- else
- if ((err = UNZ_OK) and (uData <> s^.cur_file_info.compressed_size) and
- ((uFlags and 8) = 0)) then
- err := UNZ_BADZIPFILE;
- if (unzlocal_getLong(s^.afile, uData) <> UNZ_OK) then { size uncompr }
- err := UNZ_ERRNO
- else
- if ((err = UNZ_OK) and (uData <> s^.cur_file_info.uncompressed_size) and
- ((uFlags and 8) = 0)) then
- err := UNZ_BADZIPFILE;
- if (unzlocal_getShort(s^.afile, size_filename) <> UNZ_OK) then
- err := UNZ_ERRNO
- else
- if ((err = UNZ_OK) and (size_filename <> s^.cur_file_info.size_filename)) then
- err := UNZ_BADZIPFILE;
- Inc(piSizeVar, integer(size_filename));
- if (unzlocal_getShort(s^.afile, size_extra_field) <> UNZ_OK) then
- err := UNZ_ERRNO;
- poffset_local_extrafield := s^.cur_file_info_internal.offset_curfile +
- SIZEZIPLOCALHEADER + size_filename;
- psize_local_extrafield := integer(size_extra_field);
- Inc(piSizeVar, integer(size_extra_field));
- unzlocal_CheckCurrentFileCoherencyHeader := err;
- end;
- { Open for reading data the current file in the zipfile.
- If there is no error, the return value is UNZ_OK. }
- function unzOpenCurrentFile(afile: unzFile): longint; { ZEXPORT }
- var
- err: longint;
- Store: boolean;
- iSizeVar: longint;
- s: unz_s_ptr;
- pfile_in_zip_read_info: file_in_zip_read_info_s_ptr;
- offset_local_extrafield: longint; { offset of the local extra field }
- size_local_extrafield: smallint; { size of the local extra field }
- begin
- err := UNZ_OK;
- if (afile = nil) then
- begin
- unzOpenCurrentFile := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- if not s^.current_file_ok then
- begin
- unzOpenCurrentFile := UNZ_PARAMERROR;
- exit;
- end;
- if (s^.pfile_in_zip_read <> nil) then
- unzCloseCurrentFile(afile);
- if (unzlocal_CheckCurrentFileCoherencyHeader(s, iSizeVar,
- offset_local_extrafield, size_local_extrafield) <> UNZ_OK) then
- begin
- unzOpenCurrentFile := UNZ_BADZIPFILE;
- exit;
- end;
- pfile_in_zip_read_info := file_in_zip_read_info_s_ptr(
- AllocMem(sizeof(file_in_zip_read_info_s)));
- if (pfile_in_zip_read_info = nil) then
- begin
- unzOpenCurrentFile := UNZ_INTERNALERROR;
- exit;
- end;
- pfile_in_zip_read_info^.read_buffer := PAnsiChar(AllocMem(UNZ_BUFSIZE));
- pfile_in_zip_read_info^.offset_local_extrafield := offset_local_extrafield;
- pfile_in_zip_read_info^.size_local_extrafield := size_local_extrafield;
- pfile_in_zip_read_info^.pos_local_extrafield := 0;
- if (pfile_in_zip_read_info^.read_buffer = nil) then
- begin
- FreeMem(pfile_in_zip_read_info);
- unzOpenCurrentFile := UNZ_INTERNALERROR;
- exit;
- end;
- pfile_in_zip_read_info^.stream_initialised := False;
- if ((s^.cur_file_info.compression_method <> 0) and
- (s^.cur_file_info.compression_method <> Z_DEFLATED)) then
- err := UNZ_BADZIPFILE;
- Store := s^.cur_file_info.compression_method = 0;
- pfile_in_zip_read_info^.crc32_wait := s^.cur_file_info.crc;
- pfile_in_zip_read_info^.crc32 := 0;
- pfile_in_zip_read_info^.compression_method := s^.cur_file_info.compression_method;
- pfile_in_zip_read_info^.afile := s^.afile;
- pfile_in_zip_read_info^.byte_before_the_zipfile := s^.byte_before_the_zipfile;
- pfile_in_zip_read_info^.stream.total_out := 0;
- if (not Store) then
- begin
- err := inflateInit2(pfile_in_zip_read_info^.stream, -MAX_WBITS);
- if (err = Z_OK) then
- pfile_in_zip_read_info^.stream_initialised := True;
- { windowBits is passed < 0 to tell that there is no zlib header.
- Note that in this case inflate *requires* an extra "dummy" byte
- after the compressed stream in order to complete decompression and
- return Z_STREAM_END.
- In unzip, i don't wait absolutely Z_STREAM_END because I known the
- size of both compressed and uncompressed data }
- end;
- pfile_in_zip_read_info^.rest_read_compressed := s^.cur_file_info.compressed_size;
- pfile_in_zip_read_info^.rest_read_uncompressed := s^.cur_file_info.uncompressed_size;
- pfile_in_zip_read_info^.pos_in_zipfile :=
- s^.cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + iSizeVar;
- pfile_in_zip_read_info^.stream.avail_in := 0;
- s^.pfile_in_zip_read := pfile_in_zip_read_info;
- unzOpenCurrentFile := UNZ_OK;
- end;
- { Read bytes from the current file (opened by unzOpenCurrentFile)
- buf contain buffer where data must be copied
- len the size of buf.
- return the number of byte copied if somes bytes are copied
- return 0 if the end of file was reached
- return <0 with error code if there is an error
- (UNZ_ERRNO for IO error, or zLib error for uncompress error) }
- function unzReadCurrentFile(afile: unzFile; buf: pointer; len: cardinal): longint; { ZEXPORT }
- var
- err: longint;
- iRead: Longint;
- s: unz_s_ptr;
- pfile_in_zip_read_info: file_in_zip_read_info_s_ptr;
- var
- uReadThis: longint;
- var
- uDoCopy, i: longint;
- var
- uTotalOutBefore, uTotalOutAfter: longint;
- bufBefore: pbyte;
- uOutThis: longint;
- flush: longint;
- begin
- err := UNZ_OK;
- iRead := 0;
- if (afile = nil) then
- begin
- unzReadCurrentFile := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- pfile_in_zip_read_info := s^.pfile_in_zip_read;
- if (pfile_in_zip_read_info = nil) then
- begin
- unzReadCurrentFile := UNZ_PARAMERROR;
- exit;
- end;
- if ((pfile_in_zip_read_info^.read_buffer = nil)) then
- begin
- unzReadCurrentFile := UNZ_END_OF_LIST_OF_FILE;
- exit;
- end;
- if (len = 0) then
- begin
- unzReadCurrentFile := 0;
- exit;
- end;
- pfile_in_zip_read_info^.stream.next_out := pbyte(buf);
- pfile_in_zip_read_info^.stream.avail_out := len;
- if (len > pfile_in_zip_read_info^.rest_read_uncompressed) then
- pfile_in_zip_read_info^.stream.avail_out :=
- pfile_in_zip_read_info^.rest_read_uncompressed;
- while (pfile_in_zip_read_info^.stream.avail_out > 0) do
- begin
- if ((pfile_in_zip_read_info^.stream.avail_in = 0) and
- (pfile_in_zip_read_info^.rest_read_compressed > 0)) then
- begin
- uReadThis := UNZ_BUFSIZE;
- if (pfile_in_zip_read_info^.rest_read_compressed < uReadThis) then
- uReadThis := pfile_in_zip_read_info^.rest_read_compressed;
- if (uReadThis = 0) then
- begin
- unzReadCurrentFile := UNZ_EOF;
- exit;
- end;
- if (fseek(pfile_in_zip_read_info^.afile,
- pfile_in_zip_read_info^.pos_in_zipfile +
- pfile_in_zip_read_info^.byte_before_the_zipfile, SEEK_SET) <> 0) then
- begin
- unzReadCurrentFile := UNZ_ERRNO;
- exit;
- end;
- if fread(pfile_in_zip_read_info^.read_buffer, uReadThis, 1,
- pfile_in_zip_read_info^.afile) <> 1 then
- begin
- unzReadCurrentFile := UNZ_ERRNO;
- exit;
- end;
- Inc(pfile_in_zip_read_info^.pos_in_zipfile, uReadThis);
- Dec(pfile_in_zip_read_info^.rest_read_compressed, uReadThis);
- pfile_in_zip_read_info^.stream.next_in :=
- pbyte(pfile_in_zip_read_info^.read_buffer);
- pfile_in_zip_read_info^.stream.avail_in := uReadThis;
- end;
- if (pfile_in_zip_read_info^.compression_method = 0) then
- begin
- if (pfile_in_zip_read_info^.stream.avail_out <
- pfile_in_zip_read_info^.stream.avail_in) then
- uDoCopy := pfile_in_zip_read_info^.stream.avail_out
- else
- uDoCopy := pfile_in_zip_read_info^.stream.avail_in;
- for i := 0 to uDoCopy - 1 do
- Pbytearray(pfile_in_zip_read_info^.stream.next_out)^[i] :=
- Pbytearray(pfile_in_zip_read_info^.stream.next_in)^[i];
- pfile_in_zip_read_info^.crc32 := crc32(pfile_in_zip_read_info^.crc32,
- pfile_in_zip_read_info^.stream.next_out, uDoCopy);
- Dec(pfile_in_zip_read_info^.rest_read_uncompressed, uDoCopy);
- Dec(pfile_in_zip_read_info^.stream.avail_in, uDoCopy);
- Dec(pfile_in_zip_read_info^.stream.avail_out, uDoCopy);
- Inc(pfile_in_zip_read_info^.stream.next_out, uDoCopy);
- Inc(pfile_in_zip_read_info^.stream.next_in, uDoCopy);
- Inc(pfile_in_zip_read_info^.stream.total_out, uDoCopy);
- Inc(iRead, uDoCopy);
- end
- else
- begin
- flush := Z_SYNC_FLUSH;
- uTotalOutBefore := pfile_in_zip_read_info^.stream.total_out;
- bufBefore := pfile_in_zip_read_info^.stream.next_out;
- {
- if ((pfile_in_zip_read_info^.rest_read_uncompressed =
- pfile_in_zip_read_info^.stream.avail_out) and
- (pfile_in_zip_read_info^.rest_read_compressed = 0)) then
- flush := Z_FINISH;
- }
- err := inflate(pfile_in_zip_read_info^.stream, flush);
- uTotalOutAfter := pfile_in_zip_read_info^.stream.total_out;
- uOutThis := uTotalOutAfter - uTotalOutBefore;
- pfile_in_zip_read_info^.crc32 :=
- crc32(pfile_in_zip_read_info^.crc32, bufBefore, uOutThis);
- Dec(pfile_in_zip_read_info^.rest_read_uncompressed, uOutThis);
- Inc(iRead, uTotalOutAfter - uTotalOutBefore);
- if (err = Z_STREAM_END) then
- begin
- if iRead = 0 then
- unzReadCurrentFile := UNZ_EOF
- else
- unzReadCurrentFile := iRead;
- exit;
- end;
- if (err <> Z_OK) then
- break;
- end;
- end; { while }
- if (err = Z_OK) then
- begin
- unzReadCurrentFile := iRead;
- exit;
- end;
- unzReadCurrentFile := err;
- end;
- { Give the current position in uncompressed data }
- function unztell(afile: unzFile): z_off_t; { ZEXPORT }
- var
- s: unz_s_ptr;
- pfile_in_zip_read_info: file_in_zip_read_info_s_ptr;
- begin
- if (afile = nil) then
- begin
- unztell := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- pfile_in_zip_read_info := s^.pfile_in_zip_read;
- if (pfile_in_zip_read_info = nil) then
- begin
- unztell := UNZ_PARAMERROR;
- exit;
- end;
- unztell := z_off_t(pfile_in_zip_read_info^.stream.total_out);
- end;
- { return 1 (TRUE) if the end of file was reached, 0 elsewhere }
- function unzeof(afile: unzFile): longint;
- var
- s: unz_s_ptr;
- pfile_in_zip_read_info: file_in_zip_read_info_s_ptr;
- begin
- if (afile = nil) then
- begin
- unzeof := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- pfile_in_zip_read_info := s^.pfile_in_zip_read;
- if (pfile_in_zip_read_info = nil) then
- begin
- unzeof := UNZ_PARAMERROR;
- exit;
- end;
- if (pfile_in_zip_read_info^.rest_read_uncompressed = 0) then
- unzeof := 1
- else
- unzeof := 0;
- end;
- { Read extra field from the current file (opened by unzOpenCurrentFile)
- This is the local-header version of the extra field (sometimes, there is
- more info in the local-header version than in the central-header)
- if buf=NIL, it return the size of the local extra field
- if buf<>NIL, len is the size of the buffer, the extra header is copied in
- buf.
- the return value is the number of bytes copied in buf, or (if <0)
- the error code }
- function unzGetLocalExtrafield(afile: unzFile; buf: pointer; len: cardinal): longint;
- var
- s: unz_s_ptr;
- pfile_in_zip_read_info: file_in_zip_read_info_s_ptr;
- read_now: longint;
- size_to_read: longint;
- begin
- if (afile = nil) then
- begin
- unzGetLocalExtrafield := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- pfile_in_zip_read_info := s^.pfile_in_zip_read;
- if (pfile_in_zip_read_info = nil) then
- begin
- unzGetLocalExtrafield := UNZ_PARAMERROR;
- exit;
- end;
- size_to_read := (pfile_in_zip_read_info^.size_local_extrafield -
- pfile_in_zip_read_info^.pos_local_extrafield);
- if (buf = nil) then
- begin
- unzGetLocalExtrafield := longint(size_to_read);
- exit;
- end;
- if (len > size_to_read) then
- read_now := size_to_read
- else
- read_now := len;
- if (read_now = 0) then
- begin
- unzGetLocalExtrafield := 0;
- exit;
- end;
- if (fseek(pfile_in_zip_read_info^.afile,
- pfile_in_zip_read_info^.offset_local_extrafield +
- pfile_in_zip_read_info^.pos_local_extrafield, SEEK_SET) <> 0) then
- begin
- unzGetLocalExtrafield := UNZ_ERRNO;
- exit;
- end;
- if fread(buf, size_to_read, 1, pfile_in_zip_read_info^.afile) <> 1 then
- begin
- unzGetLocalExtrafield := UNZ_ERRNO;
- exit;
- end;
- unzGetLocalExtrafield := longint(read_now);
- end;
- { Close the file in zip opened with unzOpenCurrentFile
- Return UNZ_CRCERROR if all the file was read but the CRC is not good }
- function unzCloseCurrentFile(afile: unzFile): longint; { ZEXPORT }
- var
- err: longint;
- s: unz_s_ptr;
- pfile_in_zip_read_info: file_in_zip_read_info_s_ptr;
- begin
- err := UNZ_OK;
- if (afile = nil) then
- begin
- unzCloseCurrentFile := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- pfile_in_zip_read_info := s^.pfile_in_zip_read;
- if (pfile_in_zip_read_info = nil) then
- begin
- unzCloseCurrentFile := UNZ_PARAMERROR;
- exit;
- end;
- if (pfile_in_zip_read_info^.rest_read_uncompressed = 0) then
- if (pfile_in_zip_read_info^.crc32 <> pfile_in_zip_read_info^.crc32_wait) then
- err := UNZ_CRCERROR;
- FreeMem(pfile_in_zip_read_info^.read_buffer);
- pfile_in_zip_read_info^.read_buffer := nil;
- if (pfile_in_zip_read_info^.stream_initialised) then
- inflateEnd(pfile_in_zip_read_info^.stream);
- pfile_in_zip_read_info^.stream_initialised := False;
- FreeMem(pfile_in_zip_read_info);
- s^.pfile_in_zip_read := nil;
- unzCloseCurrentFile := err;
- end;
- { Get the global comment string of the ZipFile, in the szComment buffer.
- uSizeBuf is the size of the szComment buffer.
- return the number of byte copied or an error code <0 }
- function unzGetGlobalComment(afile: unzFile; szComment: PAnsiChar; uSizeBuf: longint): longint; { ZEXPORT }
- var
- s: unz_s_ptr;
- uReadThis: longint;
- begin
- if (afile = nil) then
- begin
- unzGetGlobalComment := UNZ_PARAMERROR;
- exit;
- end;
- s := unz_s_ptr(afile);
- uReadThis := uSizeBuf;
- if (uReadThis > s^.gi.size_comment) then
- uReadThis := s^.gi.size_comment;
- if (fseek(s^.afile, s^.central_pos + 22, SEEK_SET) <> 0) then
- begin
- unzGetGlobalComment := UNZ_ERRNO;
- exit;
- end;
- if (uReadThis > 0) then
- begin
- szComment^ := #0;
- if fread(szComment, uReadThis, 1, s^.afile) <> 1 then
- begin
- unzGetGlobalComment := UNZ_ERRNO;
- exit;
- end;
- end;
- if ((szComment <> nil) and (uSizeBuf > s^.gi.size_comment)) then
- (szComment +s^.gi.size_comment)^ := #0;
- unzGetGlobalComment := longint(uReadThis);
- end;
- end.
|