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文件以 ...
随机推荐
- SpringSecurity csrf验证忽略某些请求
前几天项目中遇到springSecurity问题,研究了大半天,掉进了csrf的坑,先认识一下csrf CSRF概念:CSRF跨站点请求伪造(Cross—Site Request Forgery),跟 ...
- 大话C#中能使用foreach的集合的实现
大家都知道foreach的语法:foreach(var item in items){ Console.Writeln(item);} 通过这样一个简单的语句,就能实现遍历集合items中的所有元素. ...
- 第5月第27天 cocos2d
1. 流程是这样的: 在CCApplication的run函数中,显示设备链调用相应的场景显示函数drawScene来绘制场景,然后调用了CCScheduler的update函数,在这个函数里,对所有 ...
- 第8月第22天 python scrapy
1. cd /Users/temp/Downloads/LagouSpider-master ls ls ls lagou/settings.py cat lagou/settings.py ls p ...
- 【干货】使用EnCase来分析windows 7文件系统------认识元数据记录$MFT,数据恢复
来源:Unit 6: Windows File Systems and Registry 6.1 Windows File Systems and Registry Windows NTFS File ...
- flask基础之LocalProxy代理对象(八)
前言 flask框架自带的代理对象有四个,分别是request,session,g和current_app,各自的含义我们在前面已经详细分析过.使用代理而不是显式的对象的主要目的在于这四个对象使用太过 ...
- OE中的bitbake使用
OpenEmbedded是一些脚本(shell和python脚本)和数据构成的自动构建系统. 脚本实现构建过程,包括下载(fetch).解包(unpack).打补丁(patch).config ...
- SpringMVC_HelloWorld_01
通过配置文件的方式实现一个简单的HelloWorld. 源码 一.新建项目 1.新建动态web项目 2.命名工程springmvc-01 3.勾选"Generate web.xml depl ...
- JAVA学习(二) String使用equals方法和==分别比较的是什么?(转)
String使用的equals方法和==的区别 equals方法和==的区别 首先大家知道,String既可以作为一个对象来使用,又可以作为一个基本类型来使用.这里指的作为一个基本类型来使用只是指 ...
- 常用的 Windows 键
常用的 Windows 键 Windows 键 + 空格键 功能:透明化所有窗口,快速查看桌面.使用此快捷键可以立即将目前所有打开的窗口透明化,以便查看桌面. Windows 键 + 字母键“D” ...