功能实现.

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. Eclipse中修改Web项目的URL访问路径

    背景 访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp  这里的xxxx,也就是request.getContext ...

  2. linux 开机启动过程详解

    Linux开机执行内核后会启动init进程,该进程根据runlevel(如x)执行/etc/rcx.d/下的程序,其下的程序是符号链接,真正的程序放在/etc/init.d/下.开机启动的程序(服务等 ...

  3. bootloader (run in CLI or Qt-GUI mode)

    1.PC端 https://github.com/cidadao/efm32_loader http://theramblingness.com/2015/07/16/a-gui-and-cli-ut ...

  4. HTTP错误404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求

    http://www.cnblogs.com/JKqingxinfeng/archive/2012/10/29/2744663.html HTTP错误404.13 - Not Found 请求筛选模块 ...

  5. 【MVC5】画面多按钮提交

    画面上有个多个按钮时,如何绑定到各自的Action上? 1.追加如下MultipleButtonAttribute类 1 using System; 2 using System.Reflection ...

  6. adobe photoshop cc 2014 安装失败 解决办法之一

    首先安装失败会有提示 首先贴下错误信息 Exit Code: 34 Please see specific errors below for troubleshooting. For example, ...

  7. php如何发起POST DELETE GET POST 请求

    关于POST,DELETE,GET,POST请求 get:是用来取得数据.其要传递过的信息是拼在url后面,因为其功能使然,有长度的限制 post:是用来上传数据.要上传的数据放在request的he ...

  8. SpringMVC框架的工作原理

    学习SpringMVC的工作原理,首先有三个要解决的问题: (1)DispathcherServlet框架如何截获特定的HTTP请求,交由SpringMVC处理? (2)位于Web层的Spring容器 ...

  9. iOS- storyboard this class is not key value coding-compliant for the key xxx

    如图: 在使用storyboard的时候出现此问题,主要是因为给storybroad中的view拖线的时候,有时不小心线拖错了,或者再次拖线导致代码中控件的名字与之前拖线时定义的名字不同导致的. 解决 ...

  10. Devexpress-1 DataGrid控件

    参考资料: 使用XtraGrid自定义列计算 DEV GridControl小结 实现对两列求和后作为新的列