屏幕录象java版软件,小心共享下.一时兴起写的哦.__解压zip包:ZipFetcher.java

来源:互联网 发布:深圳数据库开发培训 编辑:程序博客网 时间:2024/04/29 03:05

屏幕录象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();
    }
}

原创粉丝点击