Java—将文件压缩为zip文件
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 hwt
*
*/
public class FileToZip { private FileToZip(){} /**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
* @param sourceFilePath :待压缩的文件路径
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static String fileToZip(String sourceFilePath,String zipFilePath,String fileName){
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
//判断源文件是否存在
if(!sourceFile.exists()){
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);
}
}
}
}
} 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 fileName + ".zip";
} public static void main(String[] args){
String sourceFilePath = "D:\\source";
String zipFilePath = "D:\\targetzip";
String fileName = "test";
String fn = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
System.out.println(fn);
} }
Java—将文件压缩为zip文件的更多相关文章
- java实现将文件压缩成zip格式
以下是将文件压缩成zip格式的工具类(复制后可以直接使用): zip4j.jar包下载地址:http://www.lingala.net/zip4j/download.php package util ...
- Java使用基本JDK操作ZIP文件以及zip文件的加密、解密等功能
Java使用基本JDK操作ZIP文件 http://blog.csdn.net/zhyh1986/article/details/7723649 Java解压和压缩带密码的zip文件 http://b ...
- vue-webpack项目自动打包压缩成zip文件批处理
为什么需要这个? 使用vue框架开发项目,npm run build这个命令会一直用到,如果需要给后端发包,那你还要打包成zip格式的压缩包,特别是项目提测的时候,一天可能要执行重复好几次,所以才有了 ...
- java 实现Excel压缩成Zip导出
1 概述 在web项目中常见的一种场景就是将文件导出为Excel,但是当需要导出多个Excel时,使用者将频繁操作,这样就严重降低了项目的友好交互性以及易用性,那么怎么才能优雅的解决这个问题呢?笔者今 ...
- php读取excel,以及php打包文件夹为zip文件
1.把文件下载到本地,放在在Apache环境下2.d.xlsx是某游戏的服务器名和玩家列表,本程序只适合此种xlsx文件结构,其他结构请修改index.php源码3.访问zip.php的功能是把生成的 ...
- Java 批量下载excel,并对excel赋值,压缩为zip文件(POI版)
package com.neusoft.nda.servlet; import java.io.File;import java.io.FileInputStream;import java.io.F ...
- Java—将文件夹压缩为zip文件
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...
- Java 多文件压缩成一个文件工具类
简单修改来自博客园勇闯天涯zfc的博客 一.内容 ①使用 Java 将多个文件打包压缩成一个压缩文件: ②主要使用 java.io 下的类 二.源代码:ZIPUtil .java import jav ...
- 批量下载,多文件压缩打包zip下载
0.写在前面的话 图片批量下载,要求下载时集成为一个压缩包进行下载.从昨天下午折腾到现在,踩坑踩得莫名其妙,还是来唠唠,给自己留个印象的同时,也希望给需要用到这个方法的人带来一些帮助. 1.先叨叨IO ...
随机推荐
- HDU 1114 完全背包问题的转化
题目大意: 根据存钱罐中钱的重量,和每一种钱对应的重量和价值,判断钱能否塞满这个重量,如果能,输出得到的最小价值 这个问题就是要把它和背包问题连接起来,这里钱取得数目是无穷的,所以这里只需要用到完全背 ...
- hdu 5037 模拟网选1006
/* 模拟 实例: 33 1 10 5 5 2 10 3 3 6 1 3 2 1 1 4 2 1 1 5 2 1 1 6 2 1 1 7 2 1 5 20 8 1 2 3 4 5 1 20 8 5 0 ...
- 1874 Bellman-ford算法 队列优化过的 用于稀疏图,有负权的图
#include<stdio.h> #include<algorithm> #include<iostream> #include<queue> usi ...
- MyBatis启动:MapperStatement创建
参考:http://blog.csdn.net/ashan_li/article/details/50351080 MappedStatement说明 一个MappedStatement对象对应Map ...
- 对象和变量的并发访问synchronized解析以及死锁分析排查
一.synchronized java并发编程中存在“非线程安全"问题.“非线程安全"是指发生在多个线程对同一个对象中的实例变量并发访问时,产生的”脏读“现象,使用synchron ...
- NOIP2011 提高组合集
NOIP 2011 提高组合集 D1 T1 铺地毯 模拟,题目让你干啥你就干啥 #include <iostream> #include <cstdio> using name ...
- 4560 NOIP2015 D2T2 子串 code vs
4560 NOIP2015 D2T2 子串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有两个仅包含小写 ...
- Ubuntu 16.04使用NASM编译时用ld链接程序出现:i386 架构于输入文件 sandbox.o 与 i386:x86-64 输出不兼容(I386 architecture in the input file sandbox.o is not compatible with i386: x86-64 output)
错误: 问题解决过程: 1.先确定CPU的架构 2.这是以64位架构的CPU,如果使用elf参数时,默认是以32位模式去处理,那么此时需要更精确的去指定这个模式,比如elf32(32位),elf64( ...
- C语言的数组初始化
http://blog.csdn.net/sibylle/article/details/2026915 一直以为 int a[256]={0};是把a的所有元素初始化为0,int a[256]={1 ...
- git bash here真牛!
git bash here真牛! 在Windows上面安装了git,在文件夹里面空白处右键点击,选择git bash here: 随手敲了几个命令:ls,ls -a,which ls,who, fin ...