練功房推薦書單

  • Google!Android 3手機應用程式設計入門(第四版)
  • 賈伯斯傳(軟皮精裝版)
  • 猛虎出閘制霸版:最新OCP Java SE 6 Programmer專業認證(附原始程式碼及範例檔)
  • SCWCD 5 猛虎出閘:Java Web 應用程式專業認證
JSP精選實用範例(六):檔案解壓縮  XML
Forum Index » 網頁程式設計 Web Development
Author Message
andowson

六段學員
[Avatar]

Joined: 2007-01-02 22:20:40
Messages: 652
Location: 台北
Offline

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
 Filename unziptest.jsp [Disk] Download
 Description java.util.zip 解壓縮範例程式
 Filesize 377 bytes
 Downloaded:  30 time(s)

 Filename UnZipBean.java [Disk] Download
 Description com.andowson.UnZipBean
 Filesize 5 Kbytes
 Downloaded:  35 time(s)

This message was edited 6 times. Last update was at 2008-08-23 09:49:26


分享經驗 累積智慧
[WWW] [MSN]
 
Forum Index » 網頁程式設計 Web Development
Go to: