java 解压,压缩单个文件
2016-01-11 13:45
381 查看
// 压缩单个文件
public static void doZip(String srcpath, String zipName) throws IOException {
// 声明一个要压缩的文件
File srcfile = new File(srcpath);
if (!srcfile.exists()) {
System.err.println(“要压缩的文件不存在!”);
System.exit(1);
}
//判断父路径是否存在
File zfile=new File(zipName);
if (!zfile.getParentFile().exists()) {
zfile.getParentFile().mkdirs();//如果不存在创建父路径
}
// 封装要压缩的文件
InputStream in = new FileInputStream(srcfile);
// 封装压缩后的路径
FileOutputStream fos = new FileOutputStream(zipName); //
ZipOutputStream zout = new ZipOutputStream(fos);
// 设置一个缓冲数组
byte[] by = new byte[1024];
int len = 0;
//(默认-1) 设置压缩等级(0-9),9最高压缩比,比GZIP压缩比更大,但是速度比它慢;
//经过粗略测试,zip的7等级跟GZIP压缩比类似;
zout.setLevel(7);
// 创建ZIP条目。传入文件名(带尾缀)
ZipEntry zipEntry = new ZipEntry(srcfile.getName());
// 开始写入新的 ZIP文件条目并将流定位到条目数据的开始处
zout.putNextEntry(zipEntry);
// 每次读取1kb
while ((len = in.read(by)) != -1) {
zout.write(by, 0, len);
}
zout.closeEntry();
zout.flush();
zout.close();
in.close();
System.out.println(“压缩成功!”);
}
// 解压单个文件
public static void UnZip(String zipName) throws IOException{
File zipfile = new File(zipName);
if (!zipfile.exists()) {
System.err.println(“要解压的文件不存在!”);
System.exit(1);
}
FileInputStream fis=new FileInputStream(zipfile);
//封装待解压文件
ZipInputStream zis=new ZipInputStream(fis);
ZipEntry zip=zis.getNextEntry();
//获取待解压文件的父路径
String parent=zipfile.getParent();
//封装解压后的路径
FileOutputStream fos=new
FileOutputStream(parent+”/”+zip.getName());
byte by[]=new byte[1024];
int len;
while((len=zis.read(by))!=-1){
fos.write(by, 0, len);
}
fos.flush();
fos.close();
zis.closeEntry();
zis.close();
System.out.println(“解压成功!”);
}
public static void doZip(String srcpath, String zipName) throws IOException {
// 声明一个要压缩的文件
File srcfile = new File(srcpath);
if (!srcfile.exists()) {
System.err.println(“要压缩的文件不存在!”);
System.exit(1);
}
//判断父路径是否存在
File zfile=new File(zipName);
if (!zfile.getParentFile().exists()) {
zfile.getParentFile().mkdirs();//如果不存在创建父路径
}
// 封装要压缩的文件
InputStream in = new FileInputStream(srcfile);
// 封装压缩后的路径
FileOutputStream fos = new FileOutputStream(zipName); //
ZipOutputStream zout = new ZipOutputStream(fos);
// 设置一个缓冲数组
byte[] by = new byte[1024];
int len = 0;
//(默认-1) 设置压缩等级(0-9),9最高压缩比,比GZIP压缩比更大,但是速度比它慢;
//经过粗略测试,zip的7等级跟GZIP压缩比类似;
zout.setLevel(7);
// 创建ZIP条目。传入文件名(带尾缀)
ZipEntry zipEntry = new ZipEntry(srcfile.getName());
// 开始写入新的 ZIP文件条目并将流定位到条目数据的开始处
zout.putNextEntry(zipEntry);
// 每次读取1kb
while ((len = in.read(by)) != -1) {
zout.write(by, 0, len);
}
zout.closeEntry();
zout.flush();
zout.close();
in.close();
System.out.println(“压缩成功!”);
}
// 解压单个文件
public static void UnZip(String zipName) throws IOException{
File zipfile = new File(zipName);
if (!zipfile.exists()) {
System.err.println(“要解压的文件不存在!”);
System.exit(1);
}
FileInputStream fis=new FileInputStream(zipfile);
//封装待解压文件
ZipInputStream zis=new ZipInputStream(fis);
ZipEntry zip=zis.getNextEntry();
//获取待解压文件的父路径
String parent=zipfile.getParent();
//封装解压后的路径
FileOutputStream fos=new
FileOutputStream(parent+”/”+zip.getName());
byte by[]=new byte[1024];
int len;
while((len=zis.read(by))!=-1){
fos.write(by, 0, len);
}
fos.flush();
fos.close();
zis.closeEntry();
zis.close();
System.out.println(“解压成功!”);
}
相关文章推荐
- Eclipse RCP 插件开发中经常调用的几个方法
- Myeclipse搭建struts2环境
- Java之工厂方法
- Java创建和解析Json数据方法(三)——json-lib包的使用
- 在Java中,break还可以这样用
- 【JVM系列】Java class文件解析2
- Spring Boot:在Eclipse/STS设置热插拔免重启
- java 正则(2) 空行匹配
- intellij idea Error:java: Compilation failed: internal java compiler error
- 深入理解Java虚拟机 类加载子系统2
- cpu缓存java性能问题初探
- eclipse+webservice开发实例
- eclipse中导入svn的三种方式
- Java-命令行编译带包和带入第三方jar的java程序
- spring的controller中使用aop
- Eclipse自动补全+常用快捷键
- MyBatis+Spring+Spring MVC整合开发
- 解析java序列化机制
- 仲恺acm 1019:石头剪子布【java】
- Java聊天室搭建