一个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;
}
}
一个zip压缩类,欢迎吐槽的更多相关文章
- php ZIP压缩类实例分享
php ZIP压缩类实例分享 <?php $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt ...
- C#zip压缩类
改造了网上的代码,可以压缩文件夹.指定文件列表(文件和文件夹的混合列表,需要指定子文件夹下需要压缩的文件),注释很详细不做解释 public class ZipHelper { /// <sum ...
- 最近工作用到压缩,写一个zip压缩工具类
package test; import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream ...
- zip压缩类
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...
- 【C#】依赖于SharpZipLib的Zip压缩工具类
上班第二天下班,课外作业,实现一个ZIP压缩的工具类.本来想用Package,但是写完了才发现不能解压其他工具压缩的zip包,比较麻烦,因此本工具类依赖了第三方的库(SharpZipLib vers ...
- C#实现Zip压缩解压实例
原文地址:https://www.cnblogs.com/GoCircle/p/6544678.html 本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZi ...
- C# ZIP 压缩解压
ZIP流是在NetFramework4.5 引入的目的是为了能够更好的操作ZIP文件,进行压缩解压等操作.与ZIP流相关的几个类是: ZipArchive 代表一个ZIP的压缩包文件 ZipArchi ...
- C# - ZIP 压缩流
C# - ZIP 压缩流 参考资料 https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchive?view= ...
- zip压缩详细分析
该文章转自:http://www.cnblogs.com/esingchan/p/3958962.html (文章写得很详细,让我对zip压缩有了了解,感谢博主,贴在这是为了防止忘了有这么好的文章,侵 ...
随机推荐
- JAVA的字节码技术
1.什么是字节码? 字节码 byteCode JVM能够解释执行的.java程序的归宿,但是从规范上来讲和Java已没有任何关系了.一些动态语言也可以编译成字节码在JVM上运行.字节码就相当于JVM上 ...
- gif修改背景透明
1.用ImageReady打开,将选中所有帧,右键选择“恢复为背景”. 2.打开“颜色板”,点击左下角的惊叹号,用吸色器点击背景,颜色板自动选中了背景色,将其映射为透明. 3.文件->将优化结果 ...
- C# 时间戳
C# 时间与时间戳互转 13位 /// <summary> /// 将c# DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <pa ...
- css/js online online code editor/formator/debuger
http://cssdeck.com/labs http://jsfiddle.net/ http://fiddle.jshell.net/ support console http://plnkr ...
- 初解DLL基本知识
1.DLL基本理论 在Windows操作系统中,几乎所有的内容都是以DLL的形式存在的. 1.DLL基本概念 语言程序要从目标代码(.obj)外部引用函数,可以通过俩种途径实现——静态链接和动态链接. ...
- 交互式shell和非交互式shell、登录shell和非登录shell的区别
交互式shell和非交互式shell.登录shell和非登录shell的区别.首先,这是两个不同的维度来划分的,一个是是否交互式,另一个是是否登录. 交互式shell和非交互式shell(intera ...
- MySQL中的配置参数interactive_timeout和wait_timeout(可能导致过多sleep进程的两个参数)
1)interactive_timeout:参数含义:服务器关闭交互式连接前等待活动的秒数.交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的 ...
- Fixing:insert_modules not found
搞linux的最怕的就是panic.满屏的报错不知头绪,百度出来的还都是抄来抄去的垃圾. 我遇到的错误已经解决,所以不想再看到报错了..google出来两个没有上下文的文本,因为和他们差不多,在下面贴 ...
- lucas求组合数C(n,k)%p
Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037 #include<cstdio> typedef __int64 L ...
- Microsoft SQLServer有四种系统数据库
Microsoft SQLServer有四种系统数据库: 1.master数据库 master数据库记录SQLServer系统的所有系统级别信息.它记录所有的登录帐户和系统配置设置.master数据库 ...