練功房推薦書單

  • 猛虎出柙雙劍合璧版--最新 OCA / OCP Java SE 7 Programmer 專業認證 (電子書)
  • 流浪教師存零股存到3000萬(全新增修版)(書+DVD)
  • 開始在關西自助旅行(京都‧大阪‧神戶‧奈良)(全新增訂版)
  • 不敗教主的300張股票存股術

JSP精選實用範例(六):檔案解壓縮 RSS feed
討論區首頁 » 網頁程式設計 Web Development
發表人 內容
andowson

七段學員
[Avatar]

註冊時間: 2007/1/2
文章: 711
來自: 台北
離線
Java內建支援ZIP格式檔案的壓縮及解壓縮,透過使用java.util.zip套件,我們可以用來將網頁上傳的ZIP檔案加以解壓縮再做後續處理。這裡我們可以先將解壓縮的固定步驟寫成一個UnZipBean,後續要解壓縮zip時只要先將檔名及解壓縮的目錄傳進去,再呼叫unzip()函式即可。

程式碼:
UnZipBean.java:
/*

* Copyright (c) 2005, Andowson Chang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the "Andowson Chang" nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.

* This file creation date: 2005/01/31 18:33:36
* Reference:
* http://www.wakhok.ac.jp/~tatsuo/sen97/10shuu/UnZip.java.html
*/
package com.andowson;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class UnZipBean {
public static final int EOF = -1;
static final int BUFFER = 2048;

private String zipFile;
private String targetDirectory;
private ZipFile zf;

/** Constructor */
public UnZipBean() {
}

public UnZipBean(String zipFile, String targetDirectory) {
this.zipFile = zipFile;
this.targetDirectory = targetDirectory;
}

public void setZipFile(String zipFile) {
this.zipFile = zipFile;
}

public String getZipFile() {
return zipFile;
}

public void setTargetDirectory(String targetDirectory) {
this.targetDirectory = targetDirectory;
}

public String getTargetDirectory() {
return targetDirectory;
}

public boolean unzip() {
boolean done = false;
if (zipFile != null) {
try {
zf = new ZipFile(zipFile);
Enumeration enumeration = zf.entries();
while (enumeration.hasMoreElements()) {
ZipEntry target = (ZipEntry)enumeration.nextElement();
System.out.print(target.getName() + " .");
saveEntry(target);
System.out.println(". unpacked");
}
done = true;
}
catch (FileNotFoundException e){
System.out.println("zipfile not found"+e.getMessage());
}
catch (ZipException e){
System.out.println("zip error..."+e.getMessage());
}
catch (IOException e){
System.out.println("IO error..."+e.getMessage());
}
finally {
try {
zf.close();
} catch (IOException e) {
System.out.println("IO error...Can't close zip file"+e.getMessage());
}
}
}
return done;
}

private void saveEntry(ZipEntry target)
throws ZipException, IOException {
try {
File file = new File(targetDirectory + File.separator + target.getName());
if (target.isDirectory()) {
file.mkdirs();
}
else {
InputStream is = zf.getInputStream(target);
BufferedInputStream bis = new BufferedInputStream(is);
File dir = new File(file.getParent());
dir.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);

int c;
byte[] data = new byte[BUFFER];
while((c = bis.read(data, 0, BUFFER)) != EOF) {
bos.write(data, 0, c);
}
bos.flush();
bos.close();
fos.close();
}
}
catch (ZipException e) {
throw e;
}
catch (IOException e) {
throw e;
}
}
}

unziptest.jsp:

<%@ page contentType="text/html; charset=Big5" errorPage="" %>
<%@ page import="com.andowson.UnZipBean" %>
<%
String zipFile = "c:\\temp\\test.zip";
String targetDirectory = application.getRealPath("/tmp");
UnZipBean uzb = new UnZipBean(zipFile, targetDirectory);
boolean succ = uzb.unzip();
if (succ) out.println(uzb.getZipFile() + " UnZipped");
%>


參考資料:
http://java.sun.com/developer/technicalArticles/Programming/compression/
http://www.wakhok.ac.jp/~tatsuo/sen97/10shuu/UnZip.java.html
 檔案名稱 UnZipBean.java [Disk] 下載
 描述 com.andowson.UnZipBean
 檔案大小 5 Kbytes
 下載次數:  57 次

 檔案名稱 unziptest.jsp [Disk] 下載
 描述 java.util.zip 解壓縮範例程式
 檔案大小 377 bytes
 下載次數:  46 次


分享經驗 累積智慧
[WWW]
 
討論區首頁 » 網頁程式設計 Web Development
前往:   
行動版