ZipFile.cs 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487
  1. // ZipFile.cs
  2. //
  3. // Copyright (C) 2001 Mike Krueger
  4. // Copyright (C) 2004 John Reilly
  5. //
  6. // This file was translated from java, it was part of the GNU Classpath
  7. // Copyright (C) 2001 Free Software Foundation, Inc.
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public License
  11. // as published by the Free Software Foundation; either version 2
  12. // of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. //
  23. // Linking this library statically or dynamically with other modules is
  24. // making a combined work based on this library. Thus, the terms and
  25. // conditions of the GNU General Public License cover the whole
  26. // combination.
  27. //
  28. // As a special exception, the copyright holders of this library give you
  29. // permission to link this library with independent modules to produce an
  30. // executable, regardless of the license terms of these independent
  31. // modules, and to copy and distribute the resulting executable under
  32. // terms of your choice, provided that you also meet, for each linked
  33. // independent module, the terms and conditions of the license of that
  34. // module. An independent module is a module which is not derived from
  35. // or based on this library. If you modify this library, you may extend
  36. // this exception to your version of the library, but you are not
  37. // obligated to do so. If you do not wish to do so, delete this
  38. // exception statement from your version.
  39. // HISTORY
  40. // 2009-12-22 Z-1649 Added AES support
  41. // 2010-03-02 Z-1650 Fixed updating ODT archives in memory. Exposed exceptions in updating.
  42. // 2010-05-25 Z-1663 Fixed exception when testing local header compressed size of -1
  43. // 2012-11-29 Z-1684 Fixed ZipFile.Add(string fileName, string entryName) losing the file TimeStamp
  44. using System;
  45. using System.Collections;
  46. using System.IO;
  47. using System.Text;
  48. using System.Globalization;
  49. #if !NETCF_1_0
  50. using System.Security.Cryptography;
  51. using CommonMPQ.SharpZipLib.Encryption;
  52. #endif
  53. using CommonMPQ.SharpZipLib.Core;
  54. using CommonMPQ.SharpZipLib.Checksums;
  55. using CommonMPQ.SharpZipLib.Zip.Compression.Streams;
  56. using CommonMPQ.SharpZipLib.Zip.Compression;
  57. namespace CommonMPQ.SharpZipLib.Zip
  58. {
  59. #region Keys Required Event Args
  60. /// <summary>
  61. /// Arguments used with KeysRequiredEvent
  62. /// </summary>
  63. public class KeysRequiredEventArgs : EventArgs
  64. {
  65. #region Constructors
  66. /// <summary>
  67. /// Initialise a new instance of <see cref="KeysRequiredEventArgs"></see>
  68. /// </summary>
  69. /// <param name="name">The name of the file for which keys are required.</param>
  70. public KeysRequiredEventArgs(string name)
  71. {
  72. fileName = name;
  73. }
  74. /// <summary>
  75. /// Initialise a new instance of <see cref="KeysRequiredEventArgs"></see>
  76. /// </summary>
  77. /// <param name="name">The name of the file for which keys are required.</param>
  78. /// <param name="keyValue">The current key value.</param>
  79. public KeysRequiredEventArgs(string name, byte[] keyValue)
  80. {
  81. fileName = name;
  82. key = keyValue;
  83. }
  84. #endregion
  85. #region Properties
  86. /// <summary>
  87. /// Gets the name of the file for which keys are required.
  88. /// </summary>
  89. public string FileName
  90. {
  91. get { return fileName; }
  92. }
  93. /// <summary>
  94. /// Gets or sets the key value
  95. /// </summary>
  96. public byte[] Key
  97. {
  98. get { return key; }
  99. set { key = value; }
  100. }
  101. #endregion
  102. #region Instance Fields
  103. string fileName;
  104. byte[] key;
  105. #endregion
  106. }
  107. #endregion
  108. #region Test Definitions
  109. /// <summary>
  110. /// The strategy to apply to testing.
  111. /// </summary>
  112. public enum TestStrategy
  113. {
  114. /// <summary>
  115. /// Find the first error only.
  116. /// </summary>
  117. FindFirstError,
  118. /// <summary>
  119. /// Find all possible errors.
  120. /// </summary>
  121. FindAllErrors,
  122. }
  123. /// <summary>
  124. /// The operation in progress reported by a <see cref="ZipTestResultHandler"/> during testing.
  125. /// </summary>
  126. /// <seealso cref="ZipFile.TestArchive(bool)">TestArchive</seealso>
  127. public enum TestOperation
  128. {
  129. /// <summary>
  130. /// Setting up testing.
  131. /// </summary>
  132. Initialising,
  133. /// <summary>
  134. /// Testing an individual entries header
  135. /// </summary>
  136. EntryHeader,
  137. /// <summary>
  138. /// Testing an individual entries data
  139. /// </summary>
  140. EntryData,
  141. /// <summary>
  142. /// Testing an individual entry has completed.
  143. /// </summary>
  144. EntryComplete,
  145. /// <summary>
  146. /// Running miscellaneous tests
  147. /// </summary>
  148. MiscellaneousTests,
  149. /// <summary>
  150. /// Testing is complete
  151. /// </summary>
  152. Complete,
  153. }
  154. /// <summary>
  155. /// Status returned returned by <see cref="ZipTestResultHandler"/> during testing.
  156. /// </summary>
  157. /// <seealso cref="ZipFile.TestArchive(bool)">TestArchive</seealso>
  158. public class TestStatus
  159. {
  160. #region Constructors
  161. /// <summary>
  162. /// Initialise a new instance of <see cref="TestStatus"/>
  163. /// </summary>
  164. /// <param name="file">The <see cref="ZipFile"/> this status applies to.</param>
  165. public TestStatus(ZipFile file)
  166. {
  167. file_ = file;
  168. }
  169. #endregion
  170. #region Properties
  171. /// <summary>
  172. /// Get the current <see cref="TestOperation"/> in progress.
  173. /// </summary>
  174. public TestOperation Operation
  175. {
  176. get { return operation_; }
  177. }
  178. /// <summary>
  179. /// Get the <see cref="ZipFile"/> this status is applicable to.
  180. /// </summary>
  181. public ZipFile File
  182. {
  183. get { return file_; }
  184. }
  185. /// <summary>
  186. /// Get the current/last entry tested.
  187. /// </summary>
  188. public ZipEntry Entry
  189. {
  190. get { return entry_; }
  191. }
  192. /// <summary>
  193. /// Get the number of errors detected so far.
  194. /// </summary>
  195. public int ErrorCount
  196. {
  197. get { return errorCount_; }
  198. }
  199. /// <summary>
  200. /// Get the number of bytes tested so far for the current entry.
  201. /// </summary>
  202. public long BytesTested
  203. {
  204. get { return bytesTested_; }
  205. }
  206. /// <summary>
  207. /// Get a value indicating wether the last entry test was valid.
  208. /// </summary>
  209. public bool EntryValid
  210. {
  211. get { return entryValid_; }
  212. }
  213. #endregion
  214. #region Internal API
  215. internal void AddError()
  216. {
  217. errorCount_++;
  218. entryValid_ = false;
  219. }
  220. internal void SetOperation(TestOperation operation)
  221. {
  222. operation_ = operation;
  223. }
  224. internal void SetEntry(ZipEntry entry)
  225. {
  226. entry_ = entry;
  227. entryValid_ = true;
  228. bytesTested_ = 0;
  229. }
  230. internal void SetBytesTested(long value)
  231. {
  232. bytesTested_ = value;
  233. }
  234. #endregion
  235. #region Instance Fields
  236. ZipFile file_;
  237. ZipEntry entry_;
  238. bool entryValid_;
  239. int errorCount_;
  240. long bytesTested_;
  241. TestOperation operation_;
  242. #endregion
  243. }
  244. /// <summary>
  245. /// Delegate invoked during <see cref="ZipFile.TestArchive(bool, TestStrategy, ZipTestResultHandler)">testing</see> if supplied indicating current progress and status.
  246. /// </summary>
  247. /// <remarks>If the message is non-null an error has occured. If the message is null
  248. /// the operation as found in <see cref="TestStatus">status</see> has started.</remarks>
  249. public delegate void ZipTestResultHandler(TestStatus status, string message);
  250. #endregion
  251. #region Update Definitions
  252. /// <summary>
  253. /// The possible ways of <see cref="ZipFile.CommitUpdate()">applying updates</see> to an archive.
  254. /// </summary>
  255. public enum FileUpdateMode
  256. {
  257. /// <summary>
  258. /// Perform all updates on temporary files ensuring that the original file is saved.
  259. /// </summary>
  260. Safe,
  261. /// <summary>
  262. /// Update the archive directly, which is faster but less safe.
  263. /// </summary>
  264. Direct,
  265. }
  266. #endregion
  267. #region ZipFile Class
  268. /// <summary>
  269. /// This class represents a Zip archive. You can ask for the contained
  270. /// entries, or get an input stream for a file entry. The entry is
  271. /// automatically decompressed.
  272. ///
  273. /// You can also update the archive adding or deleting entries.
  274. ///
  275. /// This class is thread safe for input: You can open input streams for arbitrary
  276. /// entries in different threads.
  277. /// <br/>
  278. /// <br/>Author of the original java version : Jochen Hoenicke
  279. /// </summary>
  280. /// <example>
  281. /// <code>
  282. /// using System;
  283. /// using System.Text;
  284. /// using System.Collections;
  285. /// using System.IO;
  286. ///
  287. /// using CommonMPQ.SharpZipLib.Zip;
  288. ///
  289. /// class MainClass
  290. /// {
  291. /// static public void Main(string[] args)
  292. /// {
  293. /// using (ZipFile zFile = new ZipFile(args[0])) {
  294. /// Console.WriteLine("Listing of : " + zFile.Name);
  295. /// Console.WriteLine("");
  296. /// Console.WriteLine("Raw Size Size Date Time Name");
  297. /// Console.WriteLine("-------- -------- -------- ------ ---------");
  298. /// foreach (ZipEntry e in zFile) {
  299. /// if ( e.IsFile ) {
  300. /// DateTime d = e.DateTime;
  301. /// Console.WriteLine("{0, -10}{1, -10}{2} {3} {4}", e.Size, e.CompressedSize,
  302. /// d.ToString("dd-MM-yy"), d.ToString("HH:mm"),
  303. /// e.Name);
  304. /// }
  305. /// }
  306. /// }
  307. /// }
  308. /// }
  309. /// </code>
  310. /// </example>
  311. public class ZipFile : IEnumerable, IDisposable
  312. {
  313. #region KeyHandling
  314. /// <summary>
  315. /// Delegate for handling keys/password setting during compresion/decompression.
  316. /// </summary>
  317. public delegate void KeysRequiredEventHandler(
  318. object sender,
  319. KeysRequiredEventArgs e
  320. );
  321. /// <summary>
  322. /// Event handler for handling encryption keys.
  323. /// </summary>
  324. public KeysRequiredEventHandler KeysRequired;
  325. /// <summary>
  326. /// Handles getting of encryption keys when required.
  327. /// </summary>
  328. /// <param name="fileName">The file for which encryption keys are required.</param>
  329. void OnKeysRequired(string fileName)
  330. {
  331. if (KeysRequired != null) {
  332. KeysRequiredEventArgs krea = new KeysRequiredEventArgs(fileName, key);
  333. KeysRequired(this, krea);
  334. key = krea.Key;
  335. }
  336. }
  337. /// <summary>
  338. /// Get/set the encryption key value.
  339. /// </summary>
  340. byte[] Key
  341. {
  342. get { return key; }
  343. set { key = value; }
  344. }
  345. #if !NETCF_1_0
  346. /// <summary>
  347. /// Password to be used for encrypting/decrypting files.
  348. /// </summary>
  349. /// <remarks>Set to null if no password is required.</remarks>
  350. public string Password
  351. {
  352. set
  353. {
  354. if ( (value == null) || (value.Length == 0) ) {
  355. key = null;
  356. }
  357. else {
  358. rawPassword_ = value;
  359. key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(value));
  360. }
  361. }
  362. }
  363. #endif
  364. /// <summary>
  365. /// Get a value indicating wether encryption keys are currently available.
  366. /// </summary>
  367. bool HaveKeys
  368. {
  369. get { return key != null; }
  370. }
  371. #endregion
  372. #region Constructors
  373. /// <summary>
  374. /// Opens a Zip file with the given name for reading.
  375. /// </summary>
  376. /// <param name="name">The name of the file to open.</param>
  377. /// <exception cref="ArgumentNullException">The argument supplied is null.</exception>
  378. /// <exception cref="IOException">
  379. /// An i/o error occurs
  380. /// </exception>
  381. /// <exception cref="ZipException">
  382. /// The file doesn't contain a valid zip archive.
  383. /// </exception>
  384. public ZipFile(string name)
  385. {
  386. if ( name == null ) {
  387. throw new ArgumentNullException("name");
  388. }
  389. name_ = name;
  390. baseStream_ = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
  391. isStreamOwner = true;
  392. try {
  393. ReadEntries();
  394. }
  395. catch {
  396. DisposeInternal(true);
  397. throw;
  398. }
  399. }
  400. /// <summary>
  401. /// Opens a Zip file reading the given <see cref="FileStream"/>.
  402. /// </summary>
  403. /// <param name="file">The <see cref="FileStream"/> to read archive data from.</param>
  404. /// <exception cref="ArgumentNullException">The supplied argument is null.</exception>
  405. /// <exception cref="IOException">
  406. /// An i/o error occurs.
  407. /// </exception>
  408. /// <exception cref="ZipException">
  409. /// The file doesn't contain a valid zip archive.
  410. /// </exception>
  411. public ZipFile(FileStream file)
  412. {
  413. if ( file == null ) {
  414. throw new ArgumentNullException("file");
  415. }
  416. if ( !file.CanSeek ) {
  417. throw new ArgumentException("Stream is not seekable", "file");
  418. }
  419. baseStream_ = file;
  420. name_ = file.Name;
  421. isStreamOwner = true;
  422. try {
  423. ReadEntries();
  424. }
  425. catch {
  426. DisposeInternal(true);
  427. throw;
  428. }
  429. }
  430. /// <summary>
  431. /// Opens a Zip file reading the given <see cref="Stream"/>.
  432. /// </summary>
  433. /// <param name="stream">The <see cref="Stream"/> to read archive data from.</param>
  434. /// <exception cref="IOException">
  435. /// An i/o error occurs
  436. /// </exception>
  437. /// <exception cref="ZipException">
  438. /// The stream doesn't contain a valid zip archive.<br/>
  439. /// </exception>
  440. /// <exception cref="ArgumentException">
  441. /// The <see cref="Stream">stream</see> doesnt support seeking.
  442. /// </exception>
  443. /// <exception cref="ArgumentNullException">
  444. /// The <see cref="Stream">stream</see> argument is null.
  445. /// </exception>
  446. public ZipFile(Stream stream)
  447. {
  448. if ( stream == null ) {
  449. throw new ArgumentNullException("stream");
  450. }
  451. if ( !stream.CanSeek ) {
  452. throw new ArgumentException("Stream is not seekable", "stream");
  453. }
  454. baseStream_ = stream;
  455. isStreamOwner = true;
  456. if ( baseStream_.Length > 0 ) {
  457. try {
  458. ReadEntries();
  459. }
  460. catch {
  461. DisposeInternal(true);
  462. throw;
  463. }
  464. } else {
  465. entries_ = new ZipEntry[0];
  466. isNewArchive_ = true;
  467. }
  468. }
  469. /// <summary>
  470. /// Initialises a default <see cref="ZipFile"/> instance with no entries and no file storage.
  471. /// </summary>
  472. internal ZipFile()
  473. {
  474. entries_ = new ZipEntry[0];
  475. isNewArchive_ = true;
  476. }
  477. #endregion
  478. #region Destructors and Closing
  479. /// <summary>
  480. /// Finalize this instance.
  481. /// </summary>
  482. ~ZipFile()
  483. {
  484. Dispose(false);
  485. }
  486. /// <summary>
  487. /// Closes the ZipFile. If the stream is <see cref="IsStreamOwner">owned</see> then this also closes the underlying input stream.
  488. /// Once closed, no further instance methods should be called.
  489. /// </summary>
  490. /// <exception cref="System.IO.IOException">
  491. /// An i/o error occurs.
  492. /// </exception>
  493. public void Close()
  494. {
  495. DisposeInternal(true);
  496. GC.SuppressFinalize(this);
  497. }
  498. #endregion
  499. #region Creators
  500. /// <summary>
  501. /// Create a new <see cref="ZipFile"/> whose data will be stored in a file.
  502. /// </summary>
  503. /// <param name="fileName">The name of the archive to create.</param>
  504. /// <returns>Returns the newly created <see cref="ZipFile"/></returns>
  505. /// <exception cref="ArgumentNullException"><paramref name="fileName"></paramref> is null</exception>
  506. public static ZipFile Create(string fileName)
  507. {
  508. if ( fileName == null ) {
  509. throw new ArgumentNullException("fileName");
  510. }
  511. FileStream fs = File.Create(fileName);
  512. ZipFile result = new ZipFile();
  513. result.name_ = fileName;
  514. result.baseStream_ = fs;
  515. result.isStreamOwner = true;
  516. return result;
  517. }
  518. /// <summary>
  519. /// Create a new <see cref="ZipFile"/> whose data will be stored on a stream.
  520. /// </summary>
  521. /// <param name="outStream">The stream providing data storage.</param>
  522. /// <returns>Returns the newly created <see cref="ZipFile"/></returns>
  523. /// <exception cref="ArgumentNullException"><paramref name="outStream"> is null</paramref></exception>
  524. /// <exception cref="ArgumentException"><paramref name="outStream"> doesnt support writing.</paramref></exception>
  525. public static ZipFile Create(Stream outStream)
  526. {
  527. if ( outStream == null ) {
  528. throw new ArgumentNullException("outStream");
  529. }
  530. if ( !outStream.CanWrite ) {
  531. throw new ArgumentException("Stream is not writeable", "outStream");
  532. }
  533. if ( !outStream.CanSeek ) {
  534. throw new ArgumentException("Stream is not seekable", "outStream");
  535. }
  536. ZipFile result = new ZipFile();
  537. result.baseStream_ = outStream;
  538. return result;
  539. }
  540. #endregion
  541. #region Properties
  542. /// <summary>
  543. /// Get/set a flag indicating if the underlying stream is owned by the ZipFile instance.
  544. /// If the flag is true then the stream will be closed when <see cref="Close">Close</see> is called.
  545. /// </summary>
  546. /// <remarks>
  547. /// The default value is true in all cases.
  548. /// </remarks>
  549. public bool IsStreamOwner
  550. {
  551. get { return isStreamOwner; }
  552. set { isStreamOwner = value; }
  553. }
  554. /// <summary>
  555. /// Get a value indicating wether
  556. /// this archive is embedded in another file or not.
  557. /// </summary>
  558. public bool IsEmbeddedArchive
  559. {
  560. // Not strictly correct in all circumstances currently
  561. get { return offsetOfFirstEntry > 0; }
  562. }
  563. /// <summary>
  564. /// Get a value indicating that this archive is a new one.
  565. /// </summary>
  566. public bool IsNewArchive
  567. {
  568. get { return isNewArchive_; }
  569. }
  570. /// <summary>
  571. /// Gets the comment for the zip file.
  572. /// </summary>
  573. public string ZipFileComment
  574. {
  575. get { return comment_; }
  576. }
  577. /// <summary>
  578. /// Gets the name of this zip file.
  579. /// </summary>
  580. public string Name
  581. {
  582. get { return name_; }
  583. }
  584. /// <summary>
  585. /// Gets the number of entries in this zip file.
  586. /// </summary>
  587. /// <exception cref="InvalidOperationException">
  588. /// The Zip file has been closed.
  589. /// </exception>
  590. [Obsolete("Use the Count property instead")]
  591. public int Size
  592. {
  593. get
  594. {
  595. return entries_.Length;
  596. }
  597. }
  598. /// <summary>
  599. /// Get the number of entries contained in this <see cref="ZipFile"/>.
  600. /// </summary>
  601. public long Count
  602. {
  603. get
  604. {
  605. return entries_.Length;
  606. }
  607. }
  608. /// <summary>
  609. /// Indexer property for ZipEntries
  610. /// </summary>
  611. [System.Runtime.CompilerServices.IndexerNameAttribute("EntryByIndex")]
  612. public ZipEntry this[int index]
  613. {
  614. get {
  615. return (ZipEntry) entries_[index].Clone();
  616. }
  617. }
  618. #endregion
  619. #region Input Handling
  620. /// <summary>
  621. /// Gets an enumerator for the Zip entries in this Zip file.
  622. /// </summary>
  623. /// <returns>Returns an <see cref="IEnumerator"/> for this archive.</returns>
  624. /// <exception cref="ObjectDisposedException">
  625. /// The Zip file has been closed.
  626. /// </exception>
  627. public IEnumerator GetEnumerator()
  628. {
  629. if (isDisposed_) {
  630. throw new ObjectDisposedException("ZipFile");
  631. }
  632. return new ZipEntryEnumerator(entries_);
  633. }
  634. /// <summary>
  635. /// Return the index of the entry with a matching name
  636. /// </summary>
  637. /// <param name="name">Entry name to find</param>
  638. /// <param name="ignoreCase">If true the comparison is case insensitive</param>
  639. /// <returns>The index position of the matching entry or -1 if not found</returns>
  640. /// <exception cref="ObjectDisposedException">
  641. /// The Zip file has been closed.
  642. /// </exception>
  643. public int FindEntry(string name, bool ignoreCase)
  644. {
  645. if (isDisposed_) {
  646. throw new ObjectDisposedException("ZipFile");
  647. }
  648. // TODO: This will be slow as the next ice age for huge archives!
  649. for (int i = 0; i < entries_.Length; i++) {
  650. if (string.Compare(name, entries_[i].Name, ignoreCase, CultureInfo.InvariantCulture) == 0) {
  651. return i;
  652. }
  653. }
  654. return -1;
  655. }
  656. /// <summary>
  657. /// Searches for a zip entry in this archive with the given name.
  658. /// String comparisons are case insensitive
  659. /// </summary>
  660. /// <param name="name">
  661. /// The name to find. May contain directory components separated by slashes ('/').
  662. /// </param>
  663. /// <returns>
  664. /// A clone of the zip entry, or null if no entry with that name exists.
  665. /// </returns>
  666. /// <exception cref="ObjectDisposedException">
  667. /// The Zip file has been closed.
  668. /// </exception>
  669. public ZipEntry GetEntry(string name)
  670. {
  671. if (isDisposed_) {
  672. throw new ObjectDisposedException("ZipFile");
  673. }
  674. int index = FindEntry(name, true);
  675. return (index >= 0) ? (ZipEntry) entries_[index].Clone() : null;
  676. }
  677. /// <summary>
  678. /// Gets an input stream for reading the given zip entry data in an uncompressed form.
  679. /// Normally the <see cref="ZipEntry"/> should be an entry returned by GetEntry().
  680. /// </summary>
  681. /// <param name="entry">The <see cref="ZipEntry"/> to obtain a data <see cref="Stream"/> for</param>
  682. /// <returns>An input <see cref="Stream"/> containing data for this <see cref="ZipEntry"/></returns>
  683. /// <exception cref="ObjectDisposedException">
  684. /// The ZipFile has already been closed
  685. /// </exception>
  686. /// <exception cref="CommonMPQ.SharpZipLib.Zip.ZipException">
  687. /// The compression method for the entry is unknown
  688. /// </exception>
  689. /// <exception cref="IndexOutOfRangeException">
  690. /// The entry is not found in the ZipFile
  691. /// </exception>
  692. public Stream GetInputStream(ZipEntry entry)
  693. {
  694. if ( entry == null ) {
  695. throw new ArgumentNullException("entry");
  696. }
  697. if ( isDisposed_ ) {
  698. throw new ObjectDisposedException("ZipFile");
  699. }
  700. long index = entry.ZipFileIndex;
  701. if ( (index < 0) || (index >= entries_.Length) || (entries_[index].Name != entry.Name) ) {
  702. index = FindEntry(entry.Name, true);
  703. if (index < 0) {
  704. throw new ZipException("Entry cannot be found");
  705. }
  706. }
  707. return GetInputStream(index);
  708. }
  709. /// <summary>
  710. /// Creates an input stream reading a zip entry
  711. /// </summary>
  712. /// <param name="entryIndex">The index of the entry to obtain an input stream for.</param>
  713. /// <returns>
  714. /// An input <see cref="Stream"/> containing data for this <paramref name="entryIndex"/>
  715. /// </returns>
  716. /// <exception cref="ObjectDisposedException">
  717. /// The ZipFile has already been closed
  718. /// </exception>
  719. /// <exception cref="CommonMPQ.SharpZipLib.Zip.ZipException">
  720. /// The compression method for the entry is unknown
  721. /// </exception>
  722. /// <exception cref="IndexOutOfRangeException">
  723. /// The entry is not found in the ZipFile
  724. /// </exception>
  725. public Stream GetInputStream(long entryIndex)
  726. {
  727. if ( isDisposed_ ) {
  728. throw new ObjectDisposedException("ZipFile");
  729. }
  730. long start = LocateEntry(entries_[entryIndex]);
  731. CompressionMethod method = entries_[entryIndex].CompressionMethod;
  732. Stream result = new PartialInputStream(this, start, entries_[entryIndex].CompressedSize);
  733. if (entries_[entryIndex].IsCrypted == true) {
  734. #if NETCF_1_0
  735. throw new ZipException("decryption not supported for Compact Framework 1.0");
  736. #else
  737. result = CreateAndInitDecryptionStream(result, entries_[entryIndex]);
  738. if (result == null) {
  739. throw new ZipException("Unable to decrypt this entry");
  740. }
  741. #endif
  742. }
  743. switch (method) {
  744. case CompressionMethod.Stored:
  745. // read as is.
  746. break;
  747. case CompressionMethod.Deflated:
  748. // No need to worry about ownership and closing as underlying stream close does nothing.
  749. result = new InflaterInputStream(result, new Inflater(true));
  750. break;
  751. default:
  752. throw new ZipException("Unsupported compression method " + method);
  753. }
  754. return result;
  755. }
  756. #endregion
  757. #region Archive Testing
  758. /// <summary>
  759. /// Test an archive for integrity/validity
  760. /// </summary>
  761. /// <param name="testData">Perform low level data Crc check</param>
  762. /// <returns>true if all tests pass, false otherwise</returns>
  763. /// <remarks>Testing will terminate on the first error found.</remarks>
  764. public bool TestArchive(bool testData)
  765. {
  766. return TestArchive(testData, TestStrategy.FindFirstError, null);
  767. }
  768. /// <summary>
  769. /// Test an archive for integrity/validity
  770. /// </summary>
  771. /// <param name="testData">Perform low level data Crc check</param>
  772. /// <param name="strategy">The <see cref="TestStrategy"></see> to apply.</param>
  773. /// <param name="resultHandler">The <see cref="ZipTestResultHandler"></see> handler to call during testing.</param>
  774. /// <returns>true if all tests pass, false otherwise</returns>
  775. /// <exception cref="ObjectDisposedException">The object has already been closed.</exception>
  776. public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandler resultHandler)
  777. {
  778. if (isDisposed_) {
  779. throw new ObjectDisposedException("ZipFile");
  780. }
  781. TestStatus status = new TestStatus(this);
  782. if ( resultHandler != null ) {
  783. resultHandler(status, null);
  784. }
  785. HeaderTest test = testData ? (HeaderTest.Header | HeaderTest.Extract) : HeaderTest.Header;
  786. bool testing = true;
  787. try {
  788. int entryIndex = 0;
  789. while ( testing && (entryIndex < Count) ) {
  790. if ( resultHandler != null ) {
  791. status.SetEntry(this[entryIndex]);
  792. status.SetOperation(TestOperation.EntryHeader);
  793. resultHandler(status, null);
  794. }
  795. try {
  796. TestLocalHeader(this[entryIndex], test);
  797. }
  798. catch(ZipException ex) {
  799. status.AddError();
  800. if ( resultHandler != null ) {
  801. resultHandler(status,
  802. string.Format("Exception during test - '{0}'", ex.Message));
  803. }
  804. if ( strategy == TestStrategy.FindFirstError ) {
  805. testing = false;
  806. }
  807. }
  808. if ( testing && testData && this[entryIndex].IsFile ) {
  809. if ( resultHandler != null ) {
  810. status.SetOperation(TestOperation.EntryData);
  811. resultHandler(status, null);
  812. }
  813. Crc32 crc = new Crc32();
  814. using (Stream entryStream = this.GetInputStream(this[entryIndex]))
  815. {
  816. byte[] buffer = new byte[4096];
  817. long totalBytes = 0;
  818. int bytesRead;
  819. while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0)
  820. {
  821. crc.Update(buffer, 0, bytesRead);
  822. if (resultHandler != null)
  823. {
  824. totalBytes += bytesRead;
  825. status.SetBytesTested(totalBytes);
  826. resultHandler(status, null);
  827. }
  828. }
  829. }
  830. if (this[entryIndex].Crc != crc.Value) {
  831. status.AddError();
  832. if ( resultHandler != null ) {
  833. resultHandler(status, "CRC mismatch");
  834. }
  835. if ( strategy == TestStrategy.FindFirstError ) {
  836. testing = false;
  837. }
  838. }
  839. if (( this[entryIndex].Flags & (int)GeneralBitFlags.Descriptor) != 0 ) {
  840. ZipHelperStream helper = new ZipHelperStream(baseStream_);
  841. DescriptorData data = new DescriptorData();
  842. helper.ReadDataDescriptor(this[entryIndex].LocalHeaderRequiresZip64, data);
  843. if (this[entryIndex].Crc != data.Crc) {
  844. status.AddError();
  845. }
  846. if (this[entryIndex].CompressedSize != data.CompressedSize) {
  847. status.AddError();
  848. }
  849. if (this[entryIndex].Size != data.Size) {
  850. status.AddError();
  851. }
  852. }
  853. }
  854. if ( resultHandler != null ) {
  855. status.SetOperation(TestOperation.EntryComplete);
  856. resultHandler(status, null);
  857. }
  858. entryIndex += 1;
  859. }
  860. if ( resultHandler != null ) {
  861. status.SetOperation(TestOperation.MiscellaneousTests);
  862. resultHandler(status, null);
  863. }
  864. // TODO: the 'Corrina Johns' test where local headers are missing from
  865. // the central directory. They are therefore invisible to many archivers.
  866. }
  867. catch (Exception ex) {
  868. status.AddError();
  869. if ( resultHandler != null ) {
  870. resultHandler(status, string.Format("Exception during test - '{0}'", ex.Message));
  871. }
  872. }
  873. if ( resultHandler != null ) {
  874. status.SetOperation(TestOperation.Complete);
  875. status.SetEntry(null);
  876. resultHandler(status, null);
  877. }
  878. return (status.ErrorCount == 0);
  879. }
  880. [Flags]
  881. enum HeaderTest
  882. {
  883. Extract = 0x01, // Check that this header represents an entry whose data can be extracted
  884. Header = 0x02, // Check that this header contents are valid
  885. }
  886. /// <summary>
  887. /// Test a local header against that provided from the central directory
  888. /// </summary>
  889. /// <param name="entry">
  890. /// The entry to test against
  891. /// </param>
  892. /// <param name="tests">The type of <see cref="HeaderTest">tests</see> to carry out.</param>
  893. /// <returns>The offset of the entries data in the file</returns>
  894. long TestLocalHeader(ZipEntry entry, HeaderTest tests)
  895. {
  896. lock(baseStream_)
  897. {
  898. bool testHeader = (tests & HeaderTest.Header) != 0;
  899. bool testData = (tests & HeaderTest.Extract) != 0;
  900. baseStream_.Seek(offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin);
  901. if ((int)ReadLEUint() != ZipConstants.LocalHeaderSignature) {
  902. throw new ZipException(string.Format("Wrong local header signature @{0:X}", offsetOfFirstEntry + entry.Offset));
  903. }
  904. short extractVersion = ( short ) (ReadLEUshort() & 0x00ff);
  905. short localFlags = ( short )ReadLEUshort();
  906. short compressionMethod = ( short )ReadLEUshort();
  907. short fileTime = ( short )ReadLEUshort();
  908. short fileDate = ( short )ReadLEUshort();
  909. uint crcValue = ReadLEUint();
  910. long compressedSize = ReadLEUint();
  911. long size = ReadLEUint();
  912. int storedNameLength = ReadLEUshort();
  913. int extraDataLength = ReadLEUshort();
  914. byte[] nameData = new byte[storedNameLength];
  915. StreamUtils.ReadFully(baseStream_, nameData);
  916. byte[] extraData = new byte[extraDataLength];
  917. StreamUtils.ReadFully(baseStream_, extraData);
  918. ZipExtraData localExtraData = new ZipExtraData(extraData);
  919. // Extra data / zip64 checks
  920. if (localExtraData.Find(1))
  921. {
  922. // 2010-03-04 Forum 10512: removed checks for version >= ZipConstants.VersionZip64
  923. // and size or compressedSize = MaxValue, due to rogue creators.
  924. size = localExtraData.ReadLong();
  925. compressedSize = localExtraData.ReadLong();
  926. if ((localFlags & (int)GeneralBitFlags.Descriptor) != 0)
  927. {
  928. // These may be valid if patched later
  929. if ( (size != -1) && (size != entry.Size)) {
  930. throw new ZipException("Size invalid for descriptor");
  931. }
  932. if ((compressedSize != -1) && (compressedSize != entry.CompressedSize)) {
  933. throw new ZipException("Compressed size invalid for descriptor");
  934. }
  935. }
  936. }
  937. else
  938. {
  939. // No zip64 extra data but entry requires it.
  940. if ((extractVersion >= ZipConstants.VersionZip64) &&
  941. (((uint)size == uint.MaxValue) || ((uint)compressedSize == uint.MaxValue)))
  942. {
  943. throw new ZipException("Required Zip64 extended information missing");
  944. }
  945. }
  946. if ( testData ) {
  947. if ( entry.IsFile ) {
  948. if ( !entry.IsCompressionMethodSupported() ) {
  949. throw new ZipException("Compression method not supported");
  950. }
  951. if ( (extractVersion > ZipConstants.VersionMadeBy)
  952. || ((extractVersion > 20) && (extractVersion < ZipConstants.VersionZip64)) ) {
  953. throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", extractVersion));
  954. }
  955. if ( (localFlags & ( int )(GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked)) != 0 ) {
  956. throw new ZipException("The library does not support the zip version required to extract this entry");
  957. }
  958. }
  959. }
  960. if (testHeader)
  961. {
  962. if ((extractVersion <= 63) && // Ignore later versions as we dont know about them..
  963. (extractVersion != 10) &&
  964. (extractVersion != 11) &&
  965. (extractVersion != 20) &&
  966. (extractVersion != 21) &&
  967. (extractVersion != 25) &&
  968. (extractVersion != 27) &&
  969. (extractVersion != 45) &&
  970. (extractVersion != 46) &&
  971. (extractVersion != 50) &&
  972. (extractVersion != 51) &&
  973. (extractVersion != 52) &&
  974. (extractVersion != 61) &&
  975. (extractVersion != 62) &&
  976. (extractVersion != 63)
  977. )
  978. {
  979. throw new ZipException(string.Format("Version required to extract this entry is invalid ({0})", extractVersion));
  980. }
  981. // Local entry flags dont have reserved bit set on.
  982. if ((localFlags & (int)(GeneralBitFlags.ReservedPKware4 | GeneralBitFlags.ReservedPkware14 | GeneralBitFlags.ReservedPkware15)) != 0)
  983. {
  984. throw new ZipException("Reserved bit flags cannot be set.");
  985. }
  986. // Encryption requires extract version >= 20
  987. if (((localFlags & (int)GeneralBitFlags.Encrypted) != 0) && (extractVersion < 20))
  988. {
  989. throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
  990. }
  991. // Strong encryption requires encryption flag to be set and extract version >= 50.
  992. if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0)
  993. {
  994. if ((localFlags & (int)GeneralBitFlags.Encrypted) == 0)
  995. {
  996. throw new ZipException("Strong encryption flag set but encryption flag is not set");
  997. }
  998. if (extractVersion < 50)
  999. {
  1000. throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
  1001. }
  1002. }
  1003. // Patched entries require extract version >= 27
  1004. if (((localFlags & (int)GeneralBitFlags.Patched) != 0) && (extractVersion < 27))
  1005. {
  1006. throw new ZipException(string.Format("Patched data requires higher version than ({0})", extractVersion));
  1007. }
  1008. // Central header flags match local entry flags.
  1009. if (localFlags != entry.Flags)
  1010. {
  1011. throw new ZipException("Central header/local header flags mismatch");
  1012. }
  1013. // Central header compression method matches local entry
  1014. if (entry.CompressionMethod != (CompressionMethod)compressionMethod)
  1015. {
  1016. throw new ZipException("Central header/local header compression method mismatch");
  1017. }
  1018. if (entry.Version != extractVersion)
  1019. {
  1020. throw new ZipException("Extract version mismatch");
  1021. }
  1022. // Strong encryption and extract version match
  1023. if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0)
  1024. {
  1025. if (extractVersion < 62)
  1026. {
  1027. throw new ZipException("Strong encryption flag set but version not high enough");
  1028. }
  1029. }
  1030. if ((localFlags & (int)GeneralBitFlags.HeaderMasked) != 0)
  1031. {
  1032. if ((fileTime != 0) || (fileDate != 0))
  1033. {
  1034. throw new ZipException("Header masked set but date/time values non-zero");
  1035. }
  1036. }
  1037. if ((localFlags & (int)GeneralBitFlags.Descriptor) == 0)
  1038. {
  1039. if (crcValue != (uint)entry.Crc)
  1040. {
  1041. throw new ZipException("Central header/local header crc mismatch");
  1042. }
  1043. }
  1044. // Crc valid for empty entry.
  1045. // This will also apply to streamed entries where size isnt known and the header cant be patched
  1046. if ((size == 0) && (compressedSize == 0))
  1047. {
  1048. if (crcValue != 0)
  1049. {
  1050. throw new ZipException("Invalid CRC for empty entry");
  1051. }
  1052. }
  1053. // TODO: make test more correct... can't compare lengths as was done originally as this can fail for MBCS strings
  1054. // Assuming a code page at this point is not valid? Best is to store the name length in the ZipEntry probably
  1055. if (entry.Name.Length > storedNameLength)
  1056. {
  1057. throw new ZipException("File name length mismatch");
  1058. }
  1059. // Name data has already been read convert it and compare.
  1060. string localName = ZipConstants.ConvertToStringExt(localFlags, nameData);
  1061. // Central directory and local entry name match
  1062. if (localName != entry.Name)
  1063. {
  1064. throw new ZipException("Central header and local header file name mismatch");
  1065. }
  1066. // Directories have zero actual size but can have compressed size
  1067. if (entry.IsDirectory)
  1068. {
  1069. if (size > 0)
  1070. {
  1071. throw new ZipException("Directory cannot have size");
  1072. }
  1073. // There may be other cases where the compressed size can be greater than this?
  1074. // If so until details are known we will be strict.
  1075. if (entry.IsCrypted)
  1076. {
  1077. if (compressedSize > ZipConstants.CryptoHeaderSize + 2)
  1078. {
  1079. throw new ZipException("Directory compressed size invalid");
  1080. }
  1081. }
  1082. else if (compressedSize > 2)
  1083. {
  1084. // When not compressed the directory size can validly be 2 bytes
  1085. // if the true size wasnt known when data was originally being written.
  1086. // NOTE: Versions of the library 0.85.4 and earlier always added 2 bytes
  1087. throw new ZipException("Directory compressed size invalid");
  1088. }
  1089. }
  1090. if (!ZipNameTransform.IsValidName(localName, true))
  1091. {
  1092. throw new ZipException("Name is invalid");
  1093. }
  1094. }
  1095. // Tests that apply to both data and header.
  1096. // Size can be verified only if it is known in the local header.
  1097. // it will always be known in the central header.
  1098. if (((localFlags & (int)GeneralBitFlags.Descriptor) == 0) ||
  1099. ((size > 0) || (compressedSize > 0))) {
  1100. if (size != entry.Size) {
  1101. throw new ZipException(
  1102. string.Format("Size mismatch between central header({0}) and local header({1})",
  1103. entry.Size, size));
  1104. }
  1105. if (compressedSize != entry.CompressedSize &&
  1106. compressedSize != 0xFFFFFFFF && compressedSize != -1) {
  1107. throw new ZipException(
  1108. string.Format("Compressed size mismatch between central header({0}) and local header({1})",
  1109. entry.CompressedSize, compressedSize));
  1110. }
  1111. }
  1112. int extraLength = storedNameLength + extraDataLength;
  1113. return offsetOfFirstEntry + entry.Offset + ZipConstants.LocalHeaderBaseSize + extraLength;
  1114. }
  1115. }
  1116. #endregion
  1117. #region Updating
  1118. const int DefaultBufferSize = 4096;
  1119. /// <summary>
  1120. /// The kind of update to apply.
  1121. /// </summary>
  1122. enum UpdateCommand
  1123. {
  1124. Copy, // Copy original file contents.
  1125. Modify, // Change encryption, compression, attributes, name, time etc, of an existing file.
  1126. Add, // Add a new file to the archive.
  1127. }
  1128. #region Properties
  1129. /// <summary>
  1130. /// Get / set the <see cref="INameTransform"/> to apply to names when updating.
  1131. /// </summary>
  1132. public INameTransform NameTransform
  1133. {
  1134. get {
  1135. return updateEntryFactory_.NameTransform;
  1136. }
  1137. set {
  1138. updateEntryFactory_.NameTransform = value;
  1139. }
  1140. }
  1141. /// <summary>
  1142. /// Get/set the <see cref="IEntryFactory"/> used to generate <see cref="ZipEntry"/> values
  1143. /// during updates.
  1144. /// </summary>
  1145. public IEntryFactory EntryFactory
  1146. {
  1147. get {
  1148. return updateEntryFactory_;
  1149. }
  1150. set {
  1151. if (value == null) {
  1152. updateEntryFactory_ = new ZipEntryFactory();
  1153. }
  1154. else {
  1155. updateEntryFactory_ = value;
  1156. }
  1157. }
  1158. }
  1159. /// <summary>
  1160. /// Get /set the buffer size to be used when updating this zip file.
  1161. /// </summary>
  1162. public int BufferSize
  1163. {
  1164. get { return bufferSize_; }
  1165. set {
  1166. if ( value < 1024 ) {
  1167. #if NETCF_1_0
  1168. throw new ArgumentOutOfRangeException("value");
  1169. #else
  1170. throw new ArgumentOutOfRangeException("value", "cannot be below 1024");
  1171. #endif
  1172. }
  1173. if ( bufferSize_ != value ) {
  1174. bufferSize_ = value;
  1175. copyBuffer_ = null;
  1176. }
  1177. }
  1178. }
  1179. /// <summary>
  1180. /// Get a value indicating an update has <see cref="BeginUpdate()">been started</see>.
  1181. /// </summary>
  1182. public bool IsUpdating
  1183. {
  1184. get { return updates_ != null; }
  1185. }
  1186. /// <summary>
  1187. /// Get / set a value indicating how Zip64 Extension usage is determined when adding entries.
  1188. /// </summary>
  1189. public UseZip64 UseZip64
  1190. {
  1191. get { return useZip64_; }
  1192. set { useZip64_ = value; }
  1193. }
  1194. #endregion
  1195. #region Immediate updating
  1196. // TBD: Direct form of updating
  1197. //
  1198. // public void Update(IEntryMatcher deleteMatcher)
  1199. // {
  1200. // }
  1201. //
  1202. // public void Update(IScanner addScanner)
  1203. // {
  1204. // }
  1205. #endregion
  1206. #region Deferred Updating
  1207. /// <summary>
  1208. /// Begin updating this <see cref="ZipFile"/> archive.
  1209. /// </summary>
  1210. /// <param name="archiveStorage">The <see cref="IArchiveStorage">archive storage</see> for use during the update.</param>
  1211. /// <param name="dataSource">The <see cref="IDynamicDataSource">data source</see> to utilise during updating.</param>
  1212. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1213. /// <exception cref="ArgumentNullException">One of the arguments provided is null</exception>
  1214. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1215. public void BeginUpdate(IArchiveStorage archiveStorage, IDynamicDataSource dataSource)
  1216. {
  1217. if ( archiveStorage == null ) {
  1218. throw new ArgumentNullException("archiveStorage");
  1219. }
  1220. if ( dataSource == null ) {
  1221. throw new ArgumentNullException("dataSource");
  1222. }
  1223. if ( isDisposed_ ) {
  1224. throw new ObjectDisposedException("ZipFile");
  1225. }
  1226. if ( IsEmbeddedArchive ) {
  1227. throw new ZipException ("Cannot update embedded/SFX archives");
  1228. }
  1229. archiveStorage_ = archiveStorage;
  1230. updateDataSource_ = dataSource;
  1231. // NOTE: the baseStream_ may not currently support writing or seeking.
  1232. updateIndex_ = new Hashtable();
  1233. updates_ = new ArrayList(entries_.Length);
  1234. foreach(ZipEntry entry in entries_) {
  1235. int index = updates_.Add(new ZipUpdate(entry));
  1236. updateIndex_.Add(entry.Name, index);
  1237. }
  1238. // We must sort by offset before using offset's calculated sizes
  1239. updates_.Sort(new UpdateComparer());
  1240. int idx = 0;
  1241. foreach (ZipUpdate update in updates_) {
  1242. //If last entry, there is no next entry offset to use
  1243. if (idx == updates_.Count - 1)
  1244. break;
  1245. update.OffsetBasedSize = ((ZipUpdate)updates_[idx + 1]).Entry.Offset - update.Entry.Offset;
  1246. idx++;
  1247. }
  1248. updateCount_ = updates_.Count;
  1249. contentsEdited_ = false;
  1250. commentEdited_ = false;
  1251. newComment_ = null;
  1252. }
  1253. /// <summary>
  1254. /// Begin updating to this <see cref="ZipFile"/> archive.
  1255. /// </summary>
  1256. /// <param name="archiveStorage">The storage to use during the update.</param>
  1257. public void BeginUpdate(IArchiveStorage archiveStorage)
  1258. {
  1259. BeginUpdate(archiveStorage, new DynamicDiskDataSource());
  1260. }
  1261. /// <summary>
  1262. /// Begin updating this <see cref="ZipFile"/> archive.
  1263. /// </summary>
  1264. /// <seealso cref="BeginUpdate(IArchiveStorage)"/>
  1265. /// <seealso cref="CommitUpdate"></seealso>
  1266. /// <seealso cref="AbortUpdate"></seealso>
  1267. public void BeginUpdate()
  1268. {
  1269. if ( Name == null ) {
  1270. BeginUpdate(new MemoryArchiveStorage(), new DynamicDiskDataSource());
  1271. }
  1272. else {
  1273. BeginUpdate(new DiskArchiveStorage(this), new DynamicDiskDataSource());
  1274. }
  1275. }
  1276. /// <summary>
  1277. /// Commit current updates, updating this archive.
  1278. /// </summary>
  1279. /// <seealso cref="BeginUpdate()"></seealso>
  1280. /// <seealso cref="AbortUpdate"></seealso>
  1281. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1282. public void CommitUpdate()
  1283. {
  1284. if ( isDisposed_ ) {
  1285. throw new ObjectDisposedException("ZipFile");
  1286. }
  1287. CheckUpdating();
  1288. try {
  1289. updateIndex_.Clear();
  1290. updateIndex_=null;
  1291. if( contentsEdited_ ) {
  1292. RunUpdates();
  1293. }
  1294. else if( commentEdited_ ) {
  1295. UpdateCommentOnly();
  1296. }
  1297. else {
  1298. // Create an empty archive if none existed originally.
  1299. if( entries_.Length==0 ) {
  1300. byte[] theComment=(newComment_!=null)?newComment_.RawComment:ZipConstants.ConvertToArray(comment_);
  1301. using( ZipHelperStream zhs=new ZipHelperStream(baseStream_) ) {
  1302. zhs.WriteEndOfCentralDirectory(0, 0, 0, theComment);
  1303. }
  1304. }
  1305. }
  1306. }
  1307. finally {
  1308. PostUpdateCleanup();
  1309. }
  1310. }
  1311. /// <summary>
  1312. /// Abort updating leaving the archive unchanged.
  1313. /// </summary>
  1314. /// <seealso cref="BeginUpdate()"></seealso>
  1315. /// <seealso cref="CommitUpdate"></seealso>
  1316. public void AbortUpdate()
  1317. {
  1318. PostUpdateCleanup();
  1319. }
  1320. /// <summary>
  1321. /// Set the file comment to be recorded when the current update is <see cref="CommitUpdate">commited</see>.
  1322. /// </summary>
  1323. /// <param name="comment">The comment to record.</param>
  1324. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1325. public void SetComment(string comment)
  1326. {
  1327. if ( isDisposed_ ) {
  1328. throw new ObjectDisposedException("ZipFile");
  1329. }
  1330. CheckUpdating();
  1331. newComment_ = new ZipString(comment);
  1332. if ( newComment_.RawLength > 0xffff ) {
  1333. newComment_ = null;
  1334. throw new ZipException("Comment length exceeds maximum - 65535");
  1335. }
  1336. // We dont take account of the original and current comment appearing to be the same
  1337. // as encoding may be different.
  1338. commentEdited_ = true;
  1339. }
  1340. #endregion
  1341. #region Adding Entries
  1342. void AddUpdate(ZipUpdate update)
  1343. {
  1344. contentsEdited_ = true;
  1345. int index = FindExistingUpdate(update.Entry.Name);
  1346. if (index >= 0) {
  1347. if ( updates_[index] == null ) {
  1348. updateCount_ += 1;
  1349. }
  1350. // Direct replacement is faster than delete and add.
  1351. updates_[index] = update;
  1352. }
  1353. else {
  1354. index = updates_.Add(update);
  1355. updateCount_ += 1;
  1356. updateIndex_.Add(update.Entry.Name, index);
  1357. }
  1358. }
  1359. /// <summary>
  1360. /// Add a new entry to the archive.
  1361. /// </summary>
  1362. /// <param name="fileName">The name of the file to add.</param>
  1363. /// <param name="compressionMethod">The compression method to use.</param>
  1364. /// <param name="useUnicodeText">Ensure Unicode text is used for name and comment for this entry.</param>
  1365. /// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
  1366. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1367. /// <exception cref="ArgumentOutOfRangeException">Compression method is not supported.</exception>
  1368. public void Add(string fileName, CompressionMethod compressionMethod, bool useUnicodeText )
  1369. {
  1370. if (fileName == null) {
  1371. throw new ArgumentNullException("fileName");
  1372. }
  1373. if ( isDisposed_ ) {
  1374. throw new ObjectDisposedException("ZipFile");
  1375. }
  1376. if (!ZipEntry.IsCompressionMethodSupported(compressionMethod)) {
  1377. throw new ArgumentOutOfRangeException("compressionMethod");
  1378. }
  1379. CheckUpdating();
  1380. contentsEdited_ = true;
  1381. ZipEntry entry = EntryFactory.MakeFileEntry(fileName);
  1382. entry.IsUnicodeText = useUnicodeText;
  1383. entry.CompressionMethod = compressionMethod;
  1384. AddUpdate(new ZipUpdate(fileName, entry));
  1385. }
  1386. /// <summary>
  1387. /// Add a new entry to the archive.
  1388. /// </summary>
  1389. /// <param name="fileName">The name of the file to add.</param>
  1390. /// <param name="compressionMethod">The compression method to use.</param>
  1391. /// <exception cref="ArgumentNullException">ZipFile has been closed.</exception>
  1392. /// <exception cref="ArgumentOutOfRangeException">The compression method is not supported.</exception>
  1393. public void Add(string fileName, CompressionMethod compressionMethod)
  1394. {
  1395. if ( fileName == null ) {
  1396. throw new ArgumentNullException("fileName");
  1397. }
  1398. if ( !ZipEntry.IsCompressionMethodSupported(compressionMethod) ) {
  1399. throw new ArgumentOutOfRangeException("compressionMethod");
  1400. }
  1401. CheckUpdating();
  1402. contentsEdited_ = true;
  1403. ZipEntry entry = EntryFactory.MakeFileEntry(fileName);
  1404. entry.CompressionMethod = compressionMethod;
  1405. AddUpdate(new ZipUpdate(fileName, entry));
  1406. }
  1407. /// <summary>
  1408. /// Add a file to the archive.
  1409. /// </summary>
  1410. /// <param name="fileName">The name of the file to add.</param>
  1411. /// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
  1412. public void Add(string fileName)
  1413. {
  1414. if ( fileName == null ) {
  1415. throw new ArgumentNullException("fileName");
  1416. }
  1417. CheckUpdating();
  1418. AddUpdate(new ZipUpdate(fileName, EntryFactory.MakeFileEntry(fileName)));
  1419. }
  1420. /// <summary>
  1421. /// Add a file to the archive.
  1422. /// </summary>
  1423. /// <param name="fileName">The name of the file to add.</param>
  1424. /// <param name="entryName">The name to use for the <see cref="ZipEntry"/> on the Zip file created.</param>
  1425. /// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
  1426. public void Add(string fileName, string entryName)
  1427. {
  1428. if (fileName == null) {
  1429. throw new ArgumentNullException("fileName");
  1430. }
  1431. if ( entryName == null ) {
  1432. throw new ArgumentNullException("entryName");
  1433. }
  1434. CheckUpdating();
  1435. AddUpdate(new ZipUpdate(fileName, EntryFactory.MakeFileEntry(fileName, entryName, true)));
  1436. }
  1437. /// <summary>
  1438. /// Add a file entry with data.
  1439. /// </summary>
  1440. /// <param name="dataSource">The source of the data for this entry.</param>
  1441. /// <param name="entryName">The name to give to the entry.</param>
  1442. public void Add(IStaticDataSource dataSource, string entryName)
  1443. {
  1444. if ( dataSource == null ) {
  1445. throw new ArgumentNullException("dataSource");
  1446. }
  1447. if ( entryName == null ) {
  1448. throw new ArgumentNullException("entryName");
  1449. }
  1450. CheckUpdating();
  1451. AddUpdate(new ZipUpdate(dataSource, EntryFactory.MakeFileEntry(entryName, false)));
  1452. }
  1453. /// <summary>
  1454. /// Add a file entry with data.
  1455. /// </summary>
  1456. /// <param name="dataSource">The source of the data for this entry.</param>
  1457. /// <param name="entryName">The name to give to the entry.</param>
  1458. /// <param name="compressionMethod">The compression method to use.</param>
  1459. public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod)
  1460. {
  1461. if ( dataSource == null ) {
  1462. throw new ArgumentNullException("dataSource");
  1463. }
  1464. if ( entryName == null ) {
  1465. throw new ArgumentNullException("entryName");
  1466. }
  1467. CheckUpdating();
  1468. ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false);
  1469. entry.CompressionMethod = compressionMethod;
  1470. AddUpdate(new ZipUpdate(dataSource, entry));
  1471. }
  1472. /// <summary>
  1473. /// Add a file entry with data.
  1474. /// </summary>
  1475. /// <param name="dataSource">The source of the data for this entry.</param>
  1476. /// <param name="entryName">The name to give to the entry.</param>
  1477. /// <param name="compressionMethod">The compression method to use.</param>
  1478. /// <param name="useUnicodeText">Ensure Unicode text is used for name and comments for this entry.</param>
  1479. public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod, bool useUnicodeText)
  1480. {
  1481. if (dataSource == null) {
  1482. throw new ArgumentNullException("dataSource");
  1483. }
  1484. if ( entryName == null ) {
  1485. throw new ArgumentNullException("entryName");
  1486. }
  1487. CheckUpdating();
  1488. ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false);
  1489. entry.IsUnicodeText = useUnicodeText;
  1490. entry.CompressionMethod = compressionMethod;
  1491. AddUpdate(new ZipUpdate(dataSource, entry));
  1492. }
  1493. /// <summary>
  1494. /// Add a <see cref="ZipEntry"/> that contains no data.
  1495. /// </summary>
  1496. /// <param name="entry">The entry to add.</param>
  1497. /// <remarks>This can be used to add directories, volume labels, or empty file entries.</remarks>
  1498. public void Add(ZipEntry entry)
  1499. {
  1500. if ( entry == null ) {
  1501. throw new ArgumentNullException("entry");
  1502. }
  1503. CheckUpdating();
  1504. if ( (entry.Size != 0) || (entry.CompressedSize != 0) ) {
  1505. throw new ZipException("Entry cannot have any data");
  1506. }
  1507. AddUpdate(new ZipUpdate(UpdateCommand.Add, entry));
  1508. }
  1509. /// <summary>
  1510. /// Add a directory entry to the archive.
  1511. /// </summary>
  1512. /// <param name="directoryName">The directory to add.</param>
  1513. public void AddDirectory(string directoryName)
  1514. {
  1515. if ( directoryName == null ) {
  1516. throw new ArgumentNullException("directoryName");
  1517. }
  1518. CheckUpdating();
  1519. ZipEntry dirEntry = EntryFactory.MakeDirectoryEntry(directoryName);
  1520. AddUpdate(new ZipUpdate(UpdateCommand.Add, dirEntry));
  1521. }
  1522. #endregion
  1523. #region Modifying Entries
  1524. /* Modify not yet ready for public consumption.
  1525. Direct modification of an entry should not overwrite original data before its read.
  1526. Safe mode is trivial in this sense.
  1527. public void Modify(ZipEntry original, ZipEntry updated)
  1528. {
  1529. if ( original == null ) {
  1530. throw new ArgumentNullException("original");
  1531. }
  1532. if ( updated == null ) {
  1533. throw new ArgumentNullException("updated");
  1534. }
  1535. CheckUpdating();
  1536. contentsEdited_ = true;
  1537. updates_.Add(new ZipUpdate(original, updated));
  1538. }
  1539. */
  1540. #endregion
  1541. #region Deleting Entries
  1542. /// <summary>
  1543. /// Delete an entry by name
  1544. /// </summary>
  1545. /// <param name="fileName">The filename to delete</param>
  1546. /// <returns>True if the entry was found and deleted; false otherwise.</returns>
  1547. public bool Delete(string fileName)
  1548. {
  1549. if ( fileName == null ) {
  1550. throw new ArgumentNullException("fileName");
  1551. }
  1552. CheckUpdating();
  1553. bool result = false;
  1554. int index = FindExistingUpdate(fileName);
  1555. if ( (index >= 0) && (updates_[index] != null) ) {
  1556. result = true;
  1557. contentsEdited_ = true;
  1558. updates_[index] = null;
  1559. updateCount_ -= 1;
  1560. }
  1561. else {
  1562. throw new ZipException("Cannot find entry to delete");
  1563. }
  1564. return result;
  1565. }
  1566. /// <summary>
  1567. /// Delete a <see cref="ZipEntry"/> from the archive.
  1568. /// </summary>
  1569. /// <param name="entry">The entry to delete.</param>
  1570. public void Delete(ZipEntry entry)
  1571. {
  1572. if ( entry == null ) {
  1573. throw new ArgumentNullException("entry");
  1574. }
  1575. CheckUpdating();
  1576. int index = FindExistingUpdate(entry);
  1577. if ( index >= 0 ) {
  1578. contentsEdited_ = true;
  1579. updates_[index] = null;
  1580. updateCount_ -= 1;
  1581. }
  1582. else {
  1583. throw new ZipException("Cannot find entry to delete");
  1584. }
  1585. }
  1586. #endregion
  1587. #region Update Support
  1588. #region Writing Values/Headers
  1589. void WriteLEShort(int value)
  1590. {
  1591. baseStream_.WriteByte(( byte )(value & 0xff));
  1592. baseStream_.WriteByte(( byte )((value >> 8) & 0xff));
  1593. }
  1594. /// <summary>
  1595. /// Write an unsigned short in little endian byte order.
  1596. /// </summary>
  1597. void WriteLEUshort(ushort value)
  1598. {
  1599. baseStream_.WriteByte(( byte )(value & 0xff));
  1600. baseStream_.WriteByte(( byte )(value >> 8));
  1601. }
  1602. /// <summary>
  1603. /// Write an int in little endian byte order.
  1604. /// </summary>
  1605. void WriteLEInt(int value)
  1606. {
  1607. WriteLEShort(value & 0xffff);
  1608. WriteLEShort(value >> 16);
  1609. }
  1610. /// <summary>
  1611. /// Write an unsigned int in little endian byte order.
  1612. /// </summary>
  1613. void WriteLEUint(uint value)
  1614. {
  1615. WriteLEUshort((ushort)(value & 0xffff));
  1616. WriteLEUshort((ushort)(value >> 16));
  1617. }
  1618. /// <summary>
  1619. /// Write a long in little endian byte order.
  1620. /// </summary>
  1621. void WriteLeLong(long value)
  1622. {
  1623. WriteLEInt(( int )(value & 0xffffffff));
  1624. WriteLEInt(( int )(value >> 32));
  1625. }
  1626. void WriteLEUlong(ulong value)
  1627. {
  1628. WriteLEUint(( uint )(value & 0xffffffff));
  1629. WriteLEUint(( uint )(value >> 32));
  1630. }
  1631. void WriteLocalEntryHeader(ZipUpdate update)
  1632. {
  1633. ZipEntry entry = update.OutEntry;
  1634. // TODO: Local offset will require adjusting for multi-disk zip files.
  1635. entry.Offset = baseStream_.Position;
  1636. // TODO: Need to clear any entry flags that dont make sense or throw an exception here.
  1637. if (update.Command != UpdateCommand.Copy) {
  1638. if (entry.CompressionMethod == CompressionMethod.Deflated) {
  1639. if (entry.Size == 0) {
  1640. // No need to compress - no data.
  1641. entry.CompressedSize = entry.Size;
  1642. entry.Crc = 0;
  1643. entry.CompressionMethod = CompressionMethod.Stored;
  1644. }
  1645. }
  1646. else if (entry.CompressionMethod == CompressionMethod.Stored) {
  1647. entry.Flags &= ~(int)GeneralBitFlags.Descriptor;
  1648. }
  1649. if (HaveKeys) {
  1650. entry.IsCrypted = true;
  1651. if (entry.Crc < 0) {
  1652. entry.Flags |= (int)GeneralBitFlags.Descriptor;
  1653. }
  1654. }
  1655. else {
  1656. entry.IsCrypted = false;
  1657. }
  1658. switch (useZip64_) {
  1659. case UseZip64.Dynamic:
  1660. if (entry.Size < 0) {
  1661. entry.ForceZip64();
  1662. }
  1663. break;
  1664. case UseZip64.On:
  1665. entry.ForceZip64();
  1666. break;
  1667. case UseZip64.Off:
  1668. // Do nothing. The entry itself may be using Zip64 independantly.
  1669. break;
  1670. }
  1671. }
  1672. // Write the local file header
  1673. WriteLEInt(ZipConstants.LocalHeaderSignature);
  1674. WriteLEShort(entry.Version);
  1675. WriteLEShort(entry.Flags);
  1676. WriteLEShort((byte)entry.CompressionMethod);
  1677. WriteLEInt(( int )entry.DosTime);
  1678. if ( !entry.HasCrc ) {
  1679. // Note patch address for updating CRC later.
  1680. update.CrcPatchOffset = baseStream_.Position;
  1681. WriteLEInt(( int )0);
  1682. }
  1683. else {
  1684. WriteLEInt(unchecked(( int )entry.Crc));
  1685. }
  1686. if (entry.LocalHeaderRequiresZip64) {
  1687. WriteLEInt(-1);
  1688. WriteLEInt(-1);
  1689. }
  1690. else {
  1691. if ( (entry.CompressedSize < 0) || (entry.Size < 0) ) {
  1692. update.SizePatchOffset = baseStream_.Position;
  1693. }
  1694. WriteLEInt(( int )entry.CompressedSize);
  1695. WriteLEInt(( int )entry.Size);
  1696. }
  1697. byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
  1698. if ( name.Length > 0xFFFF ) {
  1699. throw new ZipException("Entry name too long.");
  1700. }
  1701. ZipExtraData ed = new ZipExtraData(entry.ExtraData);
  1702. if ( entry.LocalHeaderRequiresZip64 ) {
  1703. ed.StartNewEntry();
  1704. // Local entry header always includes size and compressed size.
  1705. // NOTE the order of these fields is reversed when compared to the normal headers!
  1706. ed.AddLeLong(entry.Size);
  1707. ed.AddLeLong(entry.CompressedSize);
  1708. ed.AddNewEntry(1);
  1709. }
  1710. else {
  1711. ed.Delete(1);
  1712. }
  1713. entry.ExtraData = ed.GetEntryData();
  1714. WriteLEShort(name.Length);
  1715. WriteLEShort(entry.ExtraData.Length);
  1716. if ( name.Length > 0 ) {
  1717. baseStream_.Write(name, 0, name.Length);
  1718. }
  1719. if ( entry.LocalHeaderRequiresZip64 ) {
  1720. if ( !ed.Find(1) ) {
  1721. throw new ZipException("Internal error cannot find extra data");
  1722. }
  1723. update.SizePatchOffset = baseStream_.Position + ed.CurrentReadIndex;
  1724. }
  1725. if ( entry.ExtraData.Length > 0 ) {
  1726. baseStream_.Write(entry.ExtraData, 0, entry.ExtraData.Length);
  1727. }
  1728. }
  1729. int WriteCentralDirectoryHeader(ZipEntry entry)
  1730. {
  1731. if ( entry.CompressedSize < 0 ) {
  1732. throw new ZipException("Attempt to write central directory entry with unknown csize");
  1733. }
  1734. if ( entry.Size < 0 ) {
  1735. throw new ZipException("Attempt to write central directory entry with unknown size");
  1736. }
  1737. if ( entry.Crc < 0 ) {
  1738. throw new ZipException("Attempt to write central directory entry with unknown crc");
  1739. }
  1740. // Write the central file header
  1741. WriteLEInt(ZipConstants.CentralHeaderSignature);
  1742. // Version made by
  1743. WriteLEShort(ZipConstants.VersionMadeBy);
  1744. // Version required to extract
  1745. WriteLEShort(entry.Version);
  1746. WriteLEShort(entry.Flags);
  1747. unchecked {
  1748. WriteLEShort((byte)entry.CompressionMethod);
  1749. WriteLEInt((int)entry.DosTime);
  1750. WriteLEInt((int)entry.Crc);
  1751. }
  1752. if ( (entry.IsZip64Forced()) || (entry.CompressedSize >= 0xffffffff) ) {
  1753. WriteLEInt(-1);
  1754. }
  1755. else {
  1756. WriteLEInt((int)(entry.CompressedSize & 0xffffffff));
  1757. }
  1758. if ( (entry.IsZip64Forced()) || (entry.Size >= 0xffffffff) ) {
  1759. WriteLEInt(-1);
  1760. }
  1761. else {
  1762. WriteLEInt((int)entry.Size);
  1763. }
  1764. byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
  1765. if ( name.Length > 0xFFFF ) {
  1766. throw new ZipException("Entry name is too long.");
  1767. }
  1768. WriteLEShort(name.Length);
  1769. // Central header extra data is different to local header version so regenerate.
  1770. ZipExtraData ed = new ZipExtraData(entry.ExtraData);
  1771. if ( entry.CentralHeaderRequiresZip64 ) {
  1772. ed.StartNewEntry();
  1773. if ( (entry.Size >= 0xffffffff) || (useZip64_ == UseZip64.On) )
  1774. {
  1775. ed.AddLeLong(entry.Size);
  1776. }
  1777. if ( (entry.CompressedSize >= 0xffffffff) || (useZip64_ == UseZip64.On) )
  1778. {
  1779. ed.AddLeLong(entry.CompressedSize);
  1780. }
  1781. if ( entry.Offset >= 0xffffffff ) {
  1782. ed.AddLeLong(entry.Offset);
  1783. }
  1784. // Number of disk on which this file starts isnt supported and is never written here.
  1785. ed.AddNewEntry(1);
  1786. }
  1787. else {
  1788. // Should have already be done when local header was added.
  1789. ed.Delete(1);
  1790. }
  1791. byte[] centralExtraData = ed.GetEntryData();
  1792. WriteLEShort(centralExtraData.Length);
  1793. WriteLEShort(entry.Comment != null ? entry.Comment.Length : 0);
  1794. WriteLEShort(0); // disk number
  1795. WriteLEShort(0); // internal file attributes
  1796. // External file attributes...
  1797. if ( entry.ExternalFileAttributes != -1 ) {
  1798. WriteLEInt(entry.ExternalFileAttributes);
  1799. }
  1800. else {
  1801. if ( entry.IsDirectory ) {
  1802. WriteLEUint(16);
  1803. }
  1804. else {
  1805. WriteLEUint(0);
  1806. }
  1807. }
  1808. if ( entry.Offset >= 0xffffffff ) {
  1809. WriteLEUint(0xffffffff);
  1810. }
  1811. else {
  1812. WriteLEUint((uint)(int)entry.Offset);
  1813. }
  1814. if ( name.Length > 0 ) {
  1815. baseStream_.Write(name, 0, name.Length);
  1816. }
  1817. if ( centralExtraData.Length > 0 ) {
  1818. baseStream_.Write(centralExtraData, 0, centralExtraData.Length);
  1819. }
  1820. byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : new byte[0];
  1821. if ( rawComment.Length > 0 ) {
  1822. baseStream_.Write(rawComment, 0, rawComment.Length);
  1823. }
  1824. return ZipConstants.CentralHeaderBaseSize + name.Length + centralExtraData.Length + rawComment.Length;
  1825. }
  1826. #endregion
  1827. void PostUpdateCleanup()
  1828. {
  1829. updateDataSource_ = null;
  1830. updates_ = null;
  1831. updateIndex_ = null;
  1832. if (archiveStorage_ != null)
  1833. {
  1834. archiveStorage_.Dispose();
  1835. archiveStorage_=null;
  1836. }
  1837. }
  1838. string GetTransformedFileName(string name)
  1839. {
  1840. INameTransform transform = NameTransform;
  1841. return (transform != null) ?
  1842. transform.TransformFile(name) :
  1843. name;
  1844. }
  1845. string GetTransformedDirectoryName(string name)
  1846. {
  1847. INameTransform transform = NameTransform;
  1848. return (transform != null) ?
  1849. transform.TransformDirectory(name) :
  1850. name;
  1851. }
  1852. /// <summary>
  1853. /// Get a raw memory buffer.
  1854. /// </summary>
  1855. /// <returns>Returns a raw memory buffer.</returns>
  1856. byte[] GetBuffer()
  1857. {
  1858. if ( copyBuffer_ == null ) {
  1859. copyBuffer_ = new byte[bufferSize_];
  1860. }
  1861. return copyBuffer_;
  1862. }
  1863. void CopyDescriptorBytes(ZipUpdate update, Stream dest, Stream source)
  1864. {
  1865. int bytesToCopy = GetDescriptorSize(update);
  1866. if ( bytesToCopy > 0 ) {
  1867. byte[] buffer = GetBuffer();
  1868. while ( bytesToCopy > 0 ) {
  1869. int readSize = Math.Min(buffer.Length, bytesToCopy);
  1870. int bytesRead = source.Read(buffer, 0, readSize);
  1871. if ( bytesRead > 0 ) {
  1872. dest.Write(buffer, 0, bytesRead);
  1873. bytesToCopy -= bytesRead;
  1874. }
  1875. else {
  1876. throw new ZipException("Unxpected end of stream");
  1877. }
  1878. }
  1879. }
  1880. }
  1881. void CopyBytes(ZipUpdate update, Stream destination, Stream source,
  1882. long bytesToCopy, bool updateCrc)
  1883. {
  1884. if ( destination == source ) {
  1885. throw new InvalidOperationException("Destination and source are the same");
  1886. }
  1887. // NOTE: Compressed size is updated elsewhere.
  1888. Crc32 crc = new Crc32();
  1889. byte[] buffer = GetBuffer();
  1890. long targetBytes = bytesToCopy;
  1891. long totalBytesRead = 0;
  1892. int bytesRead;
  1893. do {
  1894. int readSize = buffer.Length;
  1895. if ( bytesToCopy < readSize ) {
  1896. readSize = (int)bytesToCopy;
  1897. }
  1898. bytesRead = source.Read(buffer, 0, readSize);
  1899. if ( bytesRead > 0 ) {
  1900. if ( updateCrc ) {
  1901. crc.Update(buffer, 0, bytesRead);
  1902. }
  1903. destination.Write(buffer, 0, bytesRead);
  1904. bytesToCopy -= bytesRead;
  1905. totalBytesRead += bytesRead;
  1906. }
  1907. }
  1908. while ( (bytesRead > 0) && (bytesToCopy > 0) );
  1909. if ( totalBytesRead != targetBytes ) {
  1910. throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", targetBytes, totalBytesRead));
  1911. }
  1912. if ( updateCrc ) {
  1913. update.OutEntry.Crc = crc.Value;
  1914. }
  1915. }
  1916. /// <summary>
  1917. /// Get the size of the source descriptor for a <see cref="ZipUpdate"/>.
  1918. /// </summary>
  1919. /// <param name="update">The update to get the size for.</param>
  1920. /// <returns>The descriptor size, zero if there isnt one.</returns>
  1921. int GetDescriptorSize(ZipUpdate update)
  1922. {
  1923. int result = 0;
  1924. if ( (update.Entry.Flags & (int)GeneralBitFlags.Descriptor) != 0) {
  1925. result = ZipConstants.DataDescriptorSize - 4;
  1926. if ( update.Entry.LocalHeaderRequiresZip64 ) {
  1927. result = ZipConstants.Zip64DataDescriptorSize - 4;
  1928. }
  1929. }
  1930. return result;
  1931. }
  1932. void CopyDescriptorBytesDirect(ZipUpdate update, Stream stream, ref long destinationPosition, long sourcePosition)
  1933. {
  1934. int bytesToCopy = GetDescriptorSize(update);
  1935. while ( bytesToCopy > 0 ) {
  1936. int readSize = (int)bytesToCopy;
  1937. byte[] buffer = GetBuffer();
  1938. stream.Position = sourcePosition;
  1939. int bytesRead = stream.Read(buffer, 0, readSize);
  1940. if ( bytesRead > 0 ) {
  1941. stream.Position = destinationPosition;
  1942. stream.Write(buffer, 0, bytesRead);
  1943. bytesToCopy -= bytesRead;
  1944. destinationPosition += bytesRead;
  1945. sourcePosition += bytesRead;
  1946. }
  1947. else {
  1948. throw new ZipException("Unxpected end of stream");
  1949. }
  1950. }
  1951. }
  1952. void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref long destinationPosition, ref long sourcePosition)
  1953. {
  1954. long bytesToCopy = update.Entry.CompressedSize;
  1955. // NOTE: Compressed size is updated elsewhere.
  1956. Crc32 crc = new Crc32();
  1957. byte[] buffer = GetBuffer();
  1958. long targetBytes = bytesToCopy;
  1959. long totalBytesRead = 0;
  1960. int bytesRead;
  1961. do
  1962. {
  1963. int readSize = buffer.Length;
  1964. if ( bytesToCopy < readSize ) {
  1965. readSize = (int)bytesToCopy;
  1966. }
  1967. stream.Position = sourcePosition;
  1968. bytesRead = stream.Read(buffer, 0, readSize);
  1969. if ( bytesRead > 0 ) {
  1970. if ( updateCrc ) {
  1971. crc.Update(buffer, 0, bytesRead);
  1972. }
  1973. stream.Position = destinationPosition;
  1974. stream.Write(buffer, 0, bytesRead);
  1975. destinationPosition += bytesRead;
  1976. sourcePosition += bytesRead;
  1977. bytesToCopy -= bytesRead;
  1978. totalBytesRead += bytesRead;
  1979. }
  1980. }
  1981. while ( (bytesRead > 0) && (bytesToCopy > 0) );
  1982. if ( totalBytesRead != targetBytes ) {
  1983. throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", targetBytes, totalBytesRead));
  1984. }
  1985. if ( updateCrc ) {
  1986. update.OutEntry.Crc = crc.Value;
  1987. }
  1988. }
  1989. int FindExistingUpdate(ZipEntry entry)
  1990. {
  1991. int result = -1;
  1992. string convertedName = GetTransformedFileName(entry.Name);
  1993. if (updateIndex_.ContainsKey(convertedName)) {
  1994. result = (int)updateIndex_[convertedName];
  1995. }
  1996. /*
  1997. // This is slow like the coming of the next ice age but takes less storage and may be useful
  1998. // for CF?
  1999. for (int index = 0; index < updates_.Count; ++index)
  2000. {
  2001. ZipUpdate zu = ( ZipUpdate )updates_[index];
  2002. if ( (zu.Entry.ZipFileIndex == entry.ZipFileIndex) &&
  2003. (string.Compare(convertedName, zu.Entry.Name, true, CultureInfo.InvariantCulture) == 0) ) {
  2004. result = index;
  2005. break;
  2006. }
  2007. }
  2008. */
  2009. return result;
  2010. }
  2011. int FindExistingUpdate(string fileName)
  2012. {
  2013. int result = -1;
  2014. string convertedName = GetTransformedFileName(fileName);
  2015. if (updateIndex_.ContainsKey(convertedName)) {
  2016. result = (int)updateIndex_[convertedName];
  2017. }
  2018. /*
  2019. // This is slow like the coming of the next ice age but takes less storage and may be useful
  2020. // for CF?
  2021. for ( int index = 0; index < updates_.Count; ++index ) {
  2022. if ( string.Compare(convertedName, (( ZipUpdate )updates_[index]).Entry.Name,
  2023. true, CultureInfo.InvariantCulture) == 0 ) {
  2024. result = index;
  2025. break;
  2026. }
  2027. }
  2028. */
  2029. return result;
  2030. }
  2031. /// <summary>
  2032. /// Get an output stream for the specified <see cref="ZipEntry"/>
  2033. /// </summary>
  2034. /// <param name="entry">The entry to get an output stream for.</param>
  2035. /// <returns>The output stream obtained for the entry.</returns>
  2036. Stream GetOutputStream(ZipEntry entry)
  2037. {
  2038. Stream result = baseStream_;
  2039. if ( entry.IsCrypted == true ) {
  2040. #if NETCF_1_0
  2041. throw new ZipException("Encryption not supported for Compact Framework 1.0");
  2042. #else
  2043. result = CreateAndInitEncryptionStream(result, entry);
  2044. #endif
  2045. }
  2046. switch ( entry.CompressionMethod ) {
  2047. case CompressionMethod.Stored:
  2048. result = new UncompressedStream(result);
  2049. break;
  2050. case CompressionMethod.Deflated:
  2051. DeflaterOutputStream dos = new DeflaterOutputStream(result, new Deflater(9, true));
  2052. dos.IsStreamOwner = false;
  2053. result = dos;
  2054. break;
  2055. default:
  2056. throw new ZipException("Unknown compression method " + entry.CompressionMethod);
  2057. }
  2058. return result;
  2059. }
  2060. void AddEntry(ZipFile workFile, ZipUpdate update)
  2061. {
  2062. Stream source = null;
  2063. if ( update.Entry.IsFile ) {
  2064. source = update.GetSource();
  2065. if ( source == null ) {
  2066. source = updateDataSource_.GetSource(update.Entry, update.Filename);
  2067. }
  2068. }
  2069. if ( source != null ) {
  2070. using ( source ) {
  2071. long sourceStreamLength = source.Length;
  2072. if ( update.OutEntry.Size < 0 ) {
  2073. update.OutEntry.Size = sourceStreamLength;
  2074. }
  2075. else {
  2076. // Check for errant entries.
  2077. if ( update.OutEntry.Size != sourceStreamLength ) {
  2078. throw new ZipException("Entry size/stream size mismatch");
  2079. }
  2080. }
  2081. workFile.WriteLocalEntryHeader(update);
  2082. long dataStart = workFile.baseStream_.Position;
  2083. using ( Stream output = workFile.GetOutputStream(update.OutEntry) ) {
  2084. CopyBytes(update, output, source, sourceStreamLength, true);
  2085. }
  2086. long dataEnd = workFile.baseStream_.Position;
  2087. update.OutEntry.CompressedSize = dataEnd - dataStart;
  2088. if ((update.OutEntry.Flags & (int)GeneralBitFlags.Descriptor) == (int)GeneralBitFlags.Descriptor)
  2089. {
  2090. ZipHelperStream helper = new ZipHelperStream(workFile.baseStream_);
  2091. helper.WriteDataDescriptor(update.OutEntry);
  2092. }
  2093. }
  2094. }
  2095. else {
  2096. workFile.WriteLocalEntryHeader(update);
  2097. update.OutEntry.CompressedSize = 0;
  2098. }
  2099. }
  2100. void ModifyEntry(ZipFile workFile, ZipUpdate update)
  2101. {
  2102. workFile.WriteLocalEntryHeader(update);
  2103. long dataStart = workFile.baseStream_.Position;
  2104. // TODO: This is slow if the changes don't effect the data!!
  2105. if ( update.Entry.IsFile && (update.Filename != null) ) {
  2106. using ( Stream output = workFile.GetOutputStream(update.OutEntry) ) {
  2107. using ( Stream source = this.GetInputStream(update.Entry) ) {
  2108. CopyBytes(update, output, source, source.Length, true);
  2109. }
  2110. }
  2111. }
  2112. long dataEnd = workFile.baseStream_.Position;
  2113. update.Entry.CompressedSize = dataEnd - dataStart;
  2114. }
  2115. void CopyEntryDirect(ZipFile workFile, ZipUpdate update, ref long destinationPosition)
  2116. {
  2117. bool skipOver = false;
  2118. if ( update.Entry.Offset == destinationPosition ) {
  2119. skipOver = true;
  2120. }
  2121. if ( !skipOver ) {
  2122. baseStream_.Position = destinationPosition;
  2123. workFile.WriteLocalEntryHeader(update);
  2124. destinationPosition = baseStream_.Position;
  2125. }
  2126. long sourcePosition = 0;
  2127. const int NameLengthOffset = 26;
  2128. // TODO: Add base for SFX friendly handling
  2129. long entryDataOffset = update.Entry.Offset + NameLengthOffset;
  2130. baseStream_.Seek(entryDataOffset, SeekOrigin.Begin);
  2131. // Clumsy way of handling retrieving the original name and extra data length for now.
  2132. // TODO: Stop re-reading name and data length in CopyEntryDirect.
  2133. uint nameLength = ReadLEUshort();
  2134. uint extraLength = ReadLEUshort();
  2135. sourcePosition = baseStream_.Position + nameLength + extraLength;
  2136. if (skipOver) {
  2137. if (update.OffsetBasedSize != -1)
  2138. destinationPosition += update.OffsetBasedSize;
  2139. else
  2140. // TODO: Find out why this calculation comes up 4 bytes short on some entries in ODT (Office Document Text) archives.
  2141. // WinZip produces a warning on these entries:
  2142. // "caution: value of lrec.csize (compressed size) changed from ..."
  2143. destinationPosition +=
  2144. (sourcePosition - entryDataOffset) + NameLengthOffset + // Header size
  2145. update.Entry.CompressedSize + GetDescriptorSize(update);
  2146. }
  2147. else {
  2148. if ( update.Entry.CompressedSize > 0 ) {
  2149. CopyEntryDataDirect(update, baseStream_, false, ref destinationPosition, ref sourcePosition );
  2150. }
  2151. CopyDescriptorBytesDirect(update, baseStream_, ref destinationPosition, sourcePosition);
  2152. }
  2153. }
  2154. void CopyEntry(ZipFile workFile, ZipUpdate update)
  2155. {
  2156. workFile.WriteLocalEntryHeader(update);
  2157. if ( update.Entry.CompressedSize > 0 ) {
  2158. const int NameLengthOffset = 26;
  2159. long entryDataOffset = update.Entry.Offset + NameLengthOffset;
  2160. // TODO: This wont work for SFX files!
  2161. baseStream_.Seek(entryDataOffset, SeekOrigin.Begin);
  2162. uint nameLength = ReadLEUshort();
  2163. uint extraLength = ReadLEUshort();
  2164. baseStream_.Seek(nameLength + extraLength, SeekOrigin.Current);
  2165. CopyBytes(update, workFile.baseStream_, baseStream_, update.Entry.CompressedSize, false);
  2166. }
  2167. CopyDescriptorBytes(update, workFile.baseStream_, baseStream_);
  2168. }
  2169. void Reopen(Stream source)
  2170. {
  2171. if ( source == null ) {
  2172. throw new ZipException("Failed to reopen archive - no source");
  2173. }
  2174. isNewArchive_ = false;
  2175. baseStream_ = source;
  2176. ReadEntries();
  2177. }
  2178. void Reopen()
  2179. {
  2180. if (Name == null) {
  2181. throw new InvalidOperationException("Name is not known cannot Reopen");
  2182. }
  2183. Reopen(File.Open(Name, FileMode.Open, FileAccess.Read, FileShare.Read));
  2184. }
  2185. void UpdateCommentOnly()
  2186. {
  2187. long baseLength = baseStream_.Length;
  2188. ZipHelperStream updateFile = null;
  2189. if ( archiveStorage_.UpdateMode == FileUpdateMode.Safe ) {
  2190. Stream copyStream = archiveStorage_.MakeTemporaryCopy(baseStream_);
  2191. updateFile = new ZipHelperStream(copyStream);
  2192. updateFile.IsStreamOwner = true;
  2193. baseStream_.Close();
  2194. baseStream_ = null;
  2195. }
  2196. else {
  2197. if (archiveStorage_.UpdateMode == FileUpdateMode.Direct) {
  2198. // TODO: archiveStorage wasnt originally intended for this use.
  2199. // Need to revisit this to tidy up handling as archive storage currently doesnt
  2200. // handle the original stream well.
  2201. // The problem is when using an existing zip archive with an in memory archive storage.
  2202. // The open stream wont support writing but the memory storage should open the same file not an in memory one.
  2203. // Need to tidy up the archive storage interface and contract basically.
  2204. baseStream_ = archiveStorage_.OpenForDirectUpdate(baseStream_);
  2205. updateFile = new ZipHelperStream(baseStream_);
  2206. }
  2207. else {
  2208. baseStream_.Close();
  2209. baseStream_ = null;
  2210. updateFile = new ZipHelperStream(Name);
  2211. }
  2212. }
  2213. using ( updateFile ) {
  2214. long locatedCentralDirOffset =
  2215. updateFile.LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature,
  2216. baseLength, ZipConstants.EndOfCentralRecordBaseSize, 0xffff);
  2217. if ( locatedCentralDirOffset < 0 ) {
  2218. throw new ZipException("Cannot find central directory");
  2219. }
  2220. const int CentralHeaderCommentSizeOffset = 16;
  2221. updateFile.Position += CentralHeaderCommentSizeOffset;
  2222. byte[] rawComment = newComment_.RawComment;
  2223. updateFile.WriteLEShort(rawComment.Length);
  2224. updateFile.Write(rawComment, 0, rawComment.Length);
  2225. updateFile.SetLength(updateFile.Position);
  2226. }
  2227. if ( archiveStorage_.UpdateMode == FileUpdateMode.Safe ) {
  2228. Reopen(archiveStorage_.ConvertTemporaryToFinal());
  2229. }
  2230. else {
  2231. ReadEntries();
  2232. }
  2233. }
  2234. /// <summary>
  2235. /// Class used to sort updates.
  2236. /// </summary>
  2237. class UpdateComparer : IComparer
  2238. {
  2239. /// <summary>
  2240. /// Compares two objects and returns a value indicating whether one is
  2241. /// less than, equal to or greater than the other.
  2242. /// </summary>
  2243. /// <param name="x">First object to compare</param>
  2244. /// <param name="y">Second object to compare.</param>
  2245. /// <returns>Compare result.</returns>
  2246. public int Compare(
  2247. object x,
  2248. object y)
  2249. {
  2250. ZipUpdate zx = x as ZipUpdate;
  2251. ZipUpdate zy = y as ZipUpdate;
  2252. int result;
  2253. if (zx == null) {
  2254. if (zy == null) {
  2255. result = 0;
  2256. }
  2257. else {
  2258. result = -1;
  2259. }
  2260. }
  2261. else if (zy == null) {
  2262. result = 1;
  2263. }
  2264. else {
  2265. int xCmdValue = ((zx.Command == UpdateCommand.Copy) || (zx.Command == UpdateCommand.Modify)) ? 0 : 1;
  2266. int yCmdValue = ((zy.Command == UpdateCommand.Copy) || (zy.Command == UpdateCommand.Modify)) ? 0 : 1;
  2267. result = xCmdValue - yCmdValue;
  2268. if (result == 0) {
  2269. long offsetDiff = zx.Entry.Offset - zy.Entry.Offset;
  2270. if (offsetDiff < 0) {
  2271. result = -1;
  2272. }
  2273. else if (offsetDiff == 0) {
  2274. result = 0;
  2275. }
  2276. else {
  2277. result = 1;
  2278. }
  2279. }
  2280. }
  2281. return result;
  2282. }
  2283. }
  2284. void RunUpdates()
  2285. {
  2286. long sizeEntries = 0;
  2287. long endOfStream = 0;
  2288. bool directUpdate = false;
  2289. long destinationPosition = 0; // NOT SFX friendly
  2290. ZipFile workFile;
  2291. if ( IsNewArchive ) {
  2292. workFile = this;
  2293. workFile.baseStream_.Position = 0;
  2294. directUpdate = true;
  2295. }
  2296. else if ( archiveStorage_.UpdateMode == FileUpdateMode.Direct ) {
  2297. workFile = this;
  2298. workFile.baseStream_.Position = 0;
  2299. directUpdate = true;
  2300. // Sort the updates by offset within copies/modifies, then adds.
  2301. // This ensures that data required by copies will not be overwritten.
  2302. updates_.Sort(new UpdateComparer());
  2303. }
  2304. else {
  2305. workFile = ZipFile.Create(archiveStorage_.GetTemporaryOutput());
  2306. workFile.UseZip64 = UseZip64;
  2307. if (key != null) {
  2308. workFile.key = (byte[])key.Clone();
  2309. }
  2310. }
  2311. try {
  2312. foreach ( ZipUpdate update in updates_ ) {
  2313. if (update != null) {
  2314. switch (update.Command) {
  2315. case UpdateCommand.Copy:
  2316. if (directUpdate) {
  2317. CopyEntryDirect(workFile, update, ref destinationPosition);
  2318. }
  2319. else {
  2320. CopyEntry(workFile, update);
  2321. }
  2322. break;
  2323. case UpdateCommand.Modify:
  2324. // TODO: Direct modifying of an entry will take some legwork.
  2325. ModifyEntry(workFile, update);
  2326. break;
  2327. case UpdateCommand.Add:
  2328. if (!IsNewArchive && directUpdate) {
  2329. workFile.baseStream_.Position = destinationPosition;
  2330. }
  2331. AddEntry(workFile, update);
  2332. if (directUpdate) {
  2333. destinationPosition = workFile.baseStream_.Position;
  2334. }
  2335. break;
  2336. }
  2337. }
  2338. }
  2339. if ( !IsNewArchive && directUpdate ) {
  2340. workFile.baseStream_.Position = destinationPosition;
  2341. }
  2342. long centralDirOffset = workFile.baseStream_.Position;
  2343. foreach ( ZipUpdate update in updates_ ) {
  2344. if (update != null) {
  2345. sizeEntries += workFile.WriteCentralDirectoryHeader(update.OutEntry);
  2346. }
  2347. }
  2348. byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_);
  2349. using ( ZipHelperStream zhs = new ZipHelperStream(workFile.baseStream_) ) {
  2350. zhs.WriteEndOfCentralDirectory(updateCount_, sizeEntries, centralDirOffset, theComment);
  2351. }
  2352. endOfStream = workFile.baseStream_.Position;
  2353. // And now patch entries...
  2354. foreach ( ZipUpdate update in updates_ ) {
  2355. if (update != null)
  2356. {
  2357. // If the size of the entry is zero leave the crc as 0 as well.
  2358. // The calculated crc will be all bits on...
  2359. if ((update.CrcPatchOffset > 0) && (update.OutEntry.CompressedSize > 0)) {
  2360. workFile.baseStream_.Position = update.CrcPatchOffset;
  2361. workFile.WriteLEInt((int)update.OutEntry.Crc);
  2362. }
  2363. if (update.SizePatchOffset > 0) {
  2364. workFile.baseStream_.Position = update.SizePatchOffset;
  2365. if (update.OutEntry.LocalHeaderRequiresZip64) {
  2366. workFile.WriteLeLong(update.OutEntry.Size);
  2367. workFile.WriteLeLong(update.OutEntry.CompressedSize);
  2368. }
  2369. else {
  2370. workFile.WriteLEInt((int)update.OutEntry.CompressedSize);
  2371. workFile.WriteLEInt((int)update.OutEntry.Size);
  2372. }
  2373. }
  2374. }
  2375. }
  2376. }
  2377. catch {
  2378. workFile.Close();
  2379. if (!directUpdate && (workFile.Name != null)) {
  2380. File.Delete(workFile.Name);
  2381. }
  2382. throw;
  2383. }
  2384. if (directUpdate) {
  2385. workFile.baseStream_.SetLength(endOfStream);
  2386. workFile.baseStream_.Flush();
  2387. isNewArchive_ = false;
  2388. ReadEntries();
  2389. }
  2390. else {
  2391. baseStream_.Close();
  2392. Reopen(archiveStorage_.ConvertTemporaryToFinal());
  2393. }
  2394. }
  2395. void CheckUpdating()
  2396. {
  2397. if ( updates_ == null ) {
  2398. throw new InvalidOperationException("BeginUpdate has not been called");
  2399. }
  2400. }
  2401. #endregion
  2402. #region ZipUpdate class
  2403. /// <summary>
  2404. /// Represents a pending update to a Zip file.
  2405. /// </summary>
  2406. class ZipUpdate
  2407. {
  2408. #region Constructors
  2409. public ZipUpdate(string fileName, ZipEntry entry)
  2410. {
  2411. command_ = UpdateCommand.Add;
  2412. entry_ = entry;
  2413. filename_ = fileName;
  2414. }
  2415. [Obsolete]
  2416. public ZipUpdate(string fileName, string entryName, CompressionMethod compressionMethod)
  2417. {
  2418. command_ = UpdateCommand.Add;
  2419. entry_ = new ZipEntry(entryName);
  2420. entry_.CompressionMethod = compressionMethod;
  2421. filename_ = fileName;
  2422. }
  2423. [Obsolete]
  2424. public ZipUpdate(string fileName, string entryName)
  2425. : this(fileName, entryName, CompressionMethod.Deflated)
  2426. {
  2427. // Do nothing.
  2428. }
  2429. [Obsolete]
  2430. public ZipUpdate(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod)
  2431. {
  2432. command_ = UpdateCommand.Add;
  2433. entry_ = new ZipEntry(entryName);
  2434. entry_.CompressionMethod = compressionMethod;
  2435. dataSource_ = dataSource;
  2436. }
  2437. public ZipUpdate(IStaticDataSource dataSource, ZipEntry entry)
  2438. {
  2439. command_ = UpdateCommand.Add;
  2440. entry_ = entry;
  2441. dataSource_ = dataSource;
  2442. }
  2443. public ZipUpdate(ZipEntry original, ZipEntry updated)
  2444. {
  2445. throw new ZipException("Modify not currently supported");
  2446. /*
  2447. command_ = UpdateCommand.Modify;
  2448. entry_ = ( ZipEntry )original.Clone();
  2449. outEntry_ = ( ZipEntry )updated.Clone();
  2450. */
  2451. }
  2452. public ZipUpdate(UpdateCommand command, ZipEntry entry)
  2453. {
  2454. command_ = command;
  2455. entry_ = ( ZipEntry )entry.Clone();
  2456. }
  2457. /// <summary>
  2458. /// Copy an existing entry.
  2459. /// </summary>
  2460. /// <param name="entry">The existing entry to copy.</param>
  2461. public ZipUpdate(ZipEntry entry)
  2462. : this(UpdateCommand.Copy, entry)
  2463. {
  2464. // Do nothing.
  2465. }
  2466. #endregion
  2467. /// <summary>
  2468. /// Get the <see cref="ZipEntry"/> for this update.
  2469. /// </summary>
  2470. /// <remarks>This is the source or original entry.</remarks>
  2471. public ZipEntry Entry
  2472. {
  2473. get { return entry_; }
  2474. }
  2475. /// <summary>
  2476. /// Get the <see cref="ZipEntry"/> that will be written to the updated/new file.
  2477. /// </summary>
  2478. public ZipEntry OutEntry
  2479. {
  2480. get {
  2481. if ( outEntry_ == null ) {
  2482. outEntry_ = (ZipEntry)entry_.Clone();
  2483. }
  2484. return outEntry_;
  2485. }
  2486. }
  2487. /// <summary>
  2488. /// Get the command for this update.
  2489. /// </summary>
  2490. public UpdateCommand Command
  2491. {
  2492. get { return command_; }
  2493. }
  2494. /// <summary>
  2495. /// Get the filename if any for this update. Null if none exists.
  2496. /// </summary>
  2497. public string Filename
  2498. {
  2499. get { return filename_; }
  2500. }
  2501. /// <summary>
  2502. /// Get/set the location of the size patch for this update.
  2503. /// </summary>
  2504. public long SizePatchOffset
  2505. {
  2506. get { return sizePatchOffset_; }
  2507. set { sizePatchOffset_ = value; }
  2508. }
  2509. /// <summary>
  2510. /// Get /set the location of the crc patch for this update.
  2511. /// </summary>
  2512. public long CrcPatchOffset
  2513. {
  2514. get { return crcPatchOffset_; }
  2515. set { crcPatchOffset_ = value; }
  2516. }
  2517. /// <summary>
  2518. /// Get/set the size calculated by offset.
  2519. /// Specifically, the difference between this and next entry's starting offset.
  2520. /// </summary>
  2521. public long OffsetBasedSize
  2522. {
  2523. get { return _offsetBasedSize; }
  2524. set { _offsetBasedSize = value; }
  2525. }
  2526. public Stream GetSource()
  2527. {
  2528. Stream result = null;
  2529. if ( dataSource_ != null ) {
  2530. result = dataSource_.GetSource();
  2531. }
  2532. return result;
  2533. }
  2534. #region Instance Fields
  2535. ZipEntry entry_;
  2536. ZipEntry outEntry_;
  2537. UpdateCommand command_;
  2538. IStaticDataSource dataSource_;
  2539. string filename_;
  2540. long sizePatchOffset_ = -1;
  2541. long crcPatchOffset_ = -1;
  2542. long _offsetBasedSize = -1;
  2543. #endregion
  2544. }
  2545. #endregion
  2546. #endregion
  2547. #region Disposing
  2548. #region IDisposable Members
  2549. void IDisposable.Dispose()
  2550. {
  2551. Close();
  2552. }
  2553. #endregion
  2554. void DisposeInternal(bool disposing)
  2555. {
  2556. if ( !isDisposed_ ) {
  2557. isDisposed_ = true;
  2558. entries_ = new ZipEntry[0];
  2559. if ( IsStreamOwner && (baseStream_ != null) ) {
  2560. lock(baseStream_) {
  2561. baseStream_.Close();
  2562. }
  2563. }
  2564. PostUpdateCleanup();
  2565. }
  2566. }
  2567. /// <summary>
  2568. /// Releases the unmanaged resources used by the this instance and optionally releases the managed resources.
  2569. /// </summary>
  2570. /// <param name="disposing">true to release both managed and unmanaged resources;
  2571. /// false to release only unmanaged resources.</param>
  2572. protected virtual void Dispose(bool disposing)
  2573. {
  2574. DisposeInternal(disposing);
  2575. }
  2576. #endregion
  2577. #region Internal routines
  2578. #region Reading
  2579. /// <summary>
  2580. /// Read an unsigned short in little endian byte order.
  2581. /// </summary>
  2582. /// <returns>Returns the value read.</returns>
  2583. /// <exception cref="EndOfStreamException">
  2584. /// The stream ends prematurely
  2585. /// </exception>
  2586. ushort ReadLEUshort()
  2587. {
  2588. int data1 = baseStream_.ReadByte();
  2589. if ( data1 < 0 ) {
  2590. throw new EndOfStreamException("End of stream");
  2591. }
  2592. int data2 = baseStream_.ReadByte();
  2593. if ( data2 < 0 ) {
  2594. throw new EndOfStreamException("End of stream");
  2595. }
  2596. return unchecked((ushort)((ushort)data1 | (ushort)(data2 << 8)));
  2597. }
  2598. /// <summary>
  2599. /// Read a uint in little endian byte order.
  2600. /// </summary>
  2601. /// <returns>Returns the value read.</returns>
  2602. /// <exception cref="IOException">
  2603. /// An i/o error occurs.
  2604. /// </exception>
  2605. /// <exception cref="System.IO.EndOfStreamException">
  2606. /// The file ends prematurely
  2607. /// </exception>
  2608. uint ReadLEUint()
  2609. {
  2610. return (uint)(ReadLEUshort() | (((uint)ReadLEUshort()) << 16));
  2611. }
  2612. ulong ReadLEUlong()
  2613. {
  2614. return ReadLEUint() | (((ulong)ReadLEUint()) << 32);
  2615. }
  2616. #endregion
  2617. // NOTE this returns the offset of the first byte after the signature.
  2618. long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData)
  2619. {
  2620. using ( ZipHelperStream les = new ZipHelperStream(baseStream_) ) {
  2621. return les.LocateBlockWithSignature(signature, endLocation, minimumBlockSize, maximumVariableData);
  2622. }
  2623. }
  2624. /// <summary>
  2625. /// Search for and read the central directory of a zip file filling the entries array.
  2626. /// </summary>
  2627. /// <exception cref="System.IO.IOException">
  2628. /// An i/o error occurs.
  2629. /// </exception>
  2630. /// <exception cref="CommonMPQ.SharpZipLib.Zip.ZipException">
  2631. /// The central directory is malformed or cannot be found
  2632. /// </exception>
  2633. void ReadEntries()
  2634. {
  2635. // Search for the End Of Central Directory. When a zip comment is
  2636. // present the directory will start earlier
  2637. //
  2638. // The search is limited to 64K which is the maximum size of a trailing comment field to aid speed.
  2639. // This should be compatible with both SFX and ZIP files but has only been tested for Zip files
  2640. // If a SFX file has the Zip data attached as a resource and there are other resources occuring later then
  2641. // this could be invalid.
  2642. // Could also speed this up by reading memory in larger blocks.
  2643. if (baseStream_.CanSeek == false) {
  2644. throw new ZipException("ZipFile stream must be seekable");
  2645. }
  2646. long locatedEndOfCentralDir = LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature,
  2647. baseStream_.Length, ZipConstants.EndOfCentralRecordBaseSize, 0xffff);
  2648. if (locatedEndOfCentralDir < 0) {
  2649. throw new ZipException("Cannot find central directory");
  2650. }
  2651. // Read end of central directory record
  2652. ushort thisDiskNumber = ReadLEUshort();
  2653. ushort startCentralDirDisk = ReadLEUshort();
  2654. ulong entriesForThisDisk = ReadLEUshort();
  2655. ulong entriesForWholeCentralDir = ReadLEUshort();
  2656. ulong centralDirSize = ReadLEUint();
  2657. long offsetOfCentralDir = ReadLEUint();
  2658. uint commentSize = ReadLEUshort();
  2659. if ( commentSize > 0 ) {
  2660. byte[] comment = new byte[commentSize];
  2661. StreamUtils.ReadFully(baseStream_, comment);
  2662. comment_ = ZipConstants.ConvertToString(comment);
  2663. }
  2664. else {
  2665. comment_ = string.Empty;
  2666. }
  2667. bool isZip64 = false;
  2668. // Check if zip64 header information is required.
  2669. if ( (thisDiskNumber == 0xffff) ||
  2670. (startCentralDirDisk == 0xffff) ||
  2671. (entriesForThisDisk == 0xffff) ||
  2672. (entriesForWholeCentralDir == 0xffff) ||
  2673. (centralDirSize == 0xffffffff) ||
  2674. (offsetOfCentralDir == 0xffffffff) ) {
  2675. isZip64 = true;
  2676. long offset = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000);
  2677. if ( offset < 0 ) {
  2678. throw new ZipException("Cannot find Zip64 locator");
  2679. }
  2680. // number of the disk with the start of the zip64 end of central directory 4 bytes
  2681. // relative offset of the zip64 end of central directory record 8 bytes
  2682. // total number of disks 4 bytes
  2683. ReadLEUint(); // startDisk64 is not currently used
  2684. ulong offset64 = ReadLEUlong();
  2685. uint totalDisks = ReadLEUint();
  2686. baseStream_.Position = (long)offset64;
  2687. long sig64 = ReadLEUint();
  2688. if ( sig64 != ZipConstants.Zip64CentralFileHeaderSignature ) {
  2689. throw new ZipException(string.Format("Invalid Zip64 Central directory signature at {0:X}", offset64));
  2690. }
  2691. // NOTE: Record size = SizeOfFixedFields + SizeOfVariableData - 12.
  2692. ulong recordSize = ReadLEUlong();
  2693. int versionMadeBy = ReadLEUshort();
  2694. int versionToExtract = ReadLEUshort();
  2695. uint thisDisk = ReadLEUint();
  2696. uint centralDirDisk = ReadLEUint();
  2697. entriesForThisDisk = ReadLEUlong();
  2698. entriesForWholeCentralDir = ReadLEUlong();
  2699. centralDirSize = ReadLEUlong();
  2700. offsetOfCentralDir = (long)ReadLEUlong();
  2701. // NOTE: zip64 extensible data sector (variable size) is ignored.
  2702. }
  2703. entries_ = new ZipEntry[entriesForThisDisk];
  2704. // SFX/embedded support, find the offset of the first entry vis the start of the stream
  2705. // This applies to Zip files that are appended to the end of an SFX stub.
  2706. // Or are appended as a resource to an executable.
  2707. // Zip files created by some archivers have the offsets altered to reflect the true offsets
  2708. // and so dont require any adjustment here...
  2709. // TODO: Difficulty with Zip64 and SFX offset handling needs resolution - maths?
  2710. if ( !isZip64 && (offsetOfCentralDir < locatedEndOfCentralDir - (4 + (long)centralDirSize)) ) {
  2711. offsetOfFirstEntry = locatedEndOfCentralDir - (4 + (long)centralDirSize + offsetOfCentralDir);
  2712. if (offsetOfFirstEntry <= 0) {
  2713. throw new ZipException("Invalid embedded zip archive");
  2714. }
  2715. }
  2716. baseStream_.Seek(offsetOfFirstEntry + offsetOfCentralDir, SeekOrigin.Begin);
  2717. for (ulong i = 0; i < entriesForThisDisk; i++) {
  2718. if (ReadLEUint() != ZipConstants.CentralHeaderSignature) {
  2719. throw new ZipException("Wrong Central Directory signature");
  2720. }
  2721. int versionMadeBy = ReadLEUshort();
  2722. int versionToExtract = ReadLEUshort();
  2723. int bitFlags = ReadLEUshort();
  2724. int method = ReadLEUshort();
  2725. uint dostime = ReadLEUint();
  2726. uint crc = ReadLEUint();
  2727. long csize = (long)ReadLEUint();
  2728. long size = (long)ReadLEUint();
  2729. int nameLen = ReadLEUshort();
  2730. int extraLen = ReadLEUshort();
  2731. int commentLen = ReadLEUshort();
  2732. int diskStartNo = ReadLEUshort(); // Not currently used
  2733. int internalAttributes = ReadLEUshort(); // Not currently used
  2734. uint externalAttributes = ReadLEUint();
  2735. long offset = ReadLEUint();
  2736. byte[] buffer = new byte[Math.Max(nameLen, commentLen)];
  2737. StreamUtils.ReadFully(baseStream_, buffer, 0, nameLen);
  2738. string name = ZipConstants.ConvertToStringExt(bitFlags, buffer, nameLen);
  2739. ZipEntry entry = new ZipEntry(name, versionToExtract, versionMadeBy, (CompressionMethod)method);
  2740. entry.Crc = crc & 0xffffffffL;
  2741. entry.Size = size & 0xffffffffL;
  2742. entry.CompressedSize = csize & 0xffffffffL;
  2743. entry.Flags = bitFlags;
  2744. entry.DosTime = (uint)dostime;
  2745. entry.ZipFileIndex = (long)i;
  2746. entry.Offset = offset;
  2747. entry.ExternalFileAttributes = (int)externalAttributes;
  2748. if ((bitFlags & 8) == 0) {
  2749. entry.CryptoCheckValue = (byte)(crc >> 24);
  2750. }
  2751. else {
  2752. entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
  2753. }
  2754. if (extraLen > 0) {
  2755. byte[] extra = new byte[extraLen];
  2756. StreamUtils.ReadFully(baseStream_, extra);
  2757. entry.ExtraData = extra;
  2758. }
  2759. entry.ProcessExtraData(false);
  2760. if (commentLen > 0) {
  2761. StreamUtils.ReadFully(baseStream_, buffer, 0, commentLen);
  2762. entry.Comment = ZipConstants.ConvertToStringExt(bitFlags, buffer, commentLen);
  2763. }
  2764. entries_[i] = entry;
  2765. }
  2766. }
  2767. /// <summary>
  2768. /// Locate the data for a given entry.
  2769. /// </summary>
  2770. /// <returns>
  2771. /// The start offset of the data.
  2772. /// </returns>
  2773. /// <exception cref="System.IO.EndOfStreamException">
  2774. /// The stream ends prematurely
  2775. /// </exception>
  2776. /// <exception cref="CommonMPQ.SharpZipLib.Zip.ZipException">
  2777. /// The local header signature is invalid, the entry and central header file name lengths are different
  2778. /// or the local and entry compression methods dont match
  2779. /// </exception>
  2780. long LocateEntry(ZipEntry entry)
  2781. {
  2782. return TestLocalHeader(entry, HeaderTest.Extract);
  2783. }
  2784. #if !NETCF_1_0
  2785. Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
  2786. {
  2787. CryptoStream result = null;
  2788. if ( (entry.Version < ZipConstants.VersionStrongEncryption)
  2789. || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
  2790. PkzipClassicManaged classicManaged = new PkzipClassicManaged();
  2791. OnKeysRequired(entry.Name);
  2792. if (HaveKeys == false) {
  2793. throw new ZipException("No password available for encrypted stream");
  2794. }
  2795. result = new CryptoStream(baseStream, classicManaged.CreateDecryptor(key, null), CryptoStreamMode.Read);
  2796. CheckClassicPassword(result, entry);
  2797. }
  2798. else {
  2799. #if !NET_1_1 && !NETCF_2_0
  2800. if (entry.Version == ZipConstants.VERSION_AES) {
  2801. //
  2802. OnKeysRequired(entry.Name);
  2803. if (HaveKeys == false) {
  2804. throw new ZipException("No password available for AES encrypted stream");
  2805. }
  2806. int saltLen = entry.AESSaltLen;
  2807. byte[] saltBytes = new byte[saltLen];
  2808. int saltIn = baseStream.Read(saltBytes, 0, saltLen);
  2809. if (saltIn != saltLen)
  2810. throw new ZipException("AES Salt expected " + saltLen + " got " + saltIn);
  2811. //
  2812. byte[] pwdVerifyRead = new byte[2];
  2813. baseStream.Read(pwdVerifyRead, 0, 2);
  2814. int blockSize = entry.AESKeySize / 8; // bits to bytes
  2815. ZipAESTransform decryptor = new ZipAESTransform(rawPassword_, saltBytes, blockSize, false);
  2816. byte[] pwdVerifyCalc = decryptor.PwdVerifier;
  2817. if (pwdVerifyCalc[0] != pwdVerifyRead[0] || pwdVerifyCalc[1] != pwdVerifyRead[1])
  2818. throw new Exception("Invalid password for AES");
  2819. result = new ZipAESStream(baseStream, decryptor, CryptoStreamMode.Read);
  2820. }
  2821. else
  2822. #endif
  2823. {
  2824. throw new ZipException("Decryption method not supported");
  2825. }
  2826. }
  2827. return result;
  2828. }
  2829. Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
  2830. {
  2831. CryptoStream result = null;
  2832. if ( (entry.Version < ZipConstants.VersionStrongEncryption)
  2833. || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
  2834. PkzipClassicManaged classicManaged = new PkzipClassicManaged();
  2835. OnKeysRequired(entry.Name);
  2836. if (HaveKeys == false) {
  2837. throw new ZipException("No password available for encrypted stream");
  2838. }
  2839. // Closing a CryptoStream will close the base stream as well so wrap it in an UncompressedStream
  2840. // which doesnt do this.
  2841. result = new CryptoStream(new UncompressedStream(baseStream),
  2842. classicManaged.CreateEncryptor(key, null), CryptoStreamMode.Write);
  2843. if ( (entry.Crc < 0) || (entry.Flags & 8) != 0) {
  2844. WriteEncryptionHeader(result, entry.DosTime << 16);
  2845. }
  2846. else {
  2847. WriteEncryptionHeader(result, entry.Crc);
  2848. }
  2849. }
  2850. return result;
  2851. }
  2852. static void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEntry entry)
  2853. {
  2854. byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
  2855. StreamUtils.ReadFully(classicCryptoStream, cryptbuffer);
  2856. if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) {
  2857. throw new ZipException("Invalid password");
  2858. }
  2859. }
  2860. #endif
  2861. static void WriteEncryptionHeader(Stream stream, long crcValue)
  2862. {
  2863. byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize];
  2864. Random rnd = new Random();
  2865. rnd.NextBytes(cryptBuffer);
  2866. cryptBuffer[11] = (byte)(crcValue >> 24);
  2867. stream.Write(cryptBuffer, 0, cryptBuffer.Length);
  2868. }
  2869. #endregion
  2870. #region Instance Fields
  2871. bool isDisposed_;
  2872. string name_;
  2873. string comment_;
  2874. string rawPassword_;
  2875. Stream baseStream_;
  2876. bool isStreamOwner;
  2877. long offsetOfFirstEntry;
  2878. ZipEntry[] entries_;
  2879. byte[] key;
  2880. bool isNewArchive_;
  2881. // Default is dynamic which is not backwards compatible and can cause problems
  2882. // with XP's built in compression which cant read Zip64 archives.
  2883. // However it does avoid the situation were a large file is added and cannot be completed correctly.
  2884. // Hint: Set always ZipEntry size before they are added to an archive and this setting isnt needed.
  2885. UseZip64 useZip64_ = UseZip64.Dynamic ;
  2886. #region Zip Update Instance Fields
  2887. ArrayList updates_;
  2888. long updateCount_; // Count is managed manually as updates_ can contain nulls!
  2889. Hashtable updateIndex_;
  2890. IArchiveStorage archiveStorage_;
  2891. IDynamicDataSource updateDataSource_;
  2892. bool contentsEdited_;
  2893. int bufferSize_ = DefaultBufferSize;
  2894. byte[] copyBuffer_;
  2895. ZipString newComment_;
  2896. bool commentEdited_;
  2897. IEntryFactory updateEntryFactory_ = new ZipEntryFactory();
  2898. #endregion
  2899. #endregion
  2900. #region Support Classes
  2901. /// <summary>
  2902. /// Represents a string from a <see cref="ZipFile"/> which is stored as an array of bytes.
  2903. /// </summary>
  2904. class ZipString
  2905. {
  2906. #region Constructors
  2907. /// <summary>
  2908. /// Initialise a <see cref="ZipString"/> with a string.
  2909. /// </summary>
  2910. /// <param name="comment">The textual string form.</param>
  2911. public ZipString(string comment)
  2912. {
  2913. comment_ = comment;
  2914. isSourceString_ = true;
  2915. }
  2916. /// <summary>
  2917. /// Initialise a <see cref="ZipString"/> using a string in its binary 'raw' form.
  2918. /// </summary>
  2919. /// <param name="rawString"></param>
  2920. public ZipString(byte[] rawString)
  2921. {
  2922. rawComment_ = rawString;
  2923. }
  2924. #endregion
  2925. /// <summary>
  2926. /// Get a value indicating the original source of data for this instance.
  2927. /// True if the source was a string; false if the source was binary data.
  2928. /// </summary>
  2929. public bool IsSourceString
  2930. {
  2931. get { return isSourceString_; }
  2932. }
  2933. /// <summary>
  2934. /// Get the length of the comment when represented as raw bytes.
  2935. /// </summary>
  2936. public int RawLength
  2937. {
  2938. get {
  2939. MakeBytesAvailable();
  2940. return rawComment_.Length;
  2941. }
  2942. }
  2943. /// <summary>
  2944. /// Get the comment in its 'raw' form as plain bytes.
  2945. /// </summary>
  2946. public byte[] RawComment
  2947. {
  2948. get {
  2949. MakeBytesAvailable();
  2950. return (byte[])rawComment_.Clone();
  2951. }
  2952. }
  2953. /// <summary>
  2954. /// Reset the comment to its initial state.
  2955. /// </summary>
  2956. public void Reset()
  2957. {
  2958. if ( isSourceString_ ) {
  2959. rawComment_ = null;
  2960. }
  2961. else {
  2962. comment_ = null;
  2963. }
  2964. }
  2965. void MakeTextAvailable()
  2966. {
  2967. if ( comment_ == null ) {
  2968. comment_ = ZipConstants.ConvertToString(rawComment_);
  2969. }
  2970. }
  2971. void MakeBytesAvailable()
  2972. {
  2973. if ( rawComment_ == null ) {
  2974. rawComment_ = ZipConstants.ConvertToArray(comment_);
  2975. }
  2976. }
  2977. /// <summary>
  2978. /// Implicit conversion of comment to a string.
  2979. /// </summary>
  2980. /// <param name="zipString">The <see cref="ZipString"/> to convert to a string.</param>
  2981. /// <returns>The textual equivalent for the input value.</returns>
  2982. static public implicit operator string(ZipString zipString)
  2983. {
  2984. zipString.MakeTextAvailable();
  2985. return zipString.comment_;
  2986. }
  2987. #region Instance Fields
  2988. string comment_;
  2989. byte[] rawComment_;
  2990. bool isSourceString_;
  2991. #endregion
  2992. }
  2993. /// <summary>
  2994. /// An <see cref="IEnumerator">enumerator</see> for <see cref="ZipEntry">Zip entries</see>
  2995. /// </summary>
  2996. class ZipEntryEnumerator : IEnumerator
  2997. {
  2998. #region Constructors
  2999. public ZipEntryEnumerator(ZipEntry[] entries)
  3000. {
  3001. array = entries;
  3002. }
  3003. #endregion
  3004. #region IEnumerator Members
  3005. public object Current
  3006. {
  3007. get {
  3008. return array[index];
  3009. }
  3010. }
  3011. public void Reset()
  3012. {
  3013. index = -1;
  3014. }
  3015. public bool MoveNext()
  3016. {
  3017. return (++index < array.Length);
  3018. }
  3019. #endregion
  3020. #region Instance Fields
  3021. ZipEntry[] array;
  3022. int index = -1;
  3023. #endregion
  3024. }
  3025. /// <summary>
  3026. /// An <see cref="UncompressedStream"/> is a stream that you can write uncompressed data
  3027. /// to and flush, but cannot read, seek or do anything else to.
  3028. /// </summary>
  3029. class UncompressedStream : Stream
  3030. {
  3031. #region Constructors
  3032. public UncompressedStream(Stream baseStream)
  3033. {
  3034. baseStream_ = baseStream;
  3035. }
  3036. #endregion
  3037. /// <summary>
  3038. /// Close this stream instance.
  3039. /// </summary>
  3040. public override void Close()
  3041. {
  3042. // Do nothing
  3043. }
  3044. /// <summary>
  3045. /// Gets a value indicating whether the current stream supports reading.
  3046. /// </summary>
  3047. public override bool CanRead
  3048. {
  3049. get {
  3050. return false;
  3051. }
  3052. }
  3053. /// <summary>
  3054. /// Write any buffered data to underlying storage.
  3055. /// </summary>
  3056. public override void Flush()
  3057. {
  3058. baseStream_.Flush();
  3059. }
  3060. /// <summary>
  3061. /// Gets a value indicating whether the current stream supports writing.
  3062. /// </summary>
  3063. public override bool CanWrite
  3064. {
  3065. get {
  3066. return baseStream_.CanWrite;
  3067. }
  3068. }
  3069. /// <summary>
  3070. /// Gets a value indicating whether the current stream supports seeking.
  3071. /// </summary>
  3072. public override bool CanSeek
  3073. {
  3074. get {
  3075. return false;
  3076. }
  3077. }
  3078. /// <summary>
  3079. /// Get the length in bytes of the stream.
  3080. /// </summary>
  3081. public override long Length
  3082. {
  3083. get {
  3084. return 0;
  3085. }
  3086. }
  3087. /// <summary>
  3088. /// Gets or sets the position within the current stream.
  3089. /// </summary>
  3090. public override long Position
  3091. {
  3092. get {
  3093. return baseStream_.Position;
  3094. }
  3095. set
  3096. {
  3097. }
  3098. }
  3099. /// <summary>
  3100. /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
  3101. /// </summary>
  3102. /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
  3103. /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
  3104. /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
  3105. /// <returns>
  3106. /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
  3107. /// </returns>
  3108. /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
  3109. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3110. /// <exception cref="T:System.NotSupportedException">The stream does not support reading. </exception>
  3111. /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
  3112. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3113. /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
  3114. public override int Read(byte[] buffer, int offset, int count)
  3115. {
  3116. return 0;
  3117. }
  3118. /// <summary>
  3119. /// Sets the position within the current stream.
  3120. /// </summary>
  3121. /// <param name="offset">A byte offset relative to the origin parameter.</param>
  3122. /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param>
  3123. /// <returns>
  3124. /// The new position within the current stream.
  3125. /// </returns>
  3126. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3127. /// <exception cref="T:System.NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
  3128. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3129. public override long Seek(long offset, SeekOrigin origin)
  3130. {
  3131. return 0;
  3132. }
  3133. /// <summary>
  3134. /// Sets the length of the current stream.
  3135. /// </summary>
  3136. /// <param name="value">The desired length of the current stream in bytes.</param>
  3137. /// <exception cref="T:System.NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
  3138. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3139. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3140. public override void SetLength(long value)
  3141. {
  3142. }
  3143. /// <summary>
  3144. /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
  3145. /// </summary>
  3146. /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
  3147. /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
  3148. /// <param name="count">The number of bytes to be written to the current stream.</param>
  3149. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3150. /// <exception cref="T:System.NotSupportedException">The stream does not support writing. </exception>
  3151. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3152. /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
  3153. /// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
  3154. /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
  3155. public override void Write(byte[] buffer, int offset, int count)
  3156. {
  3157. baseStream_.Write(buffer, offset, count);
  3158. }
  3159. #region Instance Fields
  3160. Stream baseStream_;
  3161. #endregion
  3162. }
  3163. /// <summary>
  3164. /// A <see cref="PartialInputStream"/> is an <see cref="InflaterInputStream"/>
  3165. /// whose data is only a part or subsection of a file.
  3166. /// </summary>
  3167. class PartialInputStream : Stream
  3168. {
  3169. #region Constructors
  3170. /// <summary>
  3171. /// Initialise a new instance of the <see cref="PartialInputStream"/> class.
  3172. /// </summary>
  3173. /// <param name="zipFile">The <see cref="ZipFile"/> containing the underlying stream to use for IO.</param>
  3174. /// <param name="start">The start of the partial data.</param>
  3175. /// <param name="length">The length of the partial data.</param>
  3176. public PartialInputStream(ZipFile zipFile, long start, long length)
  3177. {
  3178. start_ = start;
  3179. length_ = length;
  3180. // Although this is the only time the zipfile is used
  3181. // keeping a reference here prevents premature closure of
  3182. // this zip file and thus the baseStream_.
  3183. // Code like this will cause apparently random failures depending
  3184. // on the size of the files and when garbage is collected.
  3185. //
  3186. // ZipFile z = new ZipFile (stream);
  3187. // Stream reader = z.GetInputStream(0);
  3188. // uses reader here....
  3189. zipFile_ = zipFile;
  3190. baseStream_ = zipFile_.baseStream_;
  3191. readPos_ = start;
  3192. end_ = start + length;
  3193. }
  3194. #endregion
  3195. /// <summary>
  3196. /// Read a byte from this stream.
  3197. /// </summary>
  3198. /// <returns>Returns the byte read or -1 on end of stream.</returns>
  3199. public override int ReadByte()
  3200. {
  3201. if (readPos_ >= end_) {
  3202. // -1 is the correct value at end of stream.
  3203. return -1;
  3204. }
  3205. lock( baseStream_ ) {
  3206. baseStream_.Seek(readPos_++, SeekOrigin.Begin);
  3207. return baseStream_.ReadByte();
  3208. }
  3209. }
  3210. /// <summary>
  3211. /// Close this <see cref="PartialInputStream">partial input stream</see>.
  3212. /// </summary>
  3213. /// <remarks>
  3214. /// The underlying stream is not closed. Close the parent ZipFile class to do that.
  3215. /// </remarks>
  3216. public override void Close()
  3217. {
  3218. // Do nothing at all!
  3219. }
  3220. /// <summary>
  3221. /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
  3222. /// </summary>
  3223. /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
  3224. /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
  3225. /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
  3226. /// <returns>
  3227. /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
  3228. /// </returns>
  3229. /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
  3230. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3231. /// <exception cref="T:System.NotSupportedException">The stream does not support reading. </exception>
  3232. /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
  3233. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3234. /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
  3235. public override int Read(byte[] buffer, int offset, int count)
  3236. {
  3237. lock(baseStream_) {
  3238. if (count > end_ - readPos_) {
  3239. count = (int) (end_ - readPos_);
  3240. if (count == 0) {
  3241. return 0;
  3242. }
  3243. }
  3244. baseStream_.Seek(readPos_, SeekOrigin.Begin);
  3245. int readCount = baseStream_.Read(buffer, offset, count);
  3246. if (readCount > 0) {
  3247. readPos_ += readCount;
  3248. }
  3249. return readCount;
  3250. }
  3251. }
  3252. /// <summary>
  3253. /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
  3254. /// </summary>
  3255. /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
  3256. /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
  3257. /// <param name="count">The number of bytes to be written to the current stream.</param>
  3258. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3259. /// <exception cref="T:System.NotSupportedException">The stream does not support writing. </exception>
  3260. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3261. /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
  3262. /// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
  3263. /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
  3264. public override void Write(byte[] buffer, int offset, int count)
  3265. {
  3266. throw new NotSupportedException();
  3267. }
  3268. /// <summary>
  3269. /// When overridden in a derived class, sets the length of the current stream.
  3270. /// </summary>
  3271. /// <param name="value">The desired length of the current stream in bytes.</param>
  3272. /// <exception cref="T:System.NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
  3273. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3274. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3275. public override void SetLength(long value)
  3276. {
  3277. throw new NotSupportedException();
  3278. }
  3279. /// <summary>
  3280. /// When overridden in a derived class, sets the position within the current stream.
  3281. /// </summary>
  3282. /// <param name="offset">A byte offset relative to the origin parameter.</param>
  3283. /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param>
  3284. /// <returns>
  3285. /// The new position within the current stream.
  3286. /// </returns>
  3287. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3288. /// <exception cref="T:System.NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
  3289. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3290. public override long Seek(long offset, SeekOrigin origin)
  3291. {
  3292. long newPos = readPos_;
  3293. switch ( origin )
  3294. {
  3295. case SeekOrigin.Begin:
  3296. newPos = start_ + offset;
  3297. break;
  3298. case SeekOrigin.Current:
  3299. newPos = readPos_ + offset;
  3300. break;
  3301. case SeekOrigin.End:
  3302. newPos = end_ + offset;
  3303. break;
  3304. }
  3305. if ( newPos < start_ ) {
  3306. throw new ArgumentException("Negative position is invalid");
  3307. }
  3308. if ( newPos >= end_ ) {
  3309. throw new IOException("Cannot seek past end");
  3310. }
  3311. readPos_ = newPos;
  3312. return readPos_;
  3313. }
  3314. /// <summary>
  3315. /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
  3316. /// </summary>
  3317. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3318. public override void Flush()
  3319. {
  3320. // Nothing to do.
  3321. }
  3322. /// <summary>
  3323. /// Gets or sets the position within the current stream.
  3324. /// </summary>
  3325. /// <value></value>
  3326. /// <returns>The current position within the stream.</returns>
  3327. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3328. /// <exception cref="T:System.NotSupportedException">The stream does not support seeking. </exception>
  3329. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3330. public override long Position {
  3331. get { return readPos_ - start_; }
  3332. set {
  3333. long newPos = start_ + value;
  3334. if ( newPos < start_ ) {
  3335. throw new ArgumentException("Negative position is invalid");
  3336. }
  3337. if ( newPos >= end_ ) {
  3338. throw new InvalidOperationException("Cannot seek past end");
  3339. }
  3340. readPos_ = newPos;
  3341. }
  3342. }
  3343. /// <summary>
  3344. /// Gets the length in bytes of the stream.
  3345. /// </summary>
  3346. /// <value></value>
  3347. /// <returns>A long value representing the length of the stream in bytes.</returns>
  3348. /// <exception cref="T:System.NotSupportedException">A class derived from Stream does not support seeking. </exception>
  3349. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3350. public override long Length {
  3351. get { return length_; }
  3352. }
  3353. /// <summary>
  3354. /// Gets a value indicating whether the current stream supports writing.
  3355. /// </summary>
  3356. /// <value>false</value>
  3357. /// <returns>true if the stream supports writing; otherwise, false.</returns>
  3358. public override bool CanWrite {
  3359. get { return false; }
  3360. }
  3361. /// <summary>
  3362. /// Gets a value indicating whether the current stream supports seeking.
  3363. /// </summary>
  3364. /// <value>true</value>
  3365. /// <returns>true if the stream supports seeking; otherwise, false.</returns>
  3366. public override bool CanSeek {
  3367. get { return true; }
  3368. }
  3369. /// <summary>
  3370. /// Gets a value indicating whether the current stream supports reading.
  3371. /// </summary>
  3372. /// <value>true.</value>
  3373. /// <returns>true if the stream supports reading; otherwise, false.</returns>
  3374. public override bool CanRead {
  3375. get { return true; }
  3376. }
  3377. #if !NET_1_0 && !NET_1_1 && !NETCF_1_0
  3378. /// <summary>
  3379. /// Gets a value that determines whether the current stream can time out.
  3380. /// </summary>
  3381. /// <value></value>
  3382. /// <returns>A value that determines whether the current stream can time out.</returns>
  3383. public override bool CanTimeout {
  3384. get { return baseStream_.CanTimeout; }
  3385. }
  3386. #endif
  3387. #region Instance Fields
  3388. ZipFile zipFile_;
  3389. Stream baseStream_;
  3390. long start_;
  3391. long length_;
  3392. long readPos_;
  3393. long end_;
  3394. #endregion
  3395. }
  3396. #endregion
  3397. }
  3398. #endregion
  3399. #region DataSources
  3400. /// <summary>
  3401. /// Provides a static way to obtain a source of data for an entry.
  3402. /// </summary>
  3403. public interface IStaticDataSource
  3404. {
  3405. /// <summary>
  3406. /// Get a source of data by creating a new stream.
  3407. /// </summary>
  3408. /// <returns>Returns a <see cref="Stream"/> to use for compression input.</returns>
  3409. /// <remarks>Ideally a new stream is created and opened to achieve this, to avoid locking problems.</remarks>
  3410. Stream GetSource();
  3411. }
  3412. /// <summary>
  3413. /// Represents a source of data that can dynamically provide
  3414. /// multiple <see cref="Stream">data sources</see> based on the parameters passed.
  3415. /// </summary>
  3416. public interface IDynamicDataSource
  3417. {
  3418. /// <summary>
  3419. /// Get a data source.
  3420. /// </summary>
  3421. /// <param name="entry">The <see cref="ZipEntry"/> to get a source for.</param>
  3422. /// <param name="name">The name for data if known.</param>
  3423. /// <returns>Returns a <see cref="Stream"/> to use for compression input.</returns>
  3424. /// <remarks>Ideally a new stream is created and opened to achieve this, to avoid locking problems.</remarks>
  3425. Stream GetSource(ZipEntry entry, string name);
  3426. }
  3427. /// <summary>
  3428. /// Default implementation of a <see cref="IStaticDataSource"/> for use with files stored on disk.
  3429. /// </summary>
  3430. public class StaticDiskDataSource : IStaticDataSource
  3431. {
  3432. /// <summary>
  3433. /// Initialise a new instnace of <see cref="StaticDiskDataSource"/>
  3434. /// </summary>
  3435. /// <param name="fileName">The name of the file to obtain data from.</param>
  3436. public StaticDiskDataSource(string fileName)
  3437. {
  3438. fileName_ = fileName;
  3439. }
  3440. #region IDataSource Members
  3441. /// <summary>
  3442. /// Get a <see cref="Stream"/> providing data.
  3443. /// </summary>
  3444. /// <returns>Returns a <see cref="Stream"/> provising data.</returns>
  3445. public Stream GetSource()
  3446. {
  3447. return File.Open(fileName_, FileMode.Open, FileAccess.Read, FileShare.Read);
  3448. }
  3449. #endregion
  3450. #region Instance Fields
  3451. string fileName_;
  3452. #endregion
  3453. }
  3454. /// <summary>
  3455. /// Default implementation of <see cref="IDynamicDataSource"/> for files stored on disk.
  3456. /// </summary>
  3457. public class DynamicDiskDataSource : IDynamicDataSource
  3458. {
  3459. /// <summary>
  3460. /// Initialise a default instance of <see cref="DynamicDiskDataSource"/>.
  3461. /// </summary>
  3462. public DynamicDiskDataSource()
  3463. {
  3464. }
  3465. #region IDataSource Members
  3466. /// <summary>
  3467. /// Get a <see cref="Stream"/> providing data for an entry.
  3468. /// </summary>
  3469. /// <param name="entry">The entry to provide data for.</param>
  3470. /// <param name="name">The file name for data if known.</param>
  3471. /// <returns>Returns a stream providing data; or null if not available</returns>
  3472. public Stream GetSource(ZipEntry entry, string name)
  3473. {
  3474. Stream result = null;
  3475. if ( name != null ) {
  3476. result = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
  3477. }
  3478. return result;
  3479. }
  3480. #endregion
  3481. }
  3482. #endregion
  3483. #region Archive Storage
  3484. /// <summary>
  3485. /// Defines facilities for data storage when updating Zip Archives.
  3486. /// </summary>
  3487. public interface IArchiveStorage
  3488. {
  3489. /// <summary>
  3490. /// Get the <see cref="FileUpdateMode"/> to apply during updates.
  3491. /// </summary>
  3492. FileUpdateMode UpdateMode { get; }
  3493. /// <summary>
  3494. /// Get an empty <see cref="Stream"/> that can be used for temporary output.
  3495. /// </summary>
  3496. /// <returns>Returns a temporary output <see cref="Stream"/></returns>
  3497. /// <seealso cref="ConvertTemporaryToFinal"></seealso>
  3498. Stream GetTemporaryOutput();
  3499. /// <summary>
  3500. /// Convert a temporary output stream to a final stream.
  3501. /// </summary>
  3502. /// <returns>The resulting final <see cref="Stream"/></returns>
  3503. /// <seealso cref="GetTemporaryOutput"/>
  3504. Stream ConvertTemporaryToFinal();
  3505. /// <summary>
  3506. /// Make a temporary copy of the original stream.
  3507. /// </summary>
  3508. /// <param name="stream">The <see cref="Stream"/> to copy.</param>
  3509. /// <returns>Returns a temporary output <see cref="Stream"/> that is a copy of the input.</returns>
  3510. Stream MakeTemporaryCopy(Stream stream);
  3511. /// <summary>
  3512. /// Return a stream suitable for performing direct updates on the original source.
  3513. /// </summary>
  3514. /// <param name="stream">The current stream.</param>
  3515. /// <returns>Returns a stream suitable for direct updating.</returns>
  3516. /// <remarks>This may be the current stream passed.</remarks>
  3517. Stream OpenForDirectUpdate(Stream stream);
  3518. /// <summary>
  3519. /// Dispose of this instance.
  3520. /// </summary>
  3521. void Dispose();
  3522. }
  3523. /// <summary>
  3524. /// An abstract <see cref="IArchiveStorage"/> suitable for extension by inheritance.
  3525. /// </summary>
  3526. abstract public class BaseArchiveStorage : IArchiveStorage
  3527. {
  3528. #region Constructors
  3529. /// <summary>
  3530. /// Initializes a new instance of the <see cref="BaseArchiveStorage"/> class.
  3531. /// </summary>
  3532. /// <param name="updateMode">The update mode.</param>
  3533. protected BaseArchiveStorage(FileUpdateMode updateMode)
  3534. {
  3535. updateMode_ = updateMode;
  3536. }
  3537. #endregion
  3538. #region IArchiveStorage Members
  3539. /// <summary>
  3540. /// Gets a temporary output <see cref="Stream"/>
  3541. /// </summary>
  3542. /// <returns>Returns the temporary output stream.</returns>
  3543. /// <seealso cref="ConvertTemporaryToFinal"></seealso>
  3544. public abstract Stream GetTemporaryOutput();
  3545. /// <summary>
  3546. /// Converts the temporary <see cref="Stream"/> to its final form.
  3547. /// </summary>
  3548. /// <returns>Returns a <see cref="Stream"/> that can be used to read
  3549. /// the final storage for the archive.</returns>
  3550. /// <seealso cref="GetTemporaryOutput"/>
  3551. public abstract Stream ConvertTemporaryToFinal();
  3552. /// <summary>
  3553. /// Make a temporary copy of a <see cref="Stream"/>.
  3554. /// </summary>
  3555. /// <param name="stream">The <see cref="Stream"/> to make a copy of.</param>
  3556. /// <returns>Returns a temporary output <see cref="Stream"/> that is a copy of the input.</returns>
  3557. public abstract Stream MakeTemporaryCopy(Stream stream);
  3558. /// <summary>
  3559. /// Return a stream suitable for performing direct updates on the original source.
  3560. /// </summary>
  3561. /// <param name="stream">The <see cref="Stream"/> to open for direct update.</param>
  3562. /// <returns>Returns a stream suitable for direct updating.</returns>
  3563. public abstract Stream OpenForDirectUpdate(Stream stream);
  3564. /// <summary>
  3565. /// Disposes this instance.
  3566. /// </summary>
  3567. public abstract void Dispose();
  3568. /// <summary>
  3569. /// Gets the update mode applicable.
  3570. /// </summary>
  3571. /// <value>The update mode.</value>
  3572. public FileUpdateMode UpdateMode
  3573. {
  3574. get {
  3575. return updateMode_;
  3576. }
  3577. }
  3578. #endregion
  3579. #region Instance Fields
  3580. FileUpdateMode updateMode_;
  3581. #endregion
  3582. }
  3583. /// <summary>
  3584. /// An <see cref="IArchiveStorage"/> implementation suitable for hard disks.
  3585. /// </summary>
  3586. public class DiskArchiveStorage : BaseArchiveStorage
  3587. {
  3588. #region Constructors
  3589. /// <summary>
  3590. /// Initializes a new instance of the <see cref="DiskArchiveStorage"/> class.
  3591. /// </summary>
  3592. /// <param name="file">The file.</param>
  3593. /// <param name="updateMode">The update mode.</param>
  3594. public DiskArchiveStorage(ZipFile file, FileUpdateMode updateMode)
  3595. : base(updateMode)
  3596. {
  3597. if ( file.Name == null ) {
  3598. throw new ZipException("Cant handle non file archives");
  3599. }
  3600. fileName_ = file.Name;
  3601. }
  3602. /// <summary>
  3603. /// Initializes a new instance of the <see cref="DiskArchiveStorage"/> class.
  3604. /// </summary>
  3605. /// <param name="file">The file.</param>
  3606. public DiskArchiveStorage(ZipFile file)
  3607. : this(file, FileUpdateMode.Safe)
  3608. {
  3609. }
  3610. #endregion
  3611. #region IArchiveStorage Members
  3612. /// <summary>
  3613. /// Gets a temporary output <see cref="Stream"/> for performing updates on.
  3614. /// </summary>
  3615. /// <returns>Returns the temporary output stream.</returns>
  3616. public override Stream GetTemporaryOutput()
  3617. {
  3618. if ( temporaryName_ != null ) {
  3619. temporaryName_ = GetTempFileName(temporaryName_, true);
  3620. temporaryStream_ = File.Open(temporaryName_, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  3621. }
  3622. else {
  3623. // Determine where to place files based on internal strategy.
  3624. // Currently this is always done in system temp directory.
  3625. temporaryName_ = Path.GetTempFileName();
  3626. temporaryStream_ = File.Open(temporaryName_, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  3627. }
  3628. return temporaryStream_;
  3629. }
  3630. /// <summary>
  3631. /// Converts a temporary <see cref="Stream"/> to its final form.
  3632. /// </summary>
  3633. /// <returns>Returns a <see cref="Stream"/> that can be used to read
  3634. /// the final storage for the archive.</returns>
  3635. public override Stream ConvertTemporaryToFinal()
  3636. {
  3637. if ( temporaryStream_ == null ) {
  3638. throw new ZipException("No temporary stream has been created");
  3639. }
  3640. Stream result = null;
  3641. string moveTempName = GetTempFileName(fileName_, false);
  3642. bool newFileCreated = false;
  3643. try {
  3644. temporaryStream_.Close();
  3645. File.Move(fileName_, moveTempName);
  3646. File.Move(temporaryName_, fileName_);
  3647. newFileCreated = true;
  3648. File.Delete(moveTempName);
  3649. result = File.Open(fileName_, FileMode.Open, FileAccess.Read, FileShare.Read);
  3650. }
  3651. catch(Exception) {
  3652. result = null;
  3653. // Try to roll back changes...
  3654. if ( !newFileCreated ) {
  3655. File.Move(moveTempName, fileName_);
  3656. File.Delete(temporaryName_);
  3657. }
  3658. throw;
  3659. }
  3660. return result;
  3661. }
  3662. /// <summary>
  3663. /// Make a temporary copy of a stream.
  3664. /// </summary>
  3665. /// <param name="stream">The <see cref="Stream"/> to copy.</param>
  3666. /// <returns>Returns a temporary output <see cref="Stream"/> that is a copy of the input.</returns>
  3667. public override Stream MakeTemporaryCopy(Stream stream)
  3668. {
  3669. stream.Close();
  3670. temporaryName_ = GetTempFileName(fileName_, true);
  3671. File.Copy(fileName_, temporaryName_, true);
  3672. temporaryStream_ = new FileStream(temporaryName_,
  3673. FileMode.Open,
  3674. FileAccess.ReadWrite);
  3675. return temporaryStream_;
  3676. }
  3677. /// <summary>
  3678. /// Return a stream suitable for performing direct updates on the original source.
  3679. /// </summary>
  3680. /// <param name="stream">The current stream.</param>
  3681. /// <returns>Returns a stream suitable for direct updating.</returns>
  3682. /// <remarks>If the <paramref name="stream"/> is not null this is used as is.</remarks>
  3683. public override Stream OpenForDirectUpdate(Stream stream)
  3684. {
  3685. Stream result;
  3686. if ((stream == null) || !stream.CanWrite)
  3687. {
  3688. if (stream != null) {
  3689. stream.Close();
  3690. }
  3691. result = new FileStream(fileName_,
  3692. FileMode.Open,
  3693. FileAccess.ReadWrite);
  3694. }
  3695. else
  3696. {
  3697. result = stream;
  3698. }
  3699. return result;
  3700. }
  3701. /// <summary>
  3702. /// Disposes this instance.
  3703. /// </summary>
  3704. public override void Dispose()
  3705. {
  3706. if ( temporaryStream_ != null ) {
  3707. temporaryStream_.Close();
  3708. }
  3709. }
  3710. #endregion
  3711. #region Internal routines
  3712. static string GetTempFileName(string original, bool makeTempFile)
  3713. {
  3714. string result = null;
  3715. if ( original == null ) {
  3716. result = Path.GetTempFileName();
  3717. }
  3718. else {
  3719. int counter = 0;
  3720. int suffixSeed = DateTime.Now.Second;
  3721. while ( result == null ) {
  3722. counter += 1;
  3723. string newName = string.Format("{0}.{1}{2}.tmp", original, suffixSeed, counter);
  3724. if ( !File.Exists(newName) ) {
  3725. if ( makeTempFile) {
  3726. try {
  3727. // Try and create the file.
  3728. using ( FileStream stream = File.Create(newName) ) {
  3729. }
  3730. result = newName;
  3731. }
  3732. catch {
  3733. suffixSeed = DateTime.Now.Second;
  3734. }
  3735. }
  3736. else {
  3737. result = newName;
  3738. }
  3739. }
  3740. }
  3741. }
  3742. return result;
  3743. }
  3744. #endregion
  3745. #region Instance Fields
  3746. Stream temporaryStream_;
  3747. string fileName_;
  3748. string temporaryName_;
  3749. #endregion
  3750. }
  3751. /// <summary>
  3752. /// An <see cref="IArchiveStorage"/> implementation suitable for in memory streams.
  3753. /// </summary>
  3754. public class MemoryArchiveStorage : BaseArchiveStorage
  3755. {
  3756. #region Constructors
  3757. /// <summary>
  3758. /// Initializes a new instance of the <see cref="MemoryArchiveStorage"/> class.
  3759. /// </summary>
  3760. public MemoryArchiveStorage()
  3761. : base(FileUpdateMode.Direct)
  3762. {
  3763. }
  3764. /// <summary>
  3765. /// Initializes a new instance of the <see cref="MemoryArchiveStorage"/> class.
  3766. /// </summary>
  3767. /// <param name="updateMode">The <see cref="FileUpdateMode"/> to use</param>
  3768. /// <remarks>This constructor is for testing as memory streams dont really require safe mode.</remarks>
  3769. public MemoryArchiveStorage(FileUpdateMode updateMode)
  3770. : base(updateMode)
  3771. {
  3772. }
  3773. #endregion
  3774. #region Properties
  3775. /// <summary>
  3776. /// Get the stream returned by <see cref="ConvertTemporaryToFinal"/> if this was in fact called.
  3777. /// </summary>
  3778. public MemoryStream FinalStream
  3779. {
  3780. get { return finalStream_; }
  3781. }
  3782. #endregion
  3783. #region IArchiveStorage Members
  3784. /// <summary>
  3785. /// Gets the temporary output <see cref="Stream"/>
  3786. /// </summary>
  3787. /// <returns>Returns the temporary output stream.</returns>
  3788. public override Stream GetTemporaryOutput()
  3789. {
  3790. temporaryStream_ = new MemoryStream();
  3791. return temporaryStream_;
  3792. }
  3793. /// <summary>
  3794. /// Converts the temporary <see cref="Stream"/> to its final form.
  3795. /// </summary>
  3796. /// <returns>Returns a <see cref="Stream"/> that can be used to read
  3797. /// the final storage for the archive.</returns>
  3798. public override Stream ConvertTemporaryToFinal()
  3799. {
  3800. if ( temporaryStream_ == null ) {
  3801. throw new ZipException("No temporary stream has been created");
  3802. }
  3803. finalStream_ = new MemoryStream(temporaryStream_.ToArray());
  3804. return finalStream_;
  3805. }
  3806. /// <summary>
  3807. /// Make a temporary copy of the original stream.
  3808. /// </summary>
  3809. /// <param name="stream">The <see cref="Stream"/> to copy.</param>
  3810. /// <returns>Returns a temporary output <see cref="Stream"/> that is a copy of the input.</returns>
  3811. public override Stream MakeTemporaryCopy(Stream stream)
  3812. {
  3813. temporaryStream_ = new MemoryStream();
  3814. stream.Position = 0;
  3815. StreamUtils.Copy(stream, temporaryStream_, new byte[4096]);
  3816. return temporaryStream_;
  3817. }
  3818. /// <summary>
  3819. /// Return a stream suitable for performing direct updates on the original source.
  3820. /// </summary>
  3821. /// <param name="stream">The original source stream</param>
  3822. /// <returns>Returns a stream suitable for direct updating.</returns>
  3823. /// <remarks>If the <paramref name="stream"/> passed is not null this is used;
  3824. /// otherwise a new <see cref="MemoryStream"/> is returned.</remarks>
  3825. public override Stream OpenForDirectUpdate(Stream stream)
  3826. {
  3827. Stream result;
  3828. if ((stream == null) || !stream.CanWrite) {
  3829. result = new MemoryStream();
  3830. if (stream != null) {
  3831. stream.Position = 0;
  3832. StreamUtils.Copy(stream, result, new byte[4096]);
  3833. stream.Close();
  3834. }
  3835. }
  3836. else {
  3837. result = stream;
  3838. }
  3839. return result;
  3840. }
  3841. /// <summary>
  3842. /// Disposes this instance.
  3843. /// </summary>
  3844. public override void Dispose()
  3845. {
  3846. if ( temporaryStream_ != null ) {
  3847. temporaryStream_.Close();
  3848. }
  3849. }
  3850. #endregion
  3851. #region Instance Fields
  3852. MemoryStream temporaryStream_;
  3853. MemoryStream finalStream_;
  3854. #endregion
  3855. }
  3856. #endregion
  3857. }