屏幕錄象java版軟件,小心共享下.一時興起寫的哦.__解壓zip包:ZipFetcher.java

屏幕錄象java版軟件,小心共享下.一時興起寫的哦.__解壓zip包:ZipFetcher.java

//解壓zip包
package com.zip;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

public class ZipFetcher {
    byte[] by = new byte[1024];

    public void fetch(String srcFilepath, String destFilepath) throws Exception {
    }

    /** *//**
     * 解壓zip,需要ant.jar包
     *
     * @param srcFilepath
     *            源文件地址
     * @param destFilepath
     *            目標目錄
     * @throws Exception
     */
    public void unzip(String srcFilepath, String destFilepath)
            throws Exception {
    System.out.println("aaa==========="+srcFilepath+"         "+destFilepath);
    srcFilepath=srcFilepath.replace("/", "//");
        // zip數據流
        ZipFile zipFile = new ZipFile(srcFilepath);
        // zip中的項
        ZipEntry zipEntry = null;

        Enumeration e = zipFile.getEntries();

        while (e.hasMoreElements()) {
            zipEntry = (ZipEntry) e.nextElement();
            System.out.println("解壓文件 : " + zipEntry.getName());
            // zipentry是目錄
            if (zipEntry.isDirectory()) {
                // 處理目錄
                dealDir(zipEntry, destFilepath);
            }
            // zipentry是文件
            else {
                // 處理文件
                dealFile(zipFile, zipEntry, destFilepath);
            }
        }
        zipFile.close();
    }

    /** *//**
     * 解決zip中嵌套zip
     *
     * @param zipEntry
     *            源zip項
     * @param destFilepath
     *            文件保存路徑
     * @throws Exception
     */
    private void dealZipInZip(ZipEntry zipEntry, String destFilepath)
            throws Exception {
        // 嵌套
        String fileName = zipEntry.getName();
        // 儲存路徑,降後面的.zip去掉,建立文件夾
        String toPath = destFilepath + File.separator
                + fileName.substring(0, fileName.length() - 4);
        // 源文件
        String srcfile = destFilepath + File.separator + fileName;

        unzip(srcfile, toPath);// 解析子zip包
        delete(srcfile);
    }

    /** *//**
     * 如果zip項是目錄,創建目錄
     *
     * @param zipEntry
     * @param destFilepath
     */
    private void dealDir(ZipEntry zipEntry, String destFilepath) {
        String name = zipEntry.getName();// 取得文件名,因爲是目錄,所以最後一位是/
        name = name.substring(0, name.length() - 1);// 去掉最後一位

        // 建立目錄
        File dir = new File(destFilepath + File.separator + name);
        if (!dir.exists())
            dir.mkdirs();

        System.out.println("創建目錄:" + destFilepath + File.separator + name);
    }

    /** *//**
     * zip項是文件,處理文件
     *
     * @param zipFile
     *            全部zip數據流
     * @param zipEntry
     *            zip項
     * @param destFilepath
     *            目標路徑
     * @throws Exception
     */
    private void dealFile(ZipFile zipFile, ZipEntry zipEntry,
            String destFilepath) throws Exception {
        // 取得文件的保存路徑和文件名
        String fname = destFilepath + File.separator + zipEntry.getName();
        mkpath(fname);// 建立文件的目錄

        write(zipFile.getInputStream(zipEntry), new FileOutputStream(fname));

        if (isZip(zipEntry.getName()))
            dealZipInZip(zipEntry, destFilepath);
    }

    /** *//**
     * 寫入
     *
     * @param in
     * @param out
     * @throws IOException
     */
    private void write(InputStream in, OutputStream out) throws IOException {
        int c;
        try {
            while ((c = in.read(by)) != -1)
                out.write(by, 0, c);
        } catch (IOException ex) {
            throw ex;
        } finally {
            close(in, out);
        }
    }

    /** *//**
     * 創建文件的路徑
     *
     * @param fname
     *            文件名
     */
    private void mkpath(String fname) {
        File f = new File(fname);
        File pf = f.getParentFile();
        if (!pf.exists())
            pf.mkdirs();
    }

    /** *//**
     * 判斷zip項是否還是zip文件
     *
     * @param fname
     * @return
     */
    private boolean isZip(String fname) {
        if (fname.length() < 5)
            return false;
        int idx = fname.lastIndexOf(".");
        if (idx == -1)
            return false;
        if (!fname.substring(idx + 1, fname.length()).equalsIgnoreCase("zip"))
            return false;
        return true;
    }

    /** *//**
     * 關閉
     *
     * @param in
     * @param out
     */
    private void close(InputStream in, OutputStream out) {
        if (null != in) {
            try {
                in.close();
            } catch (Exception ex) {
                // ignore
                ex.printStackTrace();
            } finally {
                if (null != in)
                    in = null;
            }
        }
        if (null != out) {
            try {
                out.close();
            } catch (Exception ex) {
                // ignore
            } finally {
                if (null != out)
                    out = null;
            }
        }
    }

    /** *//**
     * 刪除文件,如果源zip包中包含zip文件,解析後刪除所包含的zip文件
     *
     * @param filename
     */
    private void delete(String filename) {
        File f = new File(filename);
        if (f.exists() && f.delete())
            System.out.println("delete " + filename + " OK!");
        else
            System.out.println("delete " + filename + " Failed!");
    }

    public static void main(String[] args) throws Exception {
        ZipFetcher test = new ZipFetcher();
        test.unzip("c://123.zip", "c:/dddd");

        // File test = new File("f:/新建文件夾/b1.zip");
        // test.delete();
    }
}

發佈了139 篇原創文章 · 獲贊 5 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章