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|
java压缩与解压文件的更多相关文章
- java 压缩以及解压文件,有tar,zip,gz(gizp)和解压
package com.yabsz.decompCompr; import java.io.File; import java.util.ArrayList; import java.util.Lis ...
- AIX系统上压缩与解压文件
压缩. 命令格式: #tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加,比如打包时,将其他文件追加进来使用该参数. t:显示tar包里的内容,但还原 ...
- Linux学习笔记之AIX系统上压缩与解压文件
0x00 概述 AIX机器真难用,一时半会还真适应不了. 0x01 压缩tar 命令格式: # tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加 ...
- C# 压缩、解压文件夹或文件(带密码)
今天梳理一下项目中用到的压缩.解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件.今天就好好整理记录下,别下次遇到需求又重头开始了. DotNetZip DotNetZip是一个开源的免 ...
- java 压缩与解压
最近复习到IO,想找个案例做一做,恰好下载了许多图片压缩包,查看图片很不方便,所以打算用IO把图片都解压到同一个文件夹下.然后集中打包. 本例使用jdk自带的ZipInputStream和ZipOut ...
- java压缩与解压
一 概述 1.目录进入点 目录进入点是文件在压缩文件中的映射,代表压缩文件.压缩文件时,创建目录进入点,将文件写入该目录进入点.解压时,获取目录进入点,将该目录进入点的内容写入硬盘指定文件. 如果目录 ...
- Linux命令(16)压缩,解压文件
tar: 简介:tar命令只是把目录打包成一个归档(文件),并不负责压缩.在tar命令中可以带参数调用gzip或bzip2压缩.因为gzip和bzip2只能压缩单个文件. 在linux下是不需要后缀名 ...
- C#工具类:使用SharpZipLib进行压缩、解压文件
SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...
- 本地上传文件至服务器的技巧(linux文件压缩及解压文件)
linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...
随机推荐
- testlink 从1.8.5 升级到 1.9.8
step1:备份原 1.8.5 的数据库. step2:分别下载 1.9.0 / 1.9.3 / 1.9.8 的安装包. step3:分别解压 1.9.0 / 1.9.3 / 1.9.8 成3 ...
- matlab化简符号表达式
化简符号表达式计算机毕竟还是挺笨的, 经过一系列的符号计算后, 得到的结果可能只有它自己才能看懂, Matlab提供大量函数以用于符号表达式的化简. collect(f): 函数用途是合并多项式中相同 ...
- IT实习总结-上海的实习的日子
在上海实习两个多月,从找工作,到面试,最后到创业公司工作,体会了一个人在外工作的那种工作时的压力与双休时的寂寞,有时甚至有点迷惘,可能这就是生活,每个人都会经历,在经历的过程中会有不同的味道. 说说我 ...
- git 控制操作
克隆文件 git clone https://gitee.com/xxxxx/xxxxx.git 克隆分支文件 git clone -b 分支名 https://gitee.com/xxxxx/xxx ...
- Oracle反向字符截取逗號分隔字符串
DECLARE M ); BEGIN FOR I IN ( WITH T AS (SELECT REVERSE('i,am,a,test,hahahhah') AS STR FROM DUAL) SE ...
- bat2exe 就是这么简单 白研究半天VC++了
bat2exe 就是这么简单 白研究半天VC++了 结果:bat2exe编译的执行文件会被杀毒软件查杀.
- CE工具里自带的学习工具--第二关
图解:
- 使用GetLogicalDrives获取卷标
#include<stdio.h> #include<windows.h> int main() { DWORD dwLogical= GetLogicalDrives(); ...
- Java IO(四--字符流基本使用
在上一节,介绍了字节流的基本使用,本节介绍一下字符流的使用 Reader: public abstract class Reader implements Readable, Closeable { ...
- EditControl 限制输入文本的三种方法
下边是三种限制编辑框输入内容的方法.在VS里建立基于对话框的应用程序CMyEdit,打开资源视图,删除对话框上自带的确定和取消按钮.然后添加一编辑框控件,将其ID修改为IDC_MY_EDIT,通过类向 ...