java 多文件压缩成一个zip压缩包
2015-10-23 14:41
447 查看
一、涉及到的包都是jdk自带的。(所以不用为包的事发愁了)
二、啥也不说了,把demo献上
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.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static void main(String[] args) {
List<File> files= new ArrayList<File>();
File file=new File("E:\\file\\原来你也在这里.mp3");
files.add(file);
File file1=new File("E:\\file\\dd.zip");
compression(files,file1);
}
public static boolean compression(List<File> sourceFiles,File zipFilePath){
boolean flag=false;
FileOutputStream fos=null;
ZipOutputStream zos=null;
FileInputStream fis=null;
BufferedInputStream bis=null;
try {
if (sourceFiles!=null&&sourceFiles.size()!=0) {
fos=new FileOutputStream(zipFilePath);
zos=new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs=new byte[1024*10];
for (int i = 0; i < sourceFiles.size(); i++) {
ZipEntry zipEntity=new ZipEntry(sourceFiles.get(i).getName());
zos.putNextEntry(zipEntity);
//读取压缩文件并写入压缩包
fis=new FileInputStream(sourceFiles.get(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);
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}finally{
try {
if(zos!=null){
zos.flush();
zos.close();
}
if (fis!=null) {
fis.close();
}
if (bis!=null) {
bis.close();
}
if (fos!=null) {
fos.flush();
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
// TODO: handle exception
}
}
return flag;
}
}流用完一定记得关
二、啥也不说了,把demo献上
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.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static void main(String[] args) {
List<File> files= new ArrayList<File>();
File file=new File("E:\\file\\原来你也在这里.mp3");
files.add(file);
File file1=new File("E:\\file\\dd.zip");
compression(files,file1);
}
public static boolean compression(List<File> sourceFiles,File zipFilePath){
boolean flag=false;
FileOutputStream fos=null;
ZipOutputStream zos=null;
FileInputStream fis=null;
BufferedInputStream bis=null;
try {
if (sourceFiles!=null&&sourceFiles.size()!=0) {
fos=new FileOutputStream(zipFilePath);
zos=new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs=new byte[1024*10];
for (int i = 0; i < sourceFiles.size(); i++) {
ZipEntry zipEntity=new ZipEntry(sourceFiles.get(i).getName());
zos.putNextEntry(zipEntity);
//读取压缩文件并写入压缩包
fis=new FileInputStream(sourceFiles.get(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);
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}finally{
try {
if(zos!=null){
zos.flush();
zos.close();
}
if (fis!=null) {
fis.close();
}
if (bis!=null) {
bis.close();
}
if (fos!=null) {
fos.flush();
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
// TODO: handle exception
}
}
return flag;
}
}流用完一定记得关
相关文章推荐
- C#使用DeflateStream解压缩数据文件的方法
- C#调用WinRar执行rar、zip压缩的方法
- C#实现页面GZip或Deflate压缩的方法
- C#使用iCSharpcode进行文件压缩实现方法
- C#实现压缩HTML代码的方法
- Asp.net在线备份、压缩和修复Access数据库示例代码
- 使用UglifyJS合并/压缩JavaScript的方法
- 高性能WEB开发 JS、CSS的合并、压缩、缓存管理
- 脚本分析、压缩、混淆工具 JSA新版本发布,压缩效率提高大约10%
- 发布一个高效的JavaScript分析、压缩工具 JavaScript Analyser
- PHP实现图片压缩的两则实例
- 在IIS上启用Gzip压缩 (HTTP压缩)第1/3页
- javascript实现的样式表(CSS) 格式整理与压缩
- php zlib压缩和解压缩swf文件的代码
- YUI Compressor压缩JavaScript原理及微优化
- Nginx中的Gzip压缩配置介绍
- python zip文件 压缩
- Tomcat配置gzip压缩提高浏览网站的速度
- C#实现的文件压缩和解压缩类
- CSS代码格式化和压缩的方法与技巧