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. Hadoop分布式安装

    一.安装准备         1.下载hadoop,地址:http://hadoop.apache.org/,下载相应版本         2.下载JDK版本:Hadoop只支持1.6以上,地址:ht ...

  2. java_Thread生产者与消费者 Demo

    package com.bjsxt.Thread.Demo; public class ProducerConsumer { /** * 生产者与消费者 * @param args */ public ...

  3. Java并发编程:Lock(上)

    在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.locks包下提供了另外一种方 ...

  4. cocos2dx中的三种基本的数据类型

    cocos2dx中提供了三种基本的数据类型:CCString(字符串),CCArray(数组),CCDictionary(数据字典(哈希的功能)) 2.CCString的用法 class  CCStr ...

  5. android 开发上传图片遇到返回 FileNotFoundException

    目的:带有参数上传图片 环境:客户端开发 android studio  ,服务器端:visual studio 2012 (c# asp.net) ************************* ...

  6. C#笔记2:重构

    转: 最常用的重构指导 参考:http://www.cnblogs.com/KnightsWarrior/archive/2010/06/30/1767981.html,本文示例代码多来自此处: 参考 ...

  7. Xcode Provisioning 路径

    ~/Library/MobileDevice/Provisioning Profiles

  8. 【HDOJ】【1693】Eat The Trees

    插头DP 插头dp模板题…… 这题比CDQ论文上的例题还要简单……因为不用区分左右插头(这题可以多回路,并不是一条哈密尔顿路) 硬枚举当前位置的状态就好了>_< 题解:http://blo ...

  9. Finite State Machine

    Contents [hide]  1 Description 2 Components 3 C# - FSMSystem.cs 4 Example Description This is a Dete ...

  10. POJ 1804

    /* *由此题发现规律 就是冒泡排序的交换次数等于这个数列的逆序对 的对数! */ #include<iostream> #include<stdio.h> #include& ...