功能实现.

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文件压缩和解压的更多相关文章

  1. java 文件压缩和解压(ZipInputStream, ZipOutputStream)

    最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...

  2. linux常用命令:4文件压缩和解压命令

    文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...

  3. Ionic.Zip.dll文件压缩和解压

    Ionic.Zip.dll文件压缩和解压 下载地址: http://download.csdn.net/detail/yfz19890410/5578515 1.下载Ionic.Zip.dll组件,添 ...

  4. c#自带压缩类实现的多文件压缩和解压

    用c#自带的System.IO.Compression命名空间下的压缩类实现的多文件压缩和解压功能,缺点是多文件压缩包的解压只能调用自身的解压方法,和现有的压缩软件不兼容.下面的代码没有把多文件的目录 ...

  5. .net文件压缩和解压及中文文件夹名称乱码问题

    /**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...

  6. 文件压缩和解压 FileStream GZipStream

    using (FileStream reader=new FileStream (@"c:\1.txt",FileMode.Open,FileAccess.Read)) { usi ...

  7. C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用

    工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...

  8. python学习shutil模块的文件压缩和解压用法

    shutil模块可以创建压缩包并返回文件路径,例如 zip,tar,下面详细其用法 base_name 压缩包的文件名,也可以是压缩包的路径,只是文件名时,则保存至当前目录,否则保存指定路径 data ...

  9. ZipArchive框架的文件压缩和解压

    导入第三方框架ZipArchive之后还要在系统库文件中导入一个如下文件(搜索libz出来的任何一个都可以)   导入的头文件是#import "Main.h" 文件压缩 -(vo ...

随机推荐

  1. Bitmap动画

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html htt ...

  2. ecshop 团购-》调取评论

    涉及到的文件及代码:lib_insert.php,comments.lbi,{insert name='comments' type=$type id=$id} html代码: <blockqu ...

  3. Tomcat 系统架构与设计模式

    Tomcat 系统架构与设计模式,第 1 部分: 工作原理 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomc ...

  4. Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布

       FastCGI编程包括四部分:初始化编码.接收请求循环.响应内容.响应结束循环. FCGX_Request request; FCGX_Init(); ); FCGX_InitRequest(& ...

  5. mysql的Replication机制

    mysql的Replication机制 参考文档:http://www.doc88.com/p-186638485596.html Mysql的 Replication 是一个异步的复制过程. 从上图 ...

  6. java正则表达式 --简单认识

    学习目标 正则表达式的作用正则表达式的模式匹配Pattern类和Matcher类的使用掌握String对正则的支持具体内容一.认识正则(为什么要有正则) 方便的对数据进行匹配 执行复杂的字符串验证.拆 ...

  7. Java并发编程核心方法与框架-Executors的使用

    合理利用线程池能够带来三个好处 降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗. 提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行. 提高线程的可管理性.线程是稀 ...

  8. firefox 提示 setTimeout():useless setTimeout call (missing quotes around argument?) 错误

    原来代码: setTimeout(window.parent.refreshNode(id),500);// 500毫秒后,调用父窗口的refreshNode()方法 refreshNode()方法总 ...

  9. JS中document对象和window对象有什么区别

    简单来说,document是window的一个对象属性.Window 对象表示浏览器中打开的窗口.如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 windo ...

  10. angularjs 过滤器详解

    https://segmentfault.com/a/1190000002758481     app.controller('testC',function($scope,$filter){ $sc ...