一个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压缩有了了解,感谢博主,贴在这是为了防止忘了有这么好的文章,侵 ...
随机推荐
- C# 枚举,传入int值返回string值
需求:1:子公司负责人2:人事3:审批人4:签批人 5:管理员 传入值为1,2,3,4,5这个数字的某一个.需要返回他们的中文描述. 一下忘记该怎么写了...后来百度下查出来了..记录下当个小工具吧 ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- DBus接口文档
gitgit.projects.genivi.org / ipc / common-api-dbus-tools.git / blob? search: re 0544e985b6e4a6c83ddf ...
- [转]一个基于完成端口的TCP Server Framework,浅析IOCP
[转]一个基于完成端口的TCP Server Framework,浅析IOCP http://www.cppblog.com/adapterofcoms/archive/2010/06/26/1187 ...
- (转) java 复制文件,不使用输出流复制,高效率,文件通道的方式复制文件
public static void fileChannelCopy(File s, File t) { FileInputStream fi = null; FileOutputStream fo ...
- JavaWeb常见错误总结
错误1:(Ajax未加载问题) 今天我在学习AJAX的时候,想从一个JS文件中引入JQuery,然后在这个JS文件中使用AJAX的方法.结果V8引擎一直报错,错误类型是Uncaught Referen ...
- Jenkins入门-转
reference : http://www.cnblogs.com/itech/archive/2011/11/23/2260009.html 在网上貌似没有找到Jenkins的中文的太多的文档,有 ...
- mysql innodb 引擎
innodb 引擎 一.概述 InnoDB 是一个用的比较广泛的存储引擎,因为它支持事物和外键,还有不错的效率;我们先看看官方教程怎么说; 我们先读一下, 对于上面的文档, 对一个InnoDB的表首先 ...
- Codeforces Round #347 (Div. 2) C. International Olympiad 找规律
题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...
- VIM配置自动提示功能
问题描述: 使用VIM作为Linux下的IDE,但是VIM默认情况下不支持自动代码提示功能,因此希望安装插件实现自动提示功能,目前找到的自动提示工具,非常好用 ...