博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java压缩与解压文件
阅读量:6994 次
发布时间:2019-06-27

本文共 4610 字,大约阅读时间需要 15 分钟。

 

 

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
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
listFile = new ArrayList
(); 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-17100000276|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|

 

转载地址:http://ozsvl.baihongyu.com/

你可能感兴趣的文章
春节期间,怎样晒朋友圈才安全?
查看>>
SD-WAN行业发展需要VNF演进
查看>>
开发漫谈:我爱编程语言的四大原因
查看>>
加密解密技术基础及PKI
查看>>
spring源码解读-xml中配置一个bean到容器的生产一个bean实例都经历了那些过程
查看>>
php instanceof 与 is_a()区别
查看>>
错误: 代理抛出异常错误: java.net.MalformedURLException: L...
查看>>
推荐android studio一个插件,可以将布局分组的
查看>>
数值型
查看>>
Hadoop集群搭建(-v1.2.1)
查看>>
内网可以访问外网,外网不能访问内网的ACL解决方法
查看>>
UITableView 编辑和删除行
查看>>
第一章,Linux常用命令
查看>>
如何在列表页面调用自定义字段值显示
查看>>
spring 使用小结
查看>>
最简单oppo系统一键激活xposed框架经验
查看>>
iptraf用法
查看>>
我的友情链接
查看>>
集算器提升Java的计算能力
查看>>
【创建型】- 建造者模式
查看>>