java 文件压缩和解压(ZipInputStream, ZipOutputStream)
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还可以对文件夹进行压缩和解压。
ZipInputStream位于java.util.zip包下。下面是它的API,比较简单。



ZipOutputStream位于java.util.zip包下。下面是它的API,比较简单。



文件的压缩
public class TestFile
{
public static void main ( String [ ] args ) throws IOException
{
// new a file input stream
FileInputStream fis = new FileInputStream (
"/home/liangruihua/ziptest/1.txt" ) ;
BufferedInputStream bis = new BufferedInputStream ( fis ) ; // new a zipPutputStream
// /home/liangruihua/ziptest/1.zip -- the out put file path and
// name
ZipOutputStream zos = new ZipOutputStream (
new FileOutputStream (
"/home/liangruihua/ziptest/1.zip" ) ) ;
BufferedOutputStream bos = new BufferedOutputStream ( zos ) ; // set the file name in the .zip file
zos.putNextEntry ( new ZipEntry ( "1.txt" ) ) ; // set the declear
zos.setComment ( "by liangruihua test!" ) ; byte [ ] b = new byte [ 100 ] ;
while ( true )
{
int len = bis.read ( b ) ;
if ( len == - 1 )
break ;
bos.write ( b , 0 , len ) ;
}
fis.close ( ) ;
zos.close ( ) ;
}
}
文件夹的压缩
public class TestDir
{
public static void main ( String [ ] args ) throws IOException
{
// the file path need to compress
File file = new File ( "/home/liangruihua/ziptest/test" ) ;
ZipOutputStream zos = new ZipOutputStream (
new FileOutputStream (
"/home/liangruihua/ziptest/test.zip" ) ) ; // judge the file is the directory
if ( file.isDirectory ( ) )
{
// get the every file in the directory
File [ ] files = file.listFiles ( ) ; for ( int i = 0 ; i < files.length ; i ++ )
{
// new the BuuferedInputStream
BufferedInputStream bis = new BufferedInputStream (
new FileInputStream (
files [ i ] ) ) ;
// the file entry ,set the file name in the zip
// file
zos.putNextEntry ( new ZipEntry ( file
.getName ( )
+ file.separator
+ files [ i ].getName ( ) ) ) ;
while ( true )
{
byte [ ] b = new byte [ 100 ] ;
int len = bis.read ( b ) ;
if ( len == - 1 )
break ;
zos.write ( b , 0 , len ) ;
} // close the input stream
bis.close ( ) ;
} }
// close the zip output stream
zos.close ( ) ;
}
}
文件的解压
public class TestZipInputStream
{
public static void main ( String [ ] args ) throws ZipException ,
IOException
{
// get a zip file instance
File file = new File ( "/home/liangruihua/ziptest/test.zip" ) ; // get a ZipFile instance
ZipFile zipFile = new ZipFile ( file ) ; // create a ZipInputStream instance
ZipInputStream zis = new ZipInputStream ( new FileInputStream (
file ) ) ; // create a ZipEntry instance , lay the every file from
// decompress file temporarily
ZipEntry entry = null ; // a circle to get every file
while ( ( entry = zis.getNextEntry ( ) ) != null )
{
System.out.println ( "decompress file :"
+ entry.getName ( ) ) ; // define the path to set the file
File outFile = new File ( "/home/liangruihua/ziptest/"
+ entry.getName ( ) ) ; // if the file's parent directory wasn't exits ,than
// create the directory
if ( ! outFile.getParentFile ( ).exists ( ) )
{
outFile.getParentFile ( ).mkdir ( ) ;
} // if the file not exits ,than create the file
if ( ! outFile.exists ( ) )
{
outFile.createNewFile ( ) ;
} // create an input stream
BufferedInputStream bis = new BufferedInputStream (
zipFile.getInputStream ( entry ) ) ; // create an output stream
BufferedOutputStream bos = new BufferedOutputStream (
new FileOutputStream ( outFile ) ) ;
byte [ ] b = new byte [ 100 ] ;
while ( true )
{
int len = bis.read ( b ) ;
if ( len == - 1 )
break ;
bos.write ( b , 0 , len ) ;
}
// close stream
bis.close ( ) ;
bos.close ( ) ;
}
zis.close ( ) ; }
}
java 文件压缩和解压(ZipInputStream, ZipOutputStream)的更多相关文章
- java文件压缩和解压
功能实现. package com.test; import java.io.File; import java.io.BufferedOutputStream; import java.io.Buf ...
- 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 ...
随机推荐
- Spring-cloud & Netflix 源码解析:Eureka 服务注册发现接口 ****
http://www.idouba.net/spring-cloud-source-eureka-client-api/?utm_source=tuicool&utm_medium=refer ...
- svn patch
生成patch文件: svn diff > patchFile // 整个工程的变动生成patch 或svn diff file > patchFile // 某个文件单独变动的patch ...
- CentOS 配置防火墙操作实例(启、停、开、闭端口):
CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service iptables status< ...
- ireport5.6+jasperreport6.3开发(二)--web开发的配置
ireport5.6只能编译出*.jasper的报表包,最终报表需要被输出为一个doc html pdf excel等文件,这时就需要jasperreport6.5的库进行配合了. jasperrep ...
- python爬取网易评论
学习python不久,最近爬的网页都是直接源代码中直接就有的,看到网易新闻的评论时,发现评论时以json格式加载的..... 爬的网页是习大大2015访英的评论页http://comment.news ...
- 3d旋转
<!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...
- 在VC++6.0开发中实现全屏显示
全屏显示是一些应用软件程序必不可少的功能.比如在用VC++编辑工程源文件或编辑对话框等资源时,选择菜单“View\Full Screen”,即可进入全屏显示状态,按“Esc”键后会退出全屏显示状态. ...
- c#数据绑定(4)——向查询中添加参数
本实例主要练习了ADO.Net 连接到外部数据库的基础上,向查询中添加参数.使用的是ACCESS数据库. 在ACCESS数据库中可以用MSSQL的形式定义操作字符串,也可以采用OLEDB的形式. MS ...
- .NET小细节
1.equals()和运算符==的区别 C#中有两种不同的相等:引用相等和值相等.值相等是两个对象包含相同的值:引用相等是两个对象引用的是同一个对象. “==”操作符比较的是两个变量的值是否相等,或两 ...
- Git bash使用中...
理解Head指向分支及标签tag Head指向当前分支(如master),可以把每次提交完成的修改看成一个圈,在修改上可以开另一分支为接下来的修改做准备,未修改前新建的分支与当前分支可看成绕修改圈转: ...