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 ...
随机推荐
- 离线更新VSAN HCL数据库
从VSAN 6.0起,VSAN提供了Health Check功能,其中就包括VSAN HCL数据库,通过此运行状况检查验证用于 HCL 检查的 VMware 兼容性指南数据库是否是最新的.这些 VCG ...
- HTML5 十大新特性(四)——Canvas绘图
H5引入了canvas标签,默认是一个300*150的inline-block.canvas的宽高只能用它自身的width和height属性来指定,而不能使用css样式中的width.height. ...
- java aes_cbc_256 加密解密
在之前我们在openssl上和ios上分别测试了 AES256位cbc模式的加密和解密 今天用java提供的api来测试一下:进而确定一下在PC,iOS,安卓上三个平台下的加密解密数据: 1. 首先通 ...
- Oracle安装时OracleMTSRecoveryService找不到
电脑重做系统之后再安装oracle过程中出现一个问题,说OracleMTSRecoveryService找不到指定的目录文件,卸载重装还是没有改变,挣了半天终于找到怎么更改了,打开注册表编辑器,SYS ...
- 在进行javaIO写文件操作后文件内容为空的情况
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\order.txt"))) ...
- GO语言学习
1. 语言特色 可直接编译成机器码,不依赖其他库,glibc的版本有一定要求,部署就是扔一个文件上去就完成了. 静态类型语言,但是有动态语言的感觉,静态类型的语言就是可以在编译的时候检查出来隐藏的大多 ...
- Discuz 网站移至 Ubuntu 14.04.4 LTS VPS 配置
查看 当前系统版本信息 复制命令:lsb_release -a 1.首先更新本地软件库索引 复制命令:apt-get update 2.安装apache2 复制命令:apt-get install ...
- SPSS数据分析—对应分析
卡方检验只能对两个分类变量之间是否存在联系进行检验,如果分类变量有多个水平的话,则无法衡量每个水平间的联系.对此,虽然可以使用逻辑回归进行建模,但是如果分类变量的水平非常多,就需要分别设定哑变量,这样 ...
- 浏览器地址栏背后的logic
曾经有面试题是这样的:"描述在浏览器的地址栏中输入:http://www.baidu.com 后发生了什么?". 1.服务端返回baidu页面资源,浏览器载入html 2.浏览器开 ...
- django服务器配置
服务器配置是Ubuntu14.04 64位OS ubuntu14.04默认是安装好了python2.7版本不用自己安装了. 先更新下源 sudo apt-get update 第一步先安装pip su ...