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 ...
随机推荐
- Quagga服务器安装和配置
使用本地源 一.安装软件包 # yum install quagga-0.99.15-7.el6_3.2.x86_64.rpm 或rpm # ls /etc/quagga/ bgpd.conf.s ...
- MongoVUE
MongoVUE运行界面如下:
- StringUtils
StringUtils.hasText(字符串) 如果字符串里面的值为null, "", " ",那么返回值为false:否则为true
- Idea修改js和jsp不用重启
- 在IE6、IE7中实现块元素的inline-block效果
在IE6.IE7中实现块元素的inline-block效果有以下两种方法: 1先使用display:inline-block属性触发layout,然后再定义display:inline让块元素呈现内联 ...
- 【转】Kafka实战-Flume到Kafka
Kafka实战-Flume到Kafka Kafka 2015-07-03 08:46:24 发布 您的评价: 0.0 收藏 2收藏 1.概述 前面给大家介绍了整个Kafka ...
- visio2007使用记录
安装时, 需要有选择性的安装, 不是所有的组件 都需要安装, 如office工具, office共享功能, visio中的.net可编程支持就用不着安装.... visio中的cad(加载项), 可以 ...
- php中关于 left join 的分页显示
/* * 统计商机搜索总条数信息 * ftt * 2015-11-10 10:13:15 */ public function getBusinessCount($btype='',$buname=' ...
- CF467 AB 水题
Codeforces Round #267 (Div. 2) (C和D的题解单独写:CF467C George and Job (DP) CF467D Fedor and Essay 建图DFS) C ...
- jQuery 学习之路(1):引子
一.主流 javascript 库 除 jQuery 外,还有 Prototype.Dojo.YUI.ExtJS.MooTools ,其中 Prototype 较老,结构设计较为松散,ExtJS 界面 ...