用java實現zip壓縮和解壓縮功能。

本文使用了jdk1.5.0_13的源代碼。(java.uti.zip*)

實現方法:首先將java.uti.zip中的源代碼解壓出來。找到DeflaterOutputStream、InflaterInputStream、ZipConstants、ZipEntry、ZipInputStream、ZipOutputStream,放到新的包com.work.util.zip中,重新組織這些文件的import,能使用當前包中類的就使用當前包中的類。

重點要修改的類:ZipEntry,修改內容如下:

  1. //修改對應的程序,在這裏加載zip庫
  2. static{
  3. /*loadtheziplibrary*/
  4. java.security.AccessController
  5. .doPrivileged(newsun.security.action.LoadLibraryAction("zip"));
  6. //initIDs();
  7. }
  8. //static{
  9. ///*ZiplibraryisloadedfromSystem.initializeSystemClass*/
  10. //initIDs();
  11. //}
  12. //privatestaticnativevoidinitIDs();

其次修改ZipInputStream,大約在279行。

  1. //ZipEntrye=createZipEntry(getUTF8String(b,0,len));
  2. ZipEntrye=null;
  3. try{
  4. if(this.encoding.toUpperCase().equals("UTF-8"))
  5. e=createZipEntry(getUTF8String(b,0,len));
  6. else
  7. e=createZipEntry(newString(b,0,len,this.encoding));
  8. }catch(ExceptionbyteE){
  9. e=createZipEntry(getUTF8String(b,0,len));
  10. }
  11. //增加構造方法
  12. private String encoding = "UTF-8";
  13. public ZipInputStream(InputStream in, String encoding) {
    super(new PushbackInputStream(in, 512), new Inflater(true), 512);
    usesDefaultInflater = true;
    if (in == null) {
    throw new NullPointerException("in is null");
    }
    this.encoding = encoding;
    }

再次修改ZipOutputStream類,源代碼如下:

  1. /*
  2. *@(#)ZipOutputStream.java1.3103/12/19
  3. *
  4. *Copyright2004SunMicrosystems,Inc.Allrightsreserved.
  5. *SUNPROPRIETARY/CONFIDENTIAL.Useissubjecttolicenseterms.
  6. */
  7. packagecom.work.util.zip;
  8. importjava.io.IOException;
  9. importjava.io.OutputStream;
  10. importjava.util.Enumeration;
  11. importjava.util.Hashtable;
  12. importjava.util.Vector;
  13. importjava.util.zip.CRC32;
  14. importjava.util.zip.Deflater;
  15. importjava.util.zip.ZipException;
  16. /**
  17. *ThisclassimplementsanoutputstreamfilterforwritingfilesintheZIP
  18. *fileformat.Includessupportforbothcompressedanduncompressedentries.
  19. *
  20. *@authorDavidConnelly
  21. *@version1.31,12/19/03
  22. */
  23. publicclassZipOutputStreamextendsDeflaterOutputStreamimplements
  24. ZipConstants{
  25. privateZipEntryentry;
  26. privateVectorentries=newVector();
  27. privateHashtablenames=newHashtable();
  28. privateCRC32crc=newCRC32();
  29. privatelongwritten=0;
  30. privatelonglocoff=0;
  31. privateStringcomment;
  32. privateintmethod=DEFLATED;
  33. privatebooleanfinished;
  34. privatebooleanclosed=false;
  35. /**
  36. *Checktomakesurethatthisstreamhasnotbeenclosed
  37. */
  38. privatevoidensureOpen()throwsIOException{
  39. if(closed){
  40. thrownewIOException("Streamclosed");
  41. }
  42. }
  43. /**
  44. *Compressionmethodforuncompressed(STORED)entries.
  45. */
  46. publicstaticfinalintSTORED=ZipEntry.STORED;
  47. /**
  48. *Compressionmethodforcompressed(DEFLATED)entries.
  49. */
  50. publicstaticfinalintDEFLATED=ZipEntry.DEFLATED;
  51. privateStringencoding="UTF-8";
  52. publicZipOutputStream(OutputStreamout,Stringencoding){
  53. super(out,newDeflater(Deflater.DEFAULT_COMPRESSION,true));
  54. usesDefaultDeflater=true;
  55. this.encoding=encoding;
  56. }
  57. /**
  58. *CreatesanewZIPoutputstream.
  59. *
  60. *@paramout
  61. *theactualoutputstream
  62. */
  63. publicZipOutputStream(OutputStreamout){
  64. super(out,newDeflater(Deflater.DEFAULT_COMPRESSION,true));
  65. usesDefaultDeflater=true;
  66. }
  67. /**
  68. *SetstheZIPfilecomment.
  69. *
  70. *@paramcomment
  71. *thecommentstring
  72. *@exceptionIllegalArgumentException
  73. *ifthelengthofthespecifiedZIPfilecommentisgreater
  74. *than0xFFFFbytes
  75. */
  76. publicvoidsetComment(Stringcomment){
  77. if(comment!=null&&comment.length()>0xffff/3
  78. &&getUTF8Length(comment)>0xffff){
  79. thrownewIllegalArgumentException("ZIPfilecommenttoolong.");
  80. }
  81. this.comment=comment;
  82. }
  83. /**
  84. *Setsthedefaultcompressionmethodforsubsequententries.Thisdefault
  85. *willbeusedwheneverthecompressionmethodisnotspecifiedforan
  86. *individualZIPfileentry,andisinitiallysettoDEFLATED.
  87. *
  88. *@parammethod
  89. *thedefaultcompressionmethod
  90. *@exceptionIllegalArgumentException
  91. *ifthespecifiedcompressionmethodisinvalid
  92. */
  93. publicvoidsetMethod(intmethod){
  94. if(method!=DEFLATED&&method!=STORED){
  95. thrownewIllegalArgumentException("invalidcompressionmethod");
  96. }
  97. this.method=method;
  98. }
  99. /**
  100. *SetsthecompressionlevelforsubsequententrieswhichareDEFLATED.The
  101. *defaultsettingisDEFAULT_COMPRESSION.
  102. *
  103. *@paramlevel
  104. *thecompressionlevel(0-9)
  105. *@exceptionIllegalArgumentException
  106. *ifthecompressionlevelisinvalid
  107. */
  108. publicvoidsetLevel(intlevel){
  109. def.setLevel(level);
  110. }
  111. /**
  112. *BeginswritinganewZIPfileentryandpositionsthestreamtothestart
  113. *oftheentrydata.Closesthecurrententryifstillactive.Thedefault
  114. *compressionmethodwillbeusedifnocompressionmethodwasspecified
  115. *fortheentry,andthecurrenttimewillbeusediftheentryhasnoset
  116. *modificationtime.
  117. *
  118. *@parame
  119. *theZIPentrytobewritten
  120. *@exceptionZipException
  121. *ifaZIPformaterrorhasoccurred
  122. *@exceptionIOException
  123. *ifanI/Oerrorhasoccurred
  124. */
  125. publicvoidputNextEntry(ZipEntrye)throwsIOException{
  126. ensureOpen();
  127. if(entry!=null){
  128. closeEntry();//closepreviousentry
  129. }
  130. if(e.time==-1){
  131. e.setTime(System.currentTimeMillis());
  132. }
  133. if(e.method==-1){
  134. e.method=method;//usedefaultmethod
  135. }
  136. switch(e.method){
  137. caseDEFLATED:
  138. if(e.size==-1||e.csize==-1||e.crc==-1){
  139. //storesize,compressedsize,andcrc-32indatadescriptor
  140. //immediatelyfollowingthecompressedentrydata
  141. e.flag=8;
  142. }elseif(e.size!=-1&&e.csize!=-1&&e.crc!=-1){
  143. //storesize,compressedsize,andcrc-32inLOCheader
  144. e.flag=0;
  145. }else{
  146. thrownewZipException(
  147. "DEFLATEDentrymissingsize,compressedsize,orcrc-32");
  148. }
  149. e.version=20;
  150. break;
  151. caseSTORED:
  152. //compressedsize,uncompressedsize,andcrc-32mustallbe
  153. //setforentriesusingSTOREDcompressionmethod
  154. if(e.size==-1){
  155. e.size=e.csize;
  156. }elseif(e.csize==-1){
  157. e.csize=e.size;
  158. }elseif(e.size!=e.csize){
  159. thrownewZipException(
  160. "STOREDentrywherecompressed!=uncompressedsize");
  161. }
  162. if(e.size==-1||e.crc==-1){
  163. thrownewZipException(
  164. "STOREDentrymissingsize,compressedsize,orcrc-32");
  165. }
  166. e.version=10;
  167. e.flag=0;
  168. break;
  169. default:
  170. thrownewZipException("unsupportedcompressionmethod");
  171. }
  172. e.offset=written;
  173. if(names.put(e.name,e)!=null){
  174. thrownewZipException("duplicateentry:"+e.name);
  175. }
  176. writeLOC(e);
  177. entries.addElement(e);
  178. entry=e;
  179. }
  180. /**
  181. *ClosesthecurrentZIPentryandpositionsthestreamforwritingthe
  182. *nextentry.
  183. *
  184. *@exceptionZipException
  185. *ifaZIPformaterrorhasoccurred
  186. *@exceptionIOException
  187. *ifanI/Oerrorhasoccurred
  188. */
  189. publicvoidcloseEntry()throwsIOException{
  190. ensureOpen();
  191. ZipEntrye=entry;
  192. if(e!=null){
  193. switch(e.method){
  194. caseDEFLATED:
  195. def.finish();
  196. while(!def.finished()){
  197. deflate();
  198. }
  199. if((e.flag&8)==0){
  200. //verifysize,compressedsize,andcrc-32settings
  201. if(e.size!=def.getBytesRead()){
  202. thrownewZipException("invalidentrysize(expected"
  203. +e.size+"butgot"+def.getBytesRead()
  204. +"bytes)");
  205. }
  206. if(e.csize!=def.getBytesWritten()){
  207. thrownewZipException(
  208. "invalidentrycompressedsize(expected"
  209. +e.csize+"butgot"
  210. +def.getBytesWritten()+"bytes)");
  211. }
  212. if(e.crc!=crc.getValue()){
  213. thrownewZipException(
  214. "invalidentryCRC-32(expected0x"
  215. +Long.toHexString(e.crc)
  216. +"butgot0x"
  217. +Long.toHexString(crc.getValue())
  218. +")");
  219. }
  220. }else{
  221. e.size=def.getBytesRead();
  222. e.csize=def.getBytesWritten();
  223. e.crc=crc.getValue();
  224. writeEXT(e);
  225. }
  226. def.reset();
  227. written+=e.csize;
  228. break;
  229. caseSTORED:
  230. //wealreadyknowthatbothe.sizeande.csizearethesame
  231. if(e.size!=written-locoff){
  232. thrownewZipException("invalidentrysize(expected"
  233. +e.size+"butgot"+(written-locoff)
  234. +"bytes)");
  235. }
  236. if(e.crc!=crc.getValue()){
  237. thrownewZipException("invalidentrycrc-32(expected0x"
  238. +Long.toHexString(e.crc)+"butgot0x"
  239. +Long.toHexString(crc.getValue())+")");
  240. }
  241. break;
  242. default:
  243. thrownewInternalError("invalidcompressionmethod");
  244. }
  245. crc.reset();
  246. entry=null;
  247. }
  248. }
  249. /**
  250. *WritesanarrayofbytestothecurrentZIPentrydata.Thismethodwill
  251. *blockuntilallthebytesarewritten.
  252. *
  253. *@paramb
  254. *thedatatobewritten
  255. *@paramoff
  256. *thestartoffsetinthedata
  257. *@paramlen
  258. *thenumberofbytesthatarewritten
  259. *@exceptionZipException
  260. *ifaZIPfileerrorhasoccurred
  261. *@exceptionIOException
  262. *ifanI/Oerrorhasoccurred
  263. */
  264. publicsynchronizedvoidwrite(byte[]b,intoff,intlen)
  265. throwsIOException{
  266. ensureOpen();
  267. if(off<0||len<0||off>b.length-len){
  268. thrownewIndexOutOfBoundsException();
  269. }elseif(len==0){
  270. return;
  271. }
  272. if(entry==null){
  273. thrownewZipException("nocurrentZIPentry");
  274. }
  275. switch(entry.method){
  276. caseDEFLATED:
  277. super.write(b,off,len);
  278. break;
  279. caseSTORED:
  280. written+=len;
  281. if(written-locoff>entry.size){
  282. thrownewZipException(
  283. "attempttowritepastendofSTOREDentry");
  284. }
  285. out.write(b,off,len);
  286. break;
  287. default:
  288. thrownewInternalError("invalidcompressionmethod");
  289. }
  290. crc.update(b,off,len);
  291. }
  292. /**
  293. *FinisheswritingthecontentsoftheZIPoutputstreamwithoutclosing
  294. *theunderlyingstream.Usethismethodwhenapplyingmultiplefiltersin
  295. *successiontothesameoutputstream.
  296. *
  297. *@exceptionZipException
  298. *ifaZIPfileerrorhasoccurred
  299. *@exceptionIOException
  300. *ifanI/Oexceptionhasoccurred
  301. */
  302. publicvoidfinish()throwsIOException{
  303. ensureOpen();
  304. if(finished){
  305. return;
  306. }
  307. if(entry!=null){
  308. closeEntry();
  309. }
  310. if(entries.size()<1){
  311. thrownewZipException("ZIPfilemusthaveatleastoneentry");
  312. }
  313. //writecentraldirectory
  314. longoff=written;
  315. Enumeratione=entries.elements();
  316. while(e.hasMoreElements()){
  317. writeCEN((ZipEntry)e.nextElement());
  318. }
  319. writeEND(off,written-off);
  320. finished=true;
  321. }
  322. /**
  323. *ClosestheZIPoutputstreamaswellasthestreambeingfiltered.
  324. *
  325. *@exceptionZipException
  326. *ifaZIPfileerrorhasoccurred
  327. *@exceptionIOException
  328. *ifanI/Oerrorhasoccurred
  329. */
  330. publicvoidclose()throwsIOException{
  331. if(!closed){
  332. super.close();
  333. closed=true;
  334. }
  335. }
  336. /*
  337. *Writeslocalfile(LOC)headerforspecifiedentry.
  338. */
  339. privatevoidwriteLOC(ZipEntrye)throwsIOException{
  340. writeInt(LOCSIG);//LOCheadersignature
  341. writeShort(e.version);//versionneededtoextract
  342. writeShort(e.flag);//generalpurposebitflag
  343. writeShort(e.method);//compressionmethod
  344. writeInt(e.time);//lastmodificationtime
  345. if((e.flag&8)==8){
  346. //storesize,uncompressedsize,andcrc-32indatadescriptor
  347. //immediatelyfollowingcompressedentrydata
  348. writeInt(0);
  349. writeInt(0);
  350. writeInt(0);
  351. }else{
  352. writeInt(e.crc);//crc-32
  353. writeInt(e.csize);//compressedsize
  354. writeInt(e.size);//uncompressedsize
  355. }
  356. //byte[]nameBytes=getUTF8Bytes(e.name);
  357. byte[]nameBytes=null;
  358. try{
  359. if(this.encoding.toUpperCase().equals("UTF-8"))
  360. nameBytes=getUTF8Bytes(e.name);
  361. else
  362. nameBytes=e.name.getBytes(this.encoding);
  363. }catch(ExceptionbyteE){
  364. nameBytes=getUTF8Bytes(e.name);
  365. }
  366. writeShort(nameBytes.length);
  367. writeShort(e.extra!=null?e.extra.length:0);
  368. writeBytes(nameBytes,0,nameBytes.length);
  369. if(e.extra!=null){
  370. writeBytes(e.extra,0,e.extra.length);
  371. }
  372. locoff=written;
  373. }
  374. /*
  375. *Writesextradatadescriptor(EXT)forspecifiedentry.
  376. */
  377. privatevoidwriteEXT(ZipEntrye)throwsIOException{
  378. writeInt(EXTSIG);//EXTheadersignature
  379. writeInt(e.crc);//crc-32
  380. writeInt(e.csize);//compressedsize
  381. writeInt(e.size);//uncompressedsize
  382. }
  383. /*
  384. *Writecentraldirectory(CEN)headerforspecifiedentry.REMIND:add
  385. *supportforfileattributes
  386. */
  387. privatevoidwriteCEN(ZipEntrye)throwsIOException{
  388. writeInt(CENSIG);//CENheadersignature
  389. writeShort(e.version);//versionmadeby
  390. writeShort(e.version);//versionneededtoextract
  391. writeShort(e.flag);//generalpurposebitflag
  392. writeShort(e.method);//compressionmethod
  393. writeInt(e.time);//lastmodificationtime
  394. writeInt(e.crc);//crc-32
  395. writeInt(e.csize);//compressedsize
  396. writeInt(e.size);//uncompressedsize
  397. //byte[]nameBytes=getUTF8Bytes(e.name);
  398. byte[]nameBytes=null;
  399. try{
  400. if(this.encoding.toUpperCase().equals("UTF-8"))
  401. nameBytes=getUTF8Bytes(e.name);
  402. else
  403. nameBytes=e.name.getBytes(this.encoding);
  404. }catch(ExceptionbyteE){
  405. nameBytes=getUTF8Bytes(e.name);
  406. }
  407. writeShort(nameBytes.length);
  408. writeShort(e.extra!=null?e.extra.length:0);
  409. byte[]commentBytes;
  410. if(e.comment!=null){
  411. //commentBytes=getUTF8Bytes(e.comment);
  412. try{
  413. if(this.encoding.toUpperCase().equals("UTF-8"))
  414. commentBytes=getUTF8Bytes(e.comment);
  415. else
  416. commentBytes=e.comment.getBytes(this.encoding);
  417. }catch(ExceptionbyteE){
  418. commentBytes=getUTF8Bytes(e.comment);
  419. }
  420. writeShort(commentBytes.length);
  421. }else{
  422. commentBytes=null;
  423. writeShort(0);
  424. }
  425. writeShort(0);//startingdisknumber
  426. writeShort(0);//internalfileattributes(unused)
  427. writeInt(0);//externalfileattributes(unused)
  428. writeInt(e.offset);//relativeoffsetoflocalheader
  429. writeBytes(nameBytes,0,nameBytes.length);
  430. if(e.extra!=null){
  431. writeBytes(e.extra,0,e.extra.length);
  432. }
  433. if(commentBytes!=null){
  434. writeBytes(commentBytes,0,commentBytes.length);
  435. }
  436. }
  437. /*
  438. *Writesendofcentraldirectory(END)header.
  439. */
  440. privatevoidwriteEND(longoff,longlen)throwsIOException{
  441. writeInt(ENDSIG);//ENDrecordsignature
  442. writeShort(0);//numberofthisdisk
  443. writeShort(0);//centraldirectorystartdisk
  444. writeShort(entries.size());//numberofdirectoryentriesondisk
  445. writeShort(entries.size());//totalnumberofdirectoryentries
  446. writeInt(len);//lengthofcentraldirectory
  447. writeInt(off);//offsetofcentraldirectory
  448. if(comment!=null){//zipfilecomment
  449. //byte[]b=getUTF8Bytes(comment);
  450. byte[]b=null;
  451. try{
  452. if(this.encoding.toUpperCase().equals("UTF-8"))
  453. b=getUTF8Bytes(comment);
  454. else
  455. b=comment.getBytes(this.encoding);
  456. }catch(ExceptionbyteE){
  457. b=getUTF8Bytes(comment);
  458. }
  459. writeShort(b.length);
  460. writeBytes(b,0,b.length);
  461. }else{
  462. writeShort(0);
  463. }
  464. }
  465. /*
  466. *Writesa16-bitshorttotheoutputstreaminlittle-endianbyteorder.
  467. */
  468. privatevoidwriteShort(intv)throwsIOException{
  469. OutputStreamout=this.out;
  470. out.write((v>>>0)&0xff);
  471. out.write((v>>>8)&0xff);
  472. written+=2;
  473. }
  474. /*
  475. *Writesa32-bitinttotheoutputstreaminlittle-endianbyteorder.
  476. */
  477. privatevoidwriteInt(longv)throwsIOException{
  478. OutputStreamout=this.out;
  479. out.write((int)((v>>>0)&0xff));
  480. out.write((int)((v>>>8)&0xff));
  481. out.write((int)((v>>>16)&0xff));
  482. out.write((int)((v>>>24)&0xff));
  483. written+=4;
  484. }
  485. /*
  486. *Writesanarrayofbytestotheoutputstream.
  487. */
  488. privatevoidwriteBytes(byte[]b,intoff,intlen)throwsIOException{
  489. super.out.write(b,off,len);
  490. written+=len;
  491. }
  492. /*
  493. *ReturnsthelengthofString'sUTF8encoding.
  494. */
  495. staticintgetUTF8Length(Strings){
  496. intcount=0;
  497. for(inti=0;i<s.length();i++){
  498. charch=s.charAt(i);
  499. if(ch<=0x7f){
  500. count++;
  501. }elseif(ch<=0x7ff){
  502. count+=2;
  503. }else{
  504. count+=3;
  505. }
  506. }
  507. returncount;
  508. }
  509. /*
  510. *ReturnsanarrayofbytesrepresentingtheUTF8encodingofthespecified
  511. *String.
  512. */
  513. privatestaticbyte[]getUTF8Bytes(Strings){
  514. char[]c=s.toCharArray();
  515. intlen=c.length;
  516. //Countthenumberofencodedbytes...
  517. intcount=0;
  518. for(inti=0;i<len;i++){
  519. intch=c[i];
  520. if(ch<=0x7f){
  521. count++;
  522. }elseif(ch<=0x7ff){
  523. count+=2;
  524. }else{
  525. count+=3;
  526. }
  527. }
  528. //Nowreturntheencodedbytes...
  529. byte[]b=newbyte[count];
  530. intoff=0;
  531. for(inti=0;i<len;i++){
  532. intch=c[i];
  533. if(ch<=0x7f){
  534. b[off++]=(byte)ch;
  535. }elseif(ch<=0x7ff){
  536. b[off++]=(byte)((ch>>6)|0xc0);
  537. b[off++]=(byte)((ch&0x3f)|0x80);
  538. }else{
  539. b[off++]=(byte)((ch>>12)|0xe0);
  540. b[off++]=(byte)(((ch>>6)&0x3f)|0x80);
  541. b[off++]=(byte)((ch&0x3f)|0x80);
  542. }
  543. }
  544. returnb;
  545. }
  546. }

剩下的工作就是編寫壓縮和解壓縮的功能類了ZipUtil.java

  1. packagecom.work.util;
  2. importjava.io.BufferedInputStream;
  3. importjava.io.BufferedOutputStream;
  4. importjava.io.File;
  5. importjava.io.FileInputStream;
  6. importjava.io.FileNotFoundException;
  7. importjava.io.FileOutputStream;
  8. importjava.io.IOException;
  9. importjava.io.InputStream;
  10. importorg.apache.commons.logging.Log;
  11. importorg.apache.commons.logging.LogFactory;
  12. importcom.work.util.zip.ZipInputStream;
  13. importcom.work.util.zip.ZipOutputStream;
  14. importcom.work.util.zip.ZipEntry;
  15. /**
  16. *@projectproxool-0.9.1中的java.util.zip包和此類聯合使用
  17. *@authorwangmingjie
  18. *
  19. */
  20. publicclassZipUtil{
  21. privatestaticLoglog=LogFactory.getLog(ZipUtil.class);
  22. /**
  23. *解壓縮文件夾。
  24. *
  25. *@paramsrcFilename
  26. *@paramdestFilename
  27. *@paramoverwrite
  28. *@throwsIOException
  29. */
  30. publicvoidunzipDir(StringsrcFilename,booleanoverwrite)throwsIOException{
  31. FilesrcFile=newFile(srcFilename);
  32. //首先判斷源文件是否存在
  33. if(!srcFile.exists()){
  34. thrownewFileNotFoundException("Cannotfindthesourcefile:"
  35. +srcFile.getAbsolutePath());
  36. }
  37. //判斷源文件是否可讀
  38. if(!srcFile.canRead()){
  39. thrownewIOException("Cannotreadthesourcefile:"
  40. +srcFile.getAbsolutePath());
  41. }
  42. FileInputStreamfin=null;
  43. BufferedInputStreaminputStream=null;
  44. ZipInputStreamzipInputStream=null;
  45. try{
  46. fin=newFileInputStream(srcFile);
  47. inputStream=newBufferedInputStream(fin);
  48. zipInputStream=newZipInputStream(inputStream,"gbk");//在這裏設置編碼格式,解決文件名和備註亂碼問題
  49. ZipEntryzipEntry=null;
  50. StringtempFileName=null;
  51. while((zipEntry=zipInputStream.getNextEntry())!=null){
  52. tempFileName=zipEntry.getName();
  53. if(zipEntry.isDirectory()){
  54. //創建文件夾
  55. FileUtil.createDirs(tempFileName,true);
  56. }else{
  57. tempFileName=tempFileName.replace("\\","/");
  58. //System.out.println("文件夾="+tempFileName.substring(0,tempFileName.lastIndexOf("/")));
  59. FileUtil.createDirs(tempFileName.substring(0,tempFileName.lastIndexOf("/")),true);
  60. //System.out.println("tempFileName="+tempFileName);
  61. unzipFile(zipInputStream,tempFileName);//解壓縮
  62. }
  63. zipInputStream.closeEntry();//關閉當前ZIP條目並定位流以讀取下一個條目。
  64. //不寫上面的一句,程序也能夠正常運行。
  65. }
  66. }finally{
  67. if(zipInputStream!=null){
  68. try{
  69. zipInputStream.close();
  70. }catch(IOExceptionex){
  71. //justignore
  72. }
  73. }
  74. if(inputStream!=null){
  75. try{
  76. inputStream.close();
  77. }catch(IOExceptionex){
發佈了3 篇原創文章 · 獲贊 0 · 訪問量 2673
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章