zoukankan      html  css  js  c++  java
  • java压缩与解压文件

    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.ArrayList;
    import java.util.List;
    
    import org.apache.tools.zip.ZipEntry;
    import org.apache.tools.zip.ZipOutputStream;
    
    import cn.zsmy.constant.Constant;
    import cn.zsmy.utils.FileBaseUtil;
    
    /**
     * 将文件或是文件夹打包压缩成zip格式
     * 
     * @author shm
     */
    public class ZipUtils {
        private ZipUtils() {
        };
        
        /**
         * 把指定文件夹下的所有文件压缩成zip包
         * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
         * @param sourceFilePath :待压缩的文件路径
         * @param zipFilePath :压缩后存放路径
         * @param fileName :压缩后文件的名称
         * @return
         */
        public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName, Boolean isDrop){
            boolean flag = false;
            File sourceFile = new File(sourceFilePath);
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            FileOutputStream fos = null;
            ZipOutputStream zos = null;
            
            if(isDrop && sourceFile.exists()){
                FileBaseUtil.deleteFile(zipFilePath, fileName);
            }
            
            if(sourceFile.exists() == false){
                Constant.MY_LOG.debug("待压缩的文件目录:"+sourceFilePath+"不存在.");
            }else{
                try {
                    File zipFile = new File(zipFilePath + "/" + fileName);
                    if(zipFile.exists()){
                        Constant.MY_LOG.debug(zipFilePath + "目录下存在名字为:" + fileName +"打包文件.");
                    }else{
                        File[] sourceFiles = sourceFile.listFiles();
                        if(null == sourceFiles || sourceFiles.length<1){
                            Constant.MY_LOG.debug("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
                        }else{
                            fos = new FileOutputStream(zipFile);
                            zos = new ZipOutputStream(new BufferedOutputStream(fos));
                            byte[] bufs = new byte[1024*10];
                            for(int i=0;i<sourceFiles.length;i++){
                                //创建ZIP实体,并添加进压缩包
                                ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                                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);
                                }
                            }
                            flag = true;
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } finally{
                    //关闭流
                    try {
                        if(null != bis) bis.close();
                        if(null != zos) zos.close();
                        if(null != fis) fis.close();
                        if(null != fos) fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                }
            }
            return flag;
        }
        
         /** 
         * 压缩文件 
         * @param srcfile File[] 需要压缩的文件列表 
         * @param zipfile File 压缩后的文件 
         */  
        public static void zipFiles(List<File> srcfile, String zipfile) {  
            byte[] buf = new byte[1024];
            ZipOutputStream out = null;
            try {  
                // Create the ZIP file  
                out = new ZipOutputStream(new FileOutputStream(zipfile));  
                // Compress the files  
                for (int i = 0; i < srcfile.size(); i++) {  
                    File file = srcfile.get(i);  
                    FileInputStream in = new FileInputStream(file);  
                    // Add ZIP entry to output stream.  
                    out.putNextEntry(new ZipEntry(file.getName()));  
                    // Transfer bytes from the file to the ZIP file  
                    int len;  
                    while ((len = in.read(buf)) > 0) {  
                        out.write(buf, 0, len);  
                    }  
                    // Complete the entry  
                    out.closeEntry();  
                    in.close();  
                }  
                // Complete the ZIP file  
               
            } catch (IOException e) {  
                Constant.MY_LOG.error("ZipUtil zipFiles exception:"+e);  
            }  finally {
                 try {
                    if(out != null){
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    
                }  
            }
        }  
      
        public static void main(String[] args){
            String sourceFilePath = "D:/nginx-1.9.9/html/upload/download";
            String zipFilePath = "D:/nginx-1.9.9/html/upload/download";
            /*String fileName = "提现记录";
            boolean flag = fileToZip(sourceFilePath, zipFilePath, fileName);
            if(flag){
                System.out.println("文件打包成功!");
            }else{
                System.out.println("文件打包失败!");
            }*/
            
            
            List<File> listFile = new ArrayList<File>();
            listFile.add(new File(sourceFilePath+"/提现记录.xlsx"));
            listFile.add(new File(sourceFilePath+"/提现记录 - 副本.xlsx"));
            zipFiles(listFile,  zipFilePath+"/a.rar");
        }
    }

    1.txt

    商户号|终端号|交易类型|交易子类型|总笔数|总金额|总手续费|清算时间
    100000276|100000990|04311|00|60|315664.20|0.00|2016-10-17
    100000276|100000991|10311|00|4|2000.00|0.00|2016-10-17
    商户号|终端号|交易类型|交易子类型|订单号|商户订单号|清算日期|订单状态|交易金额|手续费|交易号|支付订单创建时间|商户退款订单号|退款订单创建时间
    100000276|100000990|04311|00|401579336|G0000101653|2016-10-17|1|603.00|0.00|201610160110000401579336|2016-10-17 11:15:40|
    100000276|100000990|04311|00|401579350|G0000101655|2016-10-17|1|1.00|0.00|201610160110000401579350|2016-10-17 11:15:40|
    100000276|100000990|04311|00|401579351|G0000101656|2016-10-17|1|4.00|0.00|201610160110000401579351|2016-10-17 11:15:40|
  • 相关阅读:
    大二实习使用的技术汇总(下)
    【JSP】JSTL使用core标签总结(不断更新中)
    凸包---HDU 2202
    poj
    [MFC]MFC中OnDraw与OnPaint的区别
    PHP 自动生成导航网址的最佳方法 v20130826
    汉语-词语-调料:百科
    汉语-词语-味觉:百科
    汉语-词语-本源:百科
    un-人物-企业家-迈纳·基思:百科
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6835997.html
Copyright © 2011-2022 走看看