• 一个zip压缩类,欢迎吐槽


    package com.utils;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.Adler32;
    import java.util.zip.CheckedOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.zte.ums.ems.rrucheck.output.OutputFilesFilter;
    
    public class ZipUtil {
    
        private Logger logger;
        public ZipUtil() {
            logger=LoggerFactory.getLogger(ZipUtil.class);
        }
        public ZipUtil(Logger logger) {
            this.logger=logger;
        }
    
        public String zipAndDelSourceFiles_OMMB(String dir){
            
            String sourceNameRegex="(filename1|filename2)\.csv";
            String targetFileName="zipname.zip";
            String zipFilePath = null;
            try {
                if (zip(dir,sourceNameRegex,targetFileName)) {
                    zipFilePath=new File(dir,targetFileName).getCanonicalPath();
                    FilePathUtil.del(dir, sourceNameRegex);
                    zipFilePath=new File(dir,targetFileName).getCanonicalPath();
                }
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
            return zipFilePath;
        }
        
        public boolean zip(String sourceFilePath,String sourceNameRegex,String targetFileName) throws IOException{
    
            File target=new File(sourceFilePath,targetFileName);
            File dir=target.getParentFile();
            
            File[] sourceFiles=dir.listFiles(new OutputFilesFilter(sourceNameRegex));
    
            return zip(sourceFiles,target);
        }
        
        public boolean zip(File[] sorceFiles,File targetZipFile){
            boolean result = false;
            try {
                FileOutputStream target = new FileOutputStream(targetZipFile);
                CheckedOutputStream cos = new CheckedOutputStream(target,
                        new Adler32());
    
                ZipOutputStream zos = new ZipOutputStream(cos);
    
                BufferedOutputStream out = new BufferedOutputStream(zos);
    
                DataInputStream in = null;
                try {
                    for (File sorceFile : sorceFiles) {
                        zos.putNextEntry(new ZipEntry(sorceFile.getName()));
                        int count;
    
                        in = new DataInputStream(new BufferedInputStream(
                                new FileInputStream(sorceFile)));
    
                        while ((count = in.read()) != -1) {
    
                            out.write(count);
    
                        }
                        out.flush();
                        in.close();
                    }
                } catch (IOException e) {
                    logger.error(e.getMessage());
                } finally {
                    try {
                        out.close();
                    } catch (IOException e) {
                        logger.error(e.getMessage());
                    }
                }
    
                result = true;
    
            } catch (FileNotFoundException e) {
                logger.error(e.getMessage());
            }
            return result;
        }
    }
  • 相关阅读:
    django1.8模板位置的设置setting.py
    django创建工程,用命令
    杨辉三角(生成器generator)
    Git操作的一些注意
    git的一些常用操作命令
    Python合并列表,append()、extend()、+、+=
    ElementTree 解析xml(minidom解析xml大文件时,MemoryError)
    Spring Aop(十六)——编程式的自定义Advisor
    Spring Aop(十五)——Aop原理之Advised接口
    Spring Aop(十四)——Aop自动创建代理对象的原理
  • 原文地址:https://www.cnblogs.com/softidea/p/3811558.html
Copyright © 2020-2023  润新知