<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "JSP精選實用範例(六):檔案解壓縮"]]></title>
		<link>https://forum.andowson.com/posts/list/5.page</link>
		<description><![CDATA[Latest messages posted in the topic "JSP精選實用範例(六):檔案解壓縮"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>JSP精選實用範例(六):檔案解壓縮</title>
				<description><![CDATA[ Java內建支援ZIP格式檔案的壓縮及解壓縮，透過使用[url=http://java.sun.com/javase/6/docs/api/java/util/zip/package-summary.html]java.util.zip[/url]套件，我們可以用來將網頁上傳的ZIP檔案加以解壓縮再做後續處理。這裡我們可以先將解壓縮的固定步驟寫成一個UnZipBean，後續要解壓縮zip時只要先將檔名及解壓縮的目錄傳進去，再呼叫unzip()函式即可。 
<br>
<br>
程式碼： 
<br>
UnZipBean.java: 
<br>
[code]/* 
<br>
 * Copyright (c) 2005, Andowson Chang 
<br>
 * All rights reserved. 
<br>
 * 
<br>
 * Redistribution and use in source and binary forms, with or without 
<br>
 * modification, are permitted provided that the following conditions 
<br>
 * are met: 
<br>
 * 
<br>
 * 1. Redistributions of source code must retain the above copyright 
<br>
 * notice, this list of conditions and the following disclaimer. 
<br>
 * 2. Redistributions in binary form must reproduce the above copyright 
<br>
 * notice, this list of conditions and the following disclaimer in 
<br>
 * the documentation and/or other materials provided with the 
<br>
 * distribution. 
<br>
 * 3. Neither the name of the "Andowson Chang" nor the names of its 
<br>
 * contributors may be used to endorse or promote products derived 
<br>
 * from this software without specific prior written permission. 
<br>
 * 
<br>
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
<br>
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
<br>
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
<br>
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
<br>
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
<br>
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
<br>
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
<br>
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
<br>
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
<br>
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
<br>
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
<br>
 * POSSIBILITY OF SUCH DAMAGE. 
<br>
<br>
 * This file creation date: 2005/01/31 18:33:36 
<br>
 * Reference: 
<br>
 * http://www.wakhok.ac.jp/~tatsuo/sen97/10shuu/UnZip.java.html 
<br>
 */ 
<br>
package com.andowson; 
<br>
<br>
import java.io.BufferedInputStream; 
<br>
import java.io.BufferedOutputStream; 
<br>
import java.io.File; 
<br>
import java.io.FileOutputStream; 
<br>
import java.io.FileNotFoundException; 
<br>
import java.io.InputStream; 
<br>
import java.io.IOException; 
<br>
import java.util.Enumeration; 
<br>
import java.util.zip.ZipEntry; 
<br>
import java.util.zip.ZipException; 
<br>
import java.util.zip.ZipFile; 
<br>
<br>
public class UnZipBean { 
<br>
 public static final int EOF = -1; 
<br>
 static final int BUFFER = 2048; 
<br>
<br>
 private String zipFile; 
<br>
 private String targetDirectory; 
<br>
 private ZipFile zf; 
<br>
<br>
 /** Constructor */ 
<br>
 public UnZipBean() { 
<br>
 } 
<br>
<br>
 public UnZipBean(String zipFile, String targetDirectory) { 
<br>
 this.zipFile = zipFile; 
<br>
 this.targetDirectory = targetDirectory; 
<br>
 } 
<br>
<br>
 public void setZipFile(String zipFile) { 
<br>
 this.zipFile = zipFile; 
<br>
 } 
<br>
<br>
 public String getZipFile() { 
<br>
 return zipFile; 
<br>
 } 
<br>
<br>
 public void setTargetDirectory(String targetDirectory) { 
<br>
 this.targetDirectory = targetDirectory; 
<br>
 } 
<br>
<br>
 public String getTargetDirectory() { 
<br>
 return targetDirectory; 
<br>
 } 
<br>
<br>
 public boolean unzip() { 
<br>
 boolean done = false; 
<br>
 if (zipFile != null) { 
<br>
 try { 
<br>
 zf = new ZipFile(zipFile); 
<br>
 Enumeration enumeration = zf.entries(); 
<br>
 while (enumeration.hasMoreElements()) { 
<br>
 ZipEntry target = (ZipEntry)enumeration.nextElement(); 
<br>
 System.out.print(target.getName() + " ."); 
<br>
 saveEntry(target); 
<br>
 System.out.println(". unpacked"); 
<br>
 } 
<br>
 done = true; 
<br>
 } 
<br>
 catch (FileNotFoundException e){ 
<br>
 System.out.println("zipfile not found"+e.getMessage()); 
<br>
 } 
<br>
 catch (ZipException e){ 
<br>
 System.out.println("zip error..."+e.getMessage()); 
<br>
 } 
<br>
 catch (IOException e){ 
<br>
 System.out.println("IO error..."+e.getMessage()); 
<br>
 } 
<br>
 finally { 
<br>
 try { 
<br>
 zf.close(); 
<br>
 } catch (IOException e) { 
<br>
 System.out.println("IO error...Can't close zip file"+e.getMessage()); 
<br>
 } 
<br>
 } 
<br>
 } 
<br>
 return done; 
<br>
 } 
<br>
<br>
 private void saveEntry(ZipEntry target) 
<br>
 throws ZipException, IOException { 
<br>
 try { 
<br>
 File file = new File(targetDirectory + File.separator + target.getName()); 
<br>
 if (target.isDirectory()) { 
<br>
 file.mkdirs(); 
<br>
 } 
<br>
 else { 
<br>
 InputStream is = zf.getInputStream(target); 
<br>
 BufferedInputStream bis = new BufferedInputStream(is); 
<br>
 File dir = new File(file.getParent()); 
<br>
 dir.mkdirs(); 
<br>
 FileOutputStream fos = new FileOutputStream(file); 
<br>
 BufferedOutputStream bos = new BufferedOutputStream(fos); 
<br>
<br>
 int c; 
<br>
 byte[] data = new byte[BUFFER]; 
<br>
 while((c = bis.read(data, 0, BUFFER)) != EOF) { 
<br>
 bos.write(data, 0, c); 
<br>
 } 
<br>
 bos.flush(); 
<br>
 bos.close(); 
<br>
 fos.close(); 
<br>
 } 
<br>
 } 
<br>
 catch (ZipException e) { 
<br>
 throw e; 
<br>
 } 
<br>
 catch (IOException e) { 
<br>
 throw e; 
<br>
 } 
<br>
 } 
<br>
}[/code] 
<br>
unziptest.jsp: 
<br>
[code] 
<br>
&lt;%@ page contentType="text/html; charset=Big5" errorPage="" %&gt; 
<br>
&lt;%@ page import="com.andowson.UnZipBean" %&gt; 
<br>
&lt;% 
<br>
 String zipFile = "c:\\temp\\test.zip"; 
<br>
 String targetDirectory = application.getRealPath("/tmp"); 
<br>
 UnZipBean uzb = new UnZipBean(zipFile, targetDirectory); 
<br>
 boolean succ = uzb.unzip(); 
<br>
 if (succ) out.println(uzb.getZipFile() + " UnZipped"); 
<br>
%&gt; 
<br>
[/code] 
<br>
<br>
參考資料： 
<br>
http://java.sun.com/developer/technicalArticles/Programming/compression/ 
<br>
http://www.wakhok.ac.jp/~tatsuo/sen97/10shuu/UnZip.java.html]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/266/440.page</guid>
				<link>https://forum.andowson.com/posts/preList/266/440.page</link>
				<pubDate><![CDATA[Fri, 22 Aug 2008 23:29:37]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>