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压缩类,欢迎吐槽的更多相关文章

  1. php ZIP压缩类实例分享

    php ZIP压缩类实例分享 <?php $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt ...

  2. C#zip压缩类

    改造了网上的代码,可以压缩文件夹.指定文件列表(文件和文件夹的混合列表,需要指定子文件夹下需要压缩的文件),注释很详细不做解释 public class ZipHelper { /// <sum ...

  3. 最近工作用到压缩,写一个zip压缩工具类

    package test; import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream ...

  4. zip压缩类

    using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...

  5. 【C#】依赖于SharpZipLib的Zip压缩工具类

    上班第二天下班,课外作业,实现一个ZIP压缩的工具类.本来想用Package,但是写完了才发现不能解压其他工具压缩的zip包,比较麻烦,因此本工具类依赖了第三方的库(SharpZipLib  vers ...

  6. C#实现Zip压缩解压实例

    原文地址:https://www.cnblogs.com/GoCircle/p/6544678.html 本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZi ...

  7. C# ZIP 压缩解压

    ZIP流是在NetFramework4.5 引入的目的是为了能够更好的操作ZIP文件,进行压缩解压等操作.与ZIP流相关的几个类是: ZipArchive 代表一个ZIP的压缩包文件 ZipArchi ...

  8. C# - ZIP 压缩流

    C# - ZIP 压缩流 参考资料 https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchive?view= ...

  9. zip压缩详细分析

    该文章转自:http://www.cnblogs.com/esingchan/p/3958962.html (文章写得很详细,让我对zip压缩有了了解,感谢博主,贴在这是为了防止忘了有这么好的文章,侵 ...

随机推荐

  1. Oracle 的证也会过期咯

    How does this recertification requirement affect me? If your Database Certification credential is re ...

  2. 【转】微软MVP攻略 (如何成为MVP?一个SQL Server MVP的经验之谈)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 初衷 什么是微软MVP? 成为微软MVP的条件? 如何成为微软MVP? (一) 申请时间划分 (二) 前期准备 (三) ...

  3. Java中resourceBundle和Properties的区别

    第一种办法InputStream is = Test.class.getResourceAsStream("DbConfig.properties");Properties p = ...

  4. [ios]ipad下的splitViewController 让你的APP看起来酷酷的!

    在ipad下可以使用splitViewController splitViewController下包含两个viewController 这是一种将屏幕一分为二的方式. 在水平状态下会出现成两个左右两 ...

  5. 0x04 高级语法

    while-endw .while(条件) 循环体(条件满足时执行) .endw repeat-until .repeat 循环体(条件不满足时执行) .until(条件) if-elseif-end ...

  6. rJava配置

    1. 下载安装R-3.1.1-win.exe: 2. 在R中安装rJava > install.packages("rJava") 3. 设置环境变量: PATH:D:\So ...

  7. 数据结构(复习)---------字符串-----KMP算法(转载)

    字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...

  8. 1458: 士兵占领 - BZOJ

    Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放 ...

  9. vim插件介绍

    代码补全 http://blog.sina.com.cn/s/blog_a6559d920101acv3.html这个牛逼.************************************** ...

  10. IntelliJ IDEA的Maven项目在修改时报java.lang.OutOfMemoryError: PermGen space异常

    什么也不说了---内存溢出,遇见太多回了,下面是解决方式: 1.在项目设置中新建Maven,然后设置VM: 2. 在pom.xml添加下面2个插件,一个是jrebel的,一个是jetty的 <b ...