java将文件打包成ZIP压缩文件的工具类实例
- package com.lanp;
- 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.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- /**
- * 将文件打包成ZIP压缩文件
- * @author LanP
- * @since 2012-3-1 15:47
- */
- public final class FileToZip {
- private FileToZip() {
- }
- /**
- * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的ZIP文件,并存放到zipFilePath。
- * @param sourceFilePath 待压缩的文件路径
- * @param zipFilePath 压缩后存放路径
- * @param fileName 压缩后文件的名称
- * @return flag
- */
- public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName) {
- boolean flag = false;
- File sourceFile = new File(sourceFilePath);
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- FileOutputStream fos = null;
- ZipOutputStream zos = null;
- if(sourceFile.exists() == false) {
- System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 不存在. <<<<<<");
- } else {
- try {
- File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
- if(zipFile.exists()) {
- System.out.println(">>>>>> " + zipFilePath + " 目录下存在名字为:" + fileName + ".zip" + " 打包文件. <<<<<<");
- } else {
- File[] sourceFiles = sourceFile.listFiles();
- if(null == sourceFiles || sourceFiles.length < 1) {
- System.out.println(">>>>>> 待压缩的文件目录:" + 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();
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- }
- }
- }
- return flag;
- }
- /**
- * 将文件打包成ZIP压缩文件,main方法测试
- * @param args
- */
- public static void main(String[] args) {
- String sourceFilePath = "C:\\home\\lp20120301";
- String zipFilePath = "C:\\home";
- String fileName = "lp20120301";
- boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
- if(flag) {
- System.out.println(">>>>>> 文件打包成功. <<<<<<");
- } else {
- System.out.println(">>>>>> 文件打包失败. <<<<<<");
- }
- }
- }
java将文件打包成ZIP压缩文件的工具类实例的更多相关文章
- java批量下载,将多文件打包成zip格式下载
现在的需求的: 根据产品族.产品类型,下载该产品族.产品类型下面的pic包: pic包是zip压缩文件: t_product表: 这些包以blob形式存在另一张表中: t_imagefile表: 现在 ...
- 关于springmvc下服务器文件打包成zip格式下载功能
关于springmvc下服务器文件打包成zip格式下载功能 2016年09月21日 11:22:14 toxic_guantou 阅读数:5731更多 个人分类: 技术点存储 版权声明:本文为博主 ...
- JAVA从服务器下载文件根据Url把多文件打包成ZIP下载
注意: 1. String filename = new String(“xx.zip”.getBytes(“UTF-8”), “ISO8859-1”);包装zip文件名不发生乱码. 2.一定要注意 ...
- java批量将多文件打包成zip格式
public void createzip(){ List<File> nFileList = new ArrayList<File>(); nFileList.add(new ...
- pyinstall python文件打包成二进制exe文件
pycharm + python3 + win7 1 pip install pyinstall (官网) 2 准备 .py 文件 3 具体例子 from PyQt5.QtWidgets impor ...
- 如何将编译后的文件打包成jar文件
如果需要修改像spring和dubbo中的jar包源码,修改后怎么打包呢? 如下: 1.win+r进入命令行: 2.找到需要打包的class文件: 3.jar -cvf [jar包的名字] [需要打包 ...
- 将Java应用程序打包成可执行的Jar包
可以将多个class文件打包为jar包,在指定程序入口点情况下,可以用 java –jar jar包名称 的方式调用jar包内主类的main函数. 程序源代码如下: //Math.java publi ...
- 把java文件打包成.jar (jar命令详解)
把java文件打包成.jar (jar命令详解) 先打开命令提示符(win2000或在运行框里执行cmd命令,win98为DOS提示符),输入jar Chelp,然后回车(如果你盘上已经有了jdk1. ...
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
1.背景: 学习java时,教材中关于如题问题,只有一小节说明,而且要自己写麻烦的配置文件,最终结果却只能转换为jar文件.实在是心有不爽.此篇博客教你如何方便快捷地把java代码,打包成jar文件以 ...
随机推荐
- java CountDownLatch的使用
CountDownLatch能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行.使用一个计数器进行实现.计数器初始值为线程的数量.当每一个线程完成自己任务后,计数器的值就会减一.当计数器的值 ...
- not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AV ...
- ZYNQ. Interrupt(2)SPI.AXI TIMER
Shared Peripheral Interrupts (SPI) SPI 可以接收来自PL的中断,这里使用PL模块 AXI Timer 的中断模式,并连接到CPU. AXI TIMER 定时器,内 ...
- networkManger介绍
http://www.linuxidc.com/Linux/2013-08/88809.htm
- Oracle SQL部分练习题
SQL练习题 注:查询列表不建议用 “*” 1.列出至少有一个雇员的所有部门: a. select * from dept where deptno in(select distinct ...
- php里获取第一个中文首字母并排序
需求里结算首页需要按门店的首字母A-Z排序.我的数据结构原本是这样的: Array ( [0] => Array ( [sid] => 2885842 [recetcstoredpay] ...
- 兼容IE FF 获取鼠标位置
由于Firefox和IE等浏览器之间对js解释的方式不一样,firefox下面获取鼠标位置不能够直接使用clientX来获取.网上说的一般都是触发mousemove事件才行.我这里有两段代码,思路都一 ...
- Luogu P1318 积水面积
题目描述 一组正整数,分别表示由正方体迭起的柱子的高度.若某高度值为x,表示由x个正立方的方块迭起(如下图,0<=x<=5000).找出所有可能积水的地方(图中蓝色部分),统计它们可能积水 ...
- linux下如何查看命令的绝对路径
在linux上经常使用ls,grep,vi等命令,如何查看这些命令的绝对路径呢? 通过whereis/which 就可以啦,但是这两个命令之间还是有一些区别.网上查了一下资料,解释如下: which ...
- 原生js返回顶部
let backToTop = function() { let scrollToptimer = setInterval(function() { let top = document.body.s ...