12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559 |
- unit Unzip;
- { ----------------------------------------------------------------- }
- { 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}
- uses
- //zutil,
- zbase,
- //zLib,
- ziputils;
- 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: PChar; const fileName2: PChar; 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: PChar): 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: PChar; 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: PChar; 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: PChar; fileNameBufferSize: longint; extraField: pointer; extraFieldBufferSize: longint; szComment: PChar; 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
- uses
- {$ifdef Delphi}
- SysUtils,
- {$else}
- strings,
- {$endif}
- zInflate, crc;
- {$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: PChar = ' 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: PChar; { 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: integer; { 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: PChar; fileName2: PChar): longint;
- var
- c1, c2: char;
- 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: PChar; const fileName2: PChar; 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, integer(uReadSize), 1, fin) <> 1 then
- break;
- i := longint(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 + integer(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: PChar): 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: PChar; fileNameBufferSize: longint; extraField: pointer; extraFieldBufferSize: longint; szComment: PChar; 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, integer(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, integer(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, integer(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: PChar; fileNameBufferSize: longint; extraField: pointer; extraFieldBufferSize: longint; szComment: PChar; 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: PChar; 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 char;
- 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: integer; 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: integer;
- 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: integer; { 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 := PChar(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 := integer(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: integer;
- s: unz_s_ptr;
- pfile_in_zip_read_info: file_in_zip_read_info_s_ptr;
- var
- uReadThis: integer;
- var
- uDoCopy, i: integer;
- 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 := integer(len);
- if (len > pfile_in_zip_read_info^.rest_read_uncompressed) then
- pfile_in_zip_read_info^.stream.avail_out :=
- integer(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 := integer(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 := integer(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, integer(uOutThis));
- Dec(pfile_in_zip_read_info^.rest_read_uncompressed, uOutThis);
- Inc(iRead, integer(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: integer;
- 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 := integer(size_to_read)
- else
- read_now := integer(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, integer(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: PChar; 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, integer(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.
|