码迷,mamicode.com
首页 > 其他好文 > 详细

解压缩工具包

时间:2020-05-25 19:26:31      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:方法   directory   tput   list   zip   cto   解压   style   void   

package com.ytd.ebos.platform.zjmb.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author Administrator ZIP 解压缩工具包
 */
/**
 * @author Administrator
 *
 */
public class ZipUtils {

    /**
     * Default buff byte size
     *
     */
    private static final int DEFAULT_BUFF_SIZE = 1024;

    /**
     * Default basedir value
     *
     */
    private static final boolean DEFAULT_DIR = false;

    /**
    // 程序入口1,输入需要解压的文件全路径,然后在当前文件夹下面解压出文件
     * @param srcPath
     * @throws Exception
     */
    public static void decompress(String srcPath) throws Exception {
        decompress(new File(srcPath));
    }

    /**
    // 程序入口2,输入需要解压的文件File类,然后在当前文件夹下面解压出文件
     * @param srcFile 
     * @throws Exception
     */
    public static void decompress(File srcFile) throws Exception {
        File baseFile = srcFile.getParentFile();//输出上层目录
        decompress(srcFile, baseFile);
    }

    /**
    // 程序入口3 ,输入需要解压的的文件全路径,以及解压到目的文件全路径
     * @param srcPath
     * @param destPath
     * @throws Exception
     */
    public static void decompress(String srcPath, String destPath) throws Exception {
        decompress(new File(srcPath), new File(destPath));
    }

    /**
    // 程序入口4,输入需要解压的文件File类,输入目的文件夹File类
     * @param srcFile
     * @param destFile
     * @throws Exception
     */
    public static void decompress(File srcFile, File destFile) throws Exception {
        CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());
        ZipInputStream zis = new ZipInputStream(cis);
//        如果是multipartFile,可以使用如下方法取得zipInputStream
//        InputStream inputStream = multipartFile.getInputStream();
//        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(inputStream),Charset.forName("UTF-8"));
        doDecompress(destFile, zis);
        zis.close();
    }

    
    /**输入目的文件夹File类,输入压缩文件流
     * @param destFile 
     * @param zis
     * @throws Exception 
     */
    private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {
        ZipEntry zipEntry = null;
        while ((zipEntry = zis.getNextEntry()) != null) {
            String dir = destFile.getPath() + File.separator + zipEntry.getName();
            File dirFile = new File(dir);
            fileProber(dirFile);
//            解压出来的是文件文件夹,则直接创建
            if (zipEntry.isDirectory()) {
                dirFile.mkdirs();
            } else {
                doDecompressFile(dirFile, zis);
            }
            zis.closeEntry();
        }
    }

    /**边读边写,把输入流程写到文件
     * @param destFile
     * @param zis
     * @throws Exception 
     */
    private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
        int len;
        byte[] buff = new byte[DEFAULT_BUFF_SIZE];
        while ((len = zis.read(buff, 0, DEFAULT_BUFF_SIZE)) != -1) {
            bos.write(buff, 0, len);
        }
        bos.close();
    }

    /**
     * 文件探测,如果没有父级文件夹,则一直创建
     *
     * When the parent file not exist.Create it.
     *
     * @param dirFile
     * @throws Exception
     */
    public static void fileProber(File dirFile) throws Exception {
        File parentFile = dirFile.getParentFile();
        if (!parentFile.exists()) {
            fileProber(parentFile);
            parentFile.mkdirs();
        }
    }

    /**
     * @param srcPath
     * @param destPath
     * @param dirFlag
     * @throws Exception
     */
    public static void compress(String srcPath, String destPath, boolean dirFlag) throws Exception {
        compress(new File(srcPath), new File(destPath), dirFlag);
    }

    /**
     * @param srcPath
     * @param destPath
     * @throws Exception
     */
    public static void compress(String srcPath, String destPath) throws Exception {
        compress(new File(srcPath), new File(destPath), DEFAULT_DIR);
    }

    /**
     * @param srcFile
     * @param destFile
     * @param dirFlag
     * @throws Exception
     */
    public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {
        compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);
    }

    /**
     * @param srcFile
     * @param zos
     * @param dirFlag
     * @throws Exception
     */
    public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {
        if (srcFile.isDirectory()) {
            if (dirFlag) {
                doCompress(zos, srcFile, srcFile.getName() + File.separator);
            } else {
                doCompress(zos, srcFile, "");
            }
        } else {
            doCompress(zos, srcFile, "");
        }
        zos.close();
    }

    /**
     * @param zos
     * @param file
     * @param baseDir
     * @throws Exception
     */
    public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                doCompress(zos, files[i], baseDir);
            }
        } else {
            byte[] buff = new byte[DEFAULT_BUFF_SIZE];
            InputStream in = new FileInputStream(file);
            zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));
            int len;
            while ((len = in.read(buff, 0, DEFAULT_BUFF_SIZE)) != -1) {
                zos.write(buff, 0, len);
            }
            in.close();
        }
    }
    
    public static void main(String[] args) throws Exception{
        String dir = "C:\\Users\\Administrator\\Desktop\\test\\" ;
        String src = "C:\\Users\\Administrator\\Desktop\\xxxx.zip" ;
        
//        decompress(src);
        decompress(src,dir);
        
    }
}

 

解压缩工具包

标签:方法   directory   tput   list   zip   cto   解压   style   void   

原文地址:https://www.cnblogs.com/rdchen/p/12959123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!