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 ...
随机推荐
- OCP笔记001
2. View the Exhibit to examine the description for the SALES table. Which views can have all DML ope ...
- File system needs to be upgraded. You have version null and I want version 7
安装hbase时候报错: File system needs to be upgraded. You have version null and I want version 7 注: 我安装的hba ...
- java: system.gc()和垃圾回收机制finalize
System.gc()和垃圾回收机制前的收尾方法:finalize(收尾机制) 程序退出时,为每个对象调用一次finalize方法,垃圾回收前的收尾方法 System.gc() 垃圾回收方法 clas ...
- 可靠UDP
tcp为我们做了什么事情? 总得来说,tcp做了这几件事: 通过序列号和基于确认的超时重传机制,为上层提供了可靠的字节流服务: 通过滑动窗口.拥塞窗口提供了流量控制: 默认情况下,为了有效利用带宽,t ...
- 命令查看DB restore进度
SELECT DB_NAME(er.[database_id]) [DatabaseName],er.[session_id] AS [SessionID],er.[command] AS [Comm ...
- Android NDK 项目依赖简单示例
目录文件结构如图, 进入main目录执行命令 .ndkbuild NDK_MODULE_PATH=../ 说明 .ndkbuild请替换成有效的ndk-build的命令 所有文件下载 http://p ...
- python简介和入门
一.什么是python? python是一种面向对象.解释型的计算机语言,它的特点是语法简洁.优雅.简单易学. 二.解释型语言和编译型语言 编译型语言--就是先把写好的程序翻译成计算机语言然后执行,就 ...
- Tomcat(免安装版)的安装与配置
一.下载Tomcat Tomcat可以从http://tomcat.apache.org/网站下载,选择任意版本,在 Binary Distributions 下的zip包既是. 二.配置Tomcat ...
- javascript 中继承实现方式归纳
转载自:http://sentsin.com/web/1109.html 不同于基于类的编程语言,如 C++ 和 Java,javascript 中的继承方式是基于原型的.同时由于 javascrip ...
- CentOS 7 Mysql yum源
CentOS 7 安装Mysqlrpm -ivh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpmyum install m ...