iT邦幫忙

DAY 19
2

JSP 學習分享系列 第 19

JSP 和Zip Tool

  • 分享至 

  • xImage
  •  

使用程式來達到解壓縮檔案的功能
1.先把相關的參數做成bean
=== UnZipBean.java ===

	package bean;

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;
        }
   }
}

2.寫一個簡單的jsp去使用剛才的程式
=== unzipDemo.jsp===

	<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="bean.UnZipBean" %>
<%
    String zipFile = "c:\\download\\apache-tomcat-6.0.20-deployer.zip";
   String targetDirectory = application.getRealPath("/tmp");
   System.out.println("targetDirectory:"+targetDirectory);
    UnZipBean uzb = new UnZipBean(zipFile, targetDirectory);
    boolean succ = uzb.unzip();
    if (succ) out.println(uzb.getZipFile() + " UnZipped");
%>

上一篇
JSP 和 apache的FTP Tool
下一篇
JSP 和資料庫和 html Table的範例
系列文
JSP 學習分享30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言