java文件压缩和解压
功能实现.
package com.test; import java.io.File;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; /**
* 该类实现文件夹压缩成zip文件和zip文件解压
*
* @author Administrator
*
*/
public class Zip {
private ZipInputStream zipIn; // 解压Zip
private ZipOutputStream zipOut; // 压缩Zip
private ZipEntry zipEntry;
private static int bufSize; // size of bytes
private byte[] buf;
private int readedBytes; public Zip() {
this(512);
} public Zip(int bufSize) {
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
} // 压缩文件夹内的文件
public void doZip(String zipDirectory) {// zipDirectoryPath:需要压缩的文件夹名
File file;
File zipDir; zipDir = new File(zipDirectory);
String zipFileName = zipDirectory + ".zip";// 压缩后生成的zip文件名 try {
this.zipOut = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(zipFileName)));
handleDir(zipDir, this.zipOut);
this.zipOut.close();
} catch (Exception ioe) {
ioe.printStackTrace();
}
} // 由doZip调用,递归完成目录文件读取
private void handleDir(File dir, ZipOutputStream zipOut) throws Exception {
FileInputStream fileIn;
File[] files; files = dir.listFiles();
if (files.length == 0) {// 如果目录为空,则单独创建之.
// ZipEntry的isDirectory()方法中,目录以"/"结尾.
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
} else {// 如果目录不为空,则分别处理目录和文件.
for (File fileName : files) { if (fileName.isDirectory()) {
handleDir(fileName, this.zipOut);
} else {
fileIn = new FileInputStream(fileName);
String name = dir.getName();
// 生成的压缩包存放在原目录下
this.zipOut.putNextEntry(new ZipEntry(name + "/"
+ fileName.getName().toString())); // 此方法存放在该项目目录下
// this.zipOut.putNextEntry(new
// ZipEntry(fileName.toString()));
while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
this.zipOut.write(this.buf, 0, this.readedBytes);
}
this.zipOut.closeEntry();
}
}
}
} // 解压指定zip文件
public void unZip(String unZipfileName) {// unZipfileName需要解压的zip文件名
FileOutputStream fileOut;
File file;
String f = unZipfileName.substring(0, unZipfileName.length() - 4);
File ff = new File(f);
try {
this.zipIn = new ZipInputStream(new BufferedInputStream(
new FileInputStream(unZipfileName)));
while ((this.zipEntry = this.zipIn.getNextEntry()) != null) {
file = new File(this.zipEntry.getName());
if (this.zipEntry.isDirectory()) {
file.mkdirs();
} else {
// 如果指定文件的目录不存在,则创建之.
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (!ff.exists()) {
ff.mkdir();
}
fileOut = new FileOutputStream(f + "/" + file.getName()); // fileOut = new FileOutputStream(file); 此方法存放到该项目目录下
while ((this.readedBytes = this.zipIn.read(this.buf)) > 0) {
fileOut.write(this.buf, 0, this.readedBytes);
}
fileOut.close();
} this.zipIn.closeEntry();
}
} catch (Exception ioe) {
ioe.printStackTrace();
}
} // 设置缓冲区大小
public void setBufSize(int bufSize) {
this.bufSize = bufSize;
} // 测试Zip类
public static void main(String[] args) throws Exception {
Zip zip = new Zip();
zip.doZip("e:\\test");
// zip.unZip("c:\\test.zip");
}
}
java文件压缩和解压的更多相关文章
- java 文件压缩和解压(ZipInputStream, ZipOutputStream)
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...
- linux常用命令:4文件压缩和解压命令
文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...
- Ionic.Zip.dll文件压缩和解压
Ionic.Zip.dll文件压缩和解压 下载地址: http://download.csdn.net/detail/yfz19890410/5578515 1.下载Ionic.Zip.dll组件,添 ...
- c#自带压缩类实现的多文件压缩和解压
用c#自带的System.IO.Compression命名空间下的压缩类实现的多文件压缩和解压功能,缺点是多文件压缩包的解压只能调用自身的解压方法,和现有的压缩软件不兼容.下面的代码没有把多文件的目录 ...
- .net文件压缩和解压及中文文件夹名称乱码问题
/**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...
- 文件压缩和解压 FileStream GZipStream
using (FileStream reader=new FileStream (@"c:\1.txt",FileMode.Open,FileAccess.Read)) { usi ...
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...
- python学习shutil模块的文件压缩和解压用法
shutil模块可以创建压缩包并返回文件路径,例如 zip,tar,下面详细其用法 base_name 压缩包的文件名,也可以是压缩包的路径,只是文件名时,则保存至当前目录,否则保存指定路径 data ...
- ZipArchive框架的文件压缩和解压
导入第三方框架ZipArchive之后还要在系统库文件中导入一个如下文件(搜索libz出来的任何一个都可以) 导入的头文件是#import "Main.h" 文件压缩 -(vo ...
随机推荐
- SQL 使用小记
1. case语句 示例 select id, name, case user_role then "管理员" then "未注册用户" then " ...
- ecshop商品-》获取促销商品
lib_goods.php->function get_promote_goods(){} /** * 获得促销商品 * * @access public * @return array */ ...
- Nancy总结(二)记一次Nancy 框架中遇到的坑
记一次Nancy 框架中遇到的坑 前几天,公司一个项目运行很久的Nancy框架的网站,遇到了一个很诡异的问题.Session 对象跳转到另外一个页面的时候,session对象被清空了,导致用户登录不上 ...
- bash: ifconfig: command not found解决方法
1.问题: #ifconfig bash: ifconfig: command not found 2.原因:非root用户的path中没有/sbin/ifconfig ,其它的命令也可以出现这种情况 ...
- .net mvc web api 返回 json 内容,过滤值为null的属性
原文:http://blog.csdn.net/xxj_jing/article/details/49508557 版权声明:本文为博主原创文章,未经博主允许不得转载. .net mvc web ap ...
- 为什么要做url encode
因为 url 对字符有限制,比如把一个邮箱放入 url,就需要使用 urlencode 函数,因为 url 中不能包含 @ 字符.
- maven pom.xml加载不同properties配置[转]
可以参考http://www.openwebx.org/docs/autoconfig.html 1.pom.xml =========================== <!-- 不同的打包 ...
- iOS-马上着手开发iOS应用应用程序-第二部分构建应用程序
第二部分构建应用程序 1,应用程序开发过程 2,设计用户界面 3,定义交互 4,教程:串联图 1,应用程序开发过程 定义概念 设计用户界面 定义交互 实现行为整合数据 对象是应用程序的基石 类是对象的 ...
- reactjs 注意点
render的return return前要留一空行 return的括号要分别各占一行,不能与html同行 return中的html必须要有顶层容器包裹 return中的循环不能用for,改用map方 ...
- 徐汉彬:Web系统大规模并发——电商秒杀与抢购(转)
[导读]徐汉彬曾在阿里巴巴和腾讯从事4年多的技术研发工作,负责过日请求量过亿的Web系统升级与重构,目前在小满科技创业,从事SaaS服务技术建设. 电商的秒杀和抢购,对我们来说,都不是一个陌生的东西. ...