文件压缩流工具类

/**
 * 
 */
package com.hdrs.ris.law2.large.xml.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;

/**
 * @project_name aml_second
 * @package_name com.hdrs.ris.law2.large.xml.utils
 * @class_name WriteXmlAndZipUtil
 * @author 邵欣
 * @company 新致软件
 * @version 1.0
 * @time 2017年9月12日 下午3:10:34
 */
public class WriteXmlAndZipUtil implements deleteFile {
    public static String UTF8 = "<?xml version="1.0" encoding="UTF-8"?>";

    public static String GBK = "<?xml version="1.0" encoding="GBK"?>";

    public static String GB18030 = "<?xml version="1.0" encoding="GB18030"?>";

    public static String NEWLINE = "
";

    public static String N = "N";// 普通报文

    public static String R = "R";// 重发报文

    public static String A = "A";// 补报报文

    public static String C = "C";// 纠错报文

    public static String I = "I";// 补正报文

    public static String D = "D";// 删除错误报文(只有保险业大额交易报告有此类型)

    public static String IH = "IH";// 保险业大额

    public static String IS = "IS";// 保险业可疑

    public static String XML = ".xml";// xml后缀

    public static String ZIP = ".ZIP";// zip文件后缀

    public static String SEG = "-";// zip文件分隔符

    /**
     * 生成ZIP文件
     * 
     * @param filePath
     *            文件根路径
     * @param zipFileName
     *            压缩文件名
     * @param xmlFileName
     *            xml文件名
     * @param xmlHead
     *            xml文件头
     * @param xmlBody
     *            xml文件体 void
     * @author 邵欣
     * @time 2017年9月12日下午6:49:27
     */
    public static void writeZIPFile(String filePath, String dirFileName, String xmlHead, String xmlBody) {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        BufferedOutputStream bos = null;
        File sourceFile = new File(filePath + dirFileName);// 文件夹路径及文件夹名称
        if (sourceFile.exists() == false) {
            System.out.println("待压缩的文件目录:" + dirFileName + "不存在.");
        } else {
            try {
                File zipFile = new File(filePath + dirFileName + WriteXmlAndZipUtil.ZIP);// 压缩文件名称及其存储位置
                if (zipFile.exists()) {
                    System.out.println(filePath + "目录下存在名字为:" + dirFileName + ".zip" + "打包文件.");
                } else {
                    File[] sourceFiles = sourceFile.listFiles();
                    if (null == sourceFiles || sourceFiles.length < 1) {
                        System.out.println("待压缩的文件目录:" + dirFileName + "里面不存在文件,无需压缩.");
                    } else {
                        fos = new FileOutputStream(zipFile);
                        bos = new BufferedOutputStream(fos);
                        zos = new ZipOutputStream(bos);
                        byte[] bufs = new byte[1024 * 10];
                        for (int i = 0; i < sourceFiles.length; i++) {
                            ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());// 创建ZIP实体
                            zos.putNextEntry(zipEntry);// 添加进压缩包
                            fis = new FileInputStream(sourceFiles[i]); // 读取待压缩的文件并写进压缩包里
                            bis = new BufferedInputStream(fis, 1024 * 10);
                            int read = 0;
                            while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                                zos.write(bufs, 0, read);// 写入压缩文件
                            }
                            if (null != bis) {// 关闭字符缓冲输出流
                                bis.close();
                            }
                            if (null != fis) {// 关闭文件输出流
                                fis.close();
                            }
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } finally {
                // 关闭流
                try {
                    if (null != zos) {// 关闭压缩输入流
                        zos.close();
                    }
                    if (null != bos) {// 关闭字符缓冲输入流
                        bos.close();
                    }
                    if (null != fos) {// 关闭文件输出流
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
        if (sourceFile.exists() == false) {
            System.out.println("目录不存在!");
        } else {
            deleteFile(sourceFile);// 删除临时文件夹及其子目录文件
        }
    }

    /**
     * 删除临时文件
     * 
     * @param sourceFile
     *            临时文件夹名称 void
     * @author 邵欣
     * @time 2017年9月13日下午2:01:45
     */
    private static void deleteFile(File sourceFile) {
        File[] sourceFiles = sourceFile.listFiles();
        for (int i = 0; i < sourceFiles.length; i++) {
            sourceFiles[i].delete();// 删除文件夹内部文件
        }
        sourceFile.delete();// 删除文件夹
    }

    /**
     * 生成xml文件
     * 
     * @param filePath
     *            生成的文件路径
     * @param dirFileName
     *            生成的文件夹名称
     * @param xmlFileName
     *            xml文件名称
     * @param xmlHead
     *            xml文件头选择编码格式
     * @param xmlBody
     *            xml文件内容 void
     * @author 邵欣
     * @time 2017年9月13日上午9:22:26
     */
    public static void writeXMLFile(String filePath, String dirFileName, String xmlFileName, String xmlHead,
            String xmlBody) {
        try {
            File file = new File(filePath + dirFileName + "/" + xmlFileName);// 生成文件路径
            FileUtils.writeStringToFile(file, xmlBody);// 生成文件内容
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拿到xml文件名称
     * 
     * @param msgType
     *            报文类型标识
     * @param safeType
     *            交易报告类型标识
     * @param companyNum
     *            报告机构编码
     * @param dateTime
     *            报送日期
     * @param packageNum
     *            当日报送的批次
     * @param fileNum
     *            报文在该批次的编号
     * @return String 
     * @author 邵欣
     * @time 2017年9月13日下午1:58:24
     */
    public static String getXmlFileName(String msgType, String safeType, String companyNum, String dateTime,
            String packageNum, String fileNum) {
        // NIS031021101020001-20070306-0049-0021.XML
        // 报文类型和交易报告类型标识+报告机构编码+报送日期+当日报送的批次+报文在该批次的编号.XML
        StringBuffer sb = new StringBuffer();
        if (msgType != null) {
            sb.append(msgType);
            if (safeType != null) {
                sb.append(safeType);
                if (companyNum != null) {
                    sb.append(companyNum + WriteXmlAndZipUtil.SEG);
                    if (dateTime != null) {
                        sb.append(dateTime + WriteXmlAndZipUtil.SEG);
                        if (packageNum != null) {
                            sb.append(packageNum + WriteXmlAndZipUtil.SEG);
                            if (fileNum != null) {
                                sb.append(fileNum + WriteXmlAndZipUtil.XML);
                            } else {
                                throw new RuntimeException("NollPointException : fileNum");
                            }
                        } else {
                            throw new RuntimeException("NollPointException : packageNum");
                        }
                    } else {
                        throw new RuntimeException("NollPointException : dateTime");
                    }
                } else {
                    throw new RuntimeException("NollPointException : companyNum");
                }
            } else {
                throw new RuntimeException("NollPointException : safeType");
            }
        } else {
            throw new RuntimeException("NollPointException : msgType");
        }
        return sb.toString();
    }

    /**
     * 获取文件名称
     * 
     * @param msgType
     *            数据包类型标识
     * @param safeType
     *            交易报告类型标识
     * @param companyNum
     *            报告机构编码
     * @param dateTime
     *            报送日期
     * @param batchNum
     *            当日报送的批次
     * @return String
     * @author 邵欣
     * @time 2017年9月13日上午9:28:16
     */
    public static String getDirFileName(String msgType, String safeType, String companyNum, String dateTime,
            String batchNum) {
        // 数据包类型和交易报告类型标识+报告机构编码+报送日期+当日报送的批次.ZIP
        // 数据包名称的各部分之间用“-”(半角)分隔。
        StringBuffer sb = new StringBuffer();
        if (msgType != null) {
            sb.append(msgType);
            if (safeType != null) {
                sb.append(safeType);
                if (companyNum != null) {
                    sb.append(companyNum + WriteXmlAndZipUtil.SEG);
                    if (dateTime != null) {
                        sb.append(dateTime + WriteXmlAndZipUtil.SEG);
                        if (batchNum != null) {
                            sb.append(batchNum);
                        } else {
                            throw new RuntimeException("NollPointException : batchNum");
                        }
                    } else {
                        throw new RuntimeException("NollPointException : dateTime");
                    }
                } else {
                    throw new RuntimeException("NollPointException : companyNum");
                }
            } else {
                throw new RuntimeException("NollPointException : safeType");
            }
        } else {
            throw new RuntimeException("NollPointException : msgType");
        }
        return sb.toString();
    }

}
原文地址:https://www.cnblogs.com/ShaoXin/p/7514669.html