ZipOutputStream压缩(NIO)

package com.murong.ecp.app;

import com.yuangou.ecp.bp.core.common.yglog4j.Logger;
import org.apache.axis.utils.StringUtils;

import java.io.*;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

class Test {

    /**
     * 大文件压缩(只针对文件)
     * @param target 压缩后文件
     * @param srouce 源文件
     * @param isKeep 是否保留源文件(true 保留)
     * @param logger 日志
     * @return
     */
    public static boolean zipBigFile(String target, String srouce, boolean isKeep) {
        ZipOutputStream zipOut = null;
        try {
            if (StringUtils.isEmpty(target) || StringUtils.isEmpty(srouce)) {
                return false;
            }
            File srouceFile = new File(srouce);
            if (srouceFile.exists()) {
                File targetFile = createFile(target);
                //文件 则直接对问价进行压缩
                if (srouceFile.isFile()) {
                    boolean zipFlg = zipFile(targetFile, srouceFile);
                    //删除源文件
                    if (zipFlg && !isKeep) {
                        srouceFile.delete();
                    }
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (zipOut != null) {
                try {
                    zipOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     *
     * @param target 压缩文件
     * @param srouce 源文件
     * @return
     * @throws Exception
     */
    private static boolean zipFile(File target, File srouce) throws Exception {
        ZipOutputStream zipOut = null;
        WritableByteChannel writableByteChannel = null;
        FileChannel fileChannel = null;
        try {
            //目标文件输出流
            //压缩流
            zipOut = new ZipOutputStream(new FileOutputStream(target));
            //可写管道
            writableByteChannel = Channels.newChannel(zipOut);
            //源文件管道
            fileChannel = new FileInputStream(srouce).getChannel();
            //将源文件输入流 转至 压缩输出流
            zipOut.putNextEntry(new ZipEntry(srouce.getName()));
            fileChannel.transferTo(0, srouce.length(), writableByteChannel);
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (zipOut != null) {
                zipOut.close();
            }
            if (writableByteChannel != null) {
                writableByteChannel.close();
            }
            if (fileChannel != null) {
                fileChannel.close();
            }
        }
    }

    /**
     * 创建文件/目录 文件存在则删除
     * @param target
     * @return
     * @throws IOException
     */
    private static File createFile(String target) throws IOException {
        File file = new File(target);
        //文件存在 则删除文件重新创建
        if (file.exists() && file.isFile()) {
            file.delete();
            file.createNewFile();
        } else if (!file.exists() && file.isFile()) {
            //文件不存在则创建
            file.createNewFile();
        } else if (!file.exists() && file.isDirectory()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 压缩目录
     * @param target 压缩后文件
     * @param srouce 源目录
     * @return
     */
    public static boolean zipFiles(String target, String srouce) {
        ZipOutputStream zos = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            File targetFile = createFile(target);
            File srouceFile = new File(srouce);
            fos = new FileOutputStream(targetFile);
            bos = new BufferedOutputStream(fos);
            zos = new ZipOutputStream(bos);
            if (srouceFile.exists()) {
                compress(zos, srouceFile, "");
                return true;
            }
        } catch (Exception e) {
//            logger.error(e);
            e.printStackTrace();
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
//                    logger.error(e);
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     *
     * @param zos 压缩流
     * @param srouce 源文件
     * @param parentPath 压缩包内容路径
     * @throws Exception
     */
    private static void compress(ZipOutputStream zos, File srouce, String parentPath) throws Exception {
        if (zos == null || srouce == null || !srouce.exists()) {
            throw new Exception("压缩文件异常");
        }
        //文件
        if (srouce.isDirectory()) {
            parentPath += srouce.getName() + File.separator;
            File[] files = srouce.listFiles();
            if (files != null && files.length > 0) {
                //压缩目录下所有文件/目录
                for (File file : files) {
                    compress(zos, file, parentPath);
                }
            } else if (files != null && files.length == 0) {
                //空目录直接压缩
                zos.putNextEntry(new ZipEntry(parentPath));
            }
        } else if (srouce.isFile()) {
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(srouce);
                bis = new BufferedInputStream(fis);
                zos.putNextEntry(new ZipEntry(parentPath + srouce.getName()));
                byte[] buff = new byte[1024 * 8];
                int len = 0;
                while ((len = bis.read(buff)) != -1) {
                    zos.write(buff, 0, len);
                    zos.flush();
                }
            } catch (Exception e) {
                throw e;
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (Exception e) {

                    }
                    try {
                        bis.close();
                    } catch (Exception e) {

                    }
                }
            }
        }
    }

    /**
     * 使用管道 压缩
     * @param target
     * @param srouce
     * @param logger
     * @return
     */
    public static boolean fasterZipFile(String target, String srouce) {
        ZipOutputStream zos = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        WritableByteChannel wbc = null;
        try {
            File targetFile = createFile(target);
            File srouceFile = new File(srouce);
            fos = new FileOutputStream(targetFile);
            bos = new BufferedOutputStream(fos);
            zos = new ZipOutputStream(bos);
            wbc = Channels.newChannel(zos);
            if (srouceFile.exists()) {
                fasterCompress(wbc, zos, srouceFile, "");
                return true;
            }
        } catch (Exception e) {
//            logger.error(e);
            e.printStackTrace();
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
//                    logger.error(e);
                    e.printStackTrace();
                }
            }
            if (wbc != null) {
                try {
                    wbc.close();
                } catch (Exception e) {
//                    logger.error(e);
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     *
     * @param wbc 可写通道
     * @param zos 压缩流
     * @param srouce 源文件
     * @param parentPath
     * @throws Exception
     */
    private static void fasterCompress(WritableByteChannel wbc, ZipOutputStream zos, File srouce, String parentPath) throws Exception {
        if (zos == null || srouce == null || !srouce.exists()) {
            throw new Exception("压缩文件异常");
        }
        //文件
        if (srouce.isDirectory()) {
            parentPath += srouce.getName() + File.separator;
            File[] files = srouce.listFiles();
            if (files != null && files.length > 0) {
                //压缩目录下所有文件/目录
                for (File file : files) {
                    compress(zos, file, parentPath);
                }
            } else if (files != null && files.length == 0) {
                //空目录直接压缩
                zos.putNextEntry(new ZipEntry(parentPath));

            }
        } else if (srouce.isFile()) {
            FileInputStream fis = null;
            FileChannel fileChannel = null;
            try {
                fis = new FileInputStream(srouce);
                zos.putNextEntry(new ZipEntry(parentPath + srouce.getName()));
                fileChannel = fis.getChannel();
                fileChannel.transferTo(0, srouce.length(), wbc);
            } catch (Exception e) {
                throw e;
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (Exception e) {

                    }
                }
                if (fileChannel != null) {
                    try {
                        fileChannel.close();
                    } catch (Exception e) {

                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        fasterZipFile("D:\\DOWNLOAD\\UAT\\tx.zip", "D:\\DOWNLOAD\\UAT\\123");
    }

}

 

你可能感兴趣的:(备用代码)