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|

java压缩与解压文件的更多相关文章

  1. java 压缩以及解压文件,有tar,zip,gz(gizp)和解压

    package com.yabsz.decompCompr; import java.io.File; import java.util.ArrayList; import java.util.Lis ...

  2. AIX系统上压缩与解压文件

    压缩. 命令格式: #tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加,比如打包时,将其他文件追加进来使用该参数. t:显示tar包里的内容,但还原 ...

  3. Linux学习笔记之AIX系统上压缩与解压文件

    0x00 概述 AIX机器真难用,一时半会还真适应不了.   0x01 压缩tar 命令格式: # tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加 ...

  4. C# 压缩、解压文件夹或文件(带密码)

    今天梳理一下项目中用到的压缩.解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件.今天就好好整理记录下,别下次遇到需求又重头开始了. DotNetZip DotNetZip是一个开源的免 ...

  5. java 压缩与解压

    最近复习到IO,想找个案例做一做,恰好下载了许多图片压缩包,查看图片很不方便,所以打算用IO把图片都解压到同一个文件夹下.然后集中打包. 本例使用jdk自带的ZipInputStream和ZipOut ...

  6. java压缩与解压

    一 概述 1.目录进入点 目录进入点是文件在压缩文件中的映射,代表压缩文件.压缩文件时,创建目录进入点,将文件写入该目录进入点.解压时,获取目录进入点,将该目录进入点的内容写入硬盘指定文件. 如果目录 ...

  7. Linux命令(16)压缩,解压文件

    tar: 简介:tar命令只是把目录打包成一个归档(文件),并不负责压缩.在tar命令中可以带参数调用gzip或bzip2压缩.因为gzip和bzip2只能压缩单个文件. 在linux下是不需要后缀名 ...

  8. C#工具类:使用SharpZipLib进行压缩、解压文件

    SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...

  9. 本地上传文件至服务器的技巧(linux文件压缩及解压文件)

    linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...

随机推荐

  1. 结对项目--黄金点游戏(邓乐&曾亮)

    #include<stdio.h> #include<stdlib.h> #include<Windows.h> int result[100][1000000]; ...

  2. scrapy 请求传参

    class MovieSpider(scrapy.Spider): name = 'movie' allowed_domains = ['www.id97.com'] start_urls = ['h ...

  3. 关于Maven项目的pom.xml中的依赖或插件失效的解决方法

    1.请将<dependency>标签包含的依赖从<dependencyManagement>中拿出来,单独放在<dependencies>标签里面.2.请将< ...

  4. 第3节 hive高级用法:16、17、18

    第3节 hive高级用法:16.hive当中常用的几种数据存储格式对比:17.存储方式与压缩格式相结合:18.总结 hive当中的数据存储格式: 行式存储:textFile sequenceFile ...

  5. js判断图片是否有效

    var ImgObj=new Image(); ImgObj.src= 'http://192.168.10.6:8082/3D/SERVER_1_DELL_880.jpg'; if(ImgObj.f ...

  6. js ajax 传送xml dom对象到服务器

    客户端代码 1 <script> var isie = true; var xmlhttp = null; function createXMLHTTP() {//创建XMLXMLHttp ...

  7. PDO、PDOStatement、PDOException

    最近在学PDO  比较详细的资料 出处:http://blog.csdn.net/hsst027/article/details/23682003 PDO中包含三个预定义的类,它们分别是PDO.PDO ...

  8. Python之面向对象封装

    Python之面向对象封装 封装不是单纯意义的隐藏 什么是封装: 将数据放在一个设定好的盒子里,并标出数据可以实现的功能,将功能按钮外露,而隐藏其功能的工作原理,就是封装. 要怎么封装: 你余额宝有多 ...

  9. Qt 多语言支持

    简介 Qt 多语言支持很强大,很好用. 首先要强调的是程序中需要翻译的字符串最好都用 tr("message") 这种形式,这里的 "message" 就是需要 ...

  10. 集训第六周 古典概型 期望 C题

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=30728 一个立体方块,每个单位方块都是关闭状态,每次任两个点,以这两点为对角 ...