最近在看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)的更多相关文章

  1. java文件压缩和解压

    功能实现. package com.test; import java.io.File; import java.io.BufferedOutputStream; import java.io.Buf ...

  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. C语言局部变量和全局变量的区别。——Arvin

    局变量是使用相同的内存块在整个类中存储一个值. 全局变量的存在主要有以下一些原因:  1,使用全局变量会占用更多的内存(因为其生命期长),不过在计算机配置很高的今天,这个不成为什么问题,除非使用的是巨 ...

  2. Python_Day7_面向对象学习

    1.面向对象编程介绍 2.为什么要用面向对象进行开发? 3.面向对象的特性:封装.继承.多态 4.类.方法. 面向过程 VS 面向对象 编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告 ...

  3. iOS使用Security.framework进行RSA 加密解密签名和验证签名

    iOS 上 Security.framework为我们提供了安全方面相关的api: Security框架提供的RSA在iOS上使用的一些小结 支持的RSA keySize 大小有:512,768,10 ...

  4. Java中的JDK动态代理

    所谓代理,其实就是相当于一个中间人,当客户端需要服务端的服务时,不是客户直接去找服务,而是客户先去找代理,告诉代理需要什么服务,然后代理再去服务端找服务,最后将结果返回给客户. 在日常生活中,就拿买火 ...

  5. 本机不能访问虚拟机中flask的web服务器的解决办法

    在虚拟机的linux里写flask程序,在本机中用"ip:端口号"的方法不能访问,查资料试了各种方法都不行.最后发现问题出在flask程序本身: flask的app.run()方法 ...

  6. MySQL学习记录--分组与聚集

    一.group by 子句 group by 字句可对数据进行分组. 以MySQL5.5的sakila数据库中的film数据表举例:查找出各个电影等级的电影总数 mysql>SELECT rat ...

  7. 计时器js

    <html>    <head>    <meta http-equiv="Content-Type" content="text/html ...

  8. hive配置以及在启动过程中出现的问题

    一.hive配置 1.安装环境 在hadoop-1.2.1集群上安装hive-1.2.1 2.将hive-1.2.1环境变量添加到PATH路径下 使用如下命令打开配置文件 nano /etc/prof ...

  9. 查看Linux是32位还是64位

    最直接简洁的办法: 在linux终端输入getconf LONG_BIT命令 如果是32位机器,则结果为32 [root@localhost ~]# getconf LONG_BIT 32 如果是64 ...

  10. PHP返回XML数据

    开发中经常会有用到XML格式数据的时候,那么用PHP怎么来将数组格式的数据用XML格式返回呢? 新建一个类文件,叫ArrayToXml.php <?php /** * 数组转XML类 * 使用方 ...