博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 文件压缩和解压(ZipInputStream, ZipOutputStream)
阅读量:4886 次
发布时间:2019-06-11

本文共 5333 字,大约阅读时间需要 17 分钟。

  最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还可以对文件夹进行压缩和解压。

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

 

 

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

 

 

文件的压缩

1 public class TestFile 2 { 3     public static void main ( String [ ] args ) throws IOException 4     { 5         // new a file input stream 6         FileInputStream fis = new FileInputStream ( 7                 "/home/liangruihua/ziptest/1.txt" ) ; 8         BufferedInputStream bis = new BufferedInputStream ( fis ) ; 9 10         // new a zipPutputStream11         // /home/liangruihua/ziptest/1.zip -- the out put file path and12         // name13         ZipOutputStream zos = new ZipOutputStream (14                 new FileOutputStream (15                         "/home/liangruihua/ziptest/1.zip" ) ) ;16         BufferedOutputStream bos = new BufferedOutputStream ( zos ) ;17 18         // set the file name in the .zip file19         zos.putNextEntry ( new ZipEntry ( "1.txt" ) ) ;20 21         // set the declear22         zos.setComment ( "by liangruihua test!" ) ;23 24         byte [ ] b = new byte [ 100 ] ;25         while ( true )26         {27             int len = bis.read ( b ) ;28             if ( len == - 1 )29                 break ;30             bos.write ( b , 0 , len ) ;31         }32         fis.close ( ) ;33         zos.close ( ) ;34     }35 }

 

文件夹的压缩

1 public class TestDir 2 { 3     public static void main ( String [ ] args ) throws IOException 4     { 5         // the file path need to compress 6         File file = new File ( "/home/liangruihua/ziptest/test" ) ; 7         ZipOutputStream zos = new ZipOutputStream ( 8                 new FileOutputStream ( 9                         "/home/liangruihua/ziptest/test.zip" ) ) ;10 11         // judge the file is the directory12         if ( file.isDirectory ( ) )13         {14             // get the every file in the directory15             File [ ] files = file.listFiles ( ) ;16 17             for ( int i = 0 ; i < files.length ; i ++ )18             {19                 // new the BuuferedInputStream20                 BufferedInputStream bis = new BufferedInputStream (21                         new FileInputStream (22                                 files [ i ] ) ) ;23                 // the file entry ,set the file name in the zip24                 // file25                 zos.putNextEntry ( new ZipEntry ( file26                         .getName ( )27                         + file.separator28                         + files [ i ].getName ( ) ) ) ;29                 while ( true )30                 {31                     byte [ ] b = new byte [ 100 ] ;32                     int len = bis.read ( b ) ;33                     if ( len == - 1 )34                         break ;35                     zos.write ( b , 0 , len ) ;36                 }37 38                 // close the input stream39                 bis.close ( ) ;40             }41 42         }43         // close the zip output stream44         zos.close ( ) ;45     }46 }

文件的解压

1 public class TestZipInputStream 2 { 3     public static void main ( String [ ] args ) throws ZipException , 4             IOException 5     { 6         // get a zip file instance 7         File file = new File ( "/home/liangruihua/ziptest/test.zip" ) ; 8  9         // get a ZipFile instance10         ZipFile zipFile = new ZipFile ( file ) ;11 12         // create a ZipInputStream instance13         ZipInputStream zis = new ZipInputStream ( new FileInputStream (14                 file ) ) ;15 16         // create a ZipEntry instance , lay the every file from17         // decompress file temporarily18         ZipEntry entry = null ;19 20         // a circle to get every file21         while ( ( entry = zis.getNextEntry ( ) ) != null )22         {23             System.out.println ( "decompress file :"24                     + entry.getName ( ) ) ;25 26             // define the path to set the file27             File outFile = new File ( "/home/liangruihua/ziptest/"28                     + entry.getName ( ) ) ;29 30             // if the file's parent directory wasn't exits ,than31             // create the directory32             if ( ! outFile.getParentFile ( ).exists ( ) )33             {34                 outFile.getParentFile ( ).mkdir ( ) ;35             }36 37             // if the file not exits ,than create the file38             if ( ! outFile.exists ( ) )39             {40                 outFile.createNewFile ( ) ;41             }42 43             // create an input stream44             BufferedInputStream bis = new BufferedInputStream (45                     zipFile.getInputStream ( entry ) ) ;46 47             // create an output stream48             BufferedOutputStream bos = new BufferedOutputStream (49                     new FileOutputStream ( outFile ) ) ;50             byte [ ] b = new byte [ 100 ] ;51             while ( true )52             {53                 int len = bis.read ( b ) ;54                 if ( len == - 1 )55                     break ;56                 bos.write ( b , 0 , len ) ;57             }58             // close stream59             bis.close ( ) ;60             bos.close ( ) ;61         }62         zis.close ( ) ;63 64     }65 }

 

转载于:https://www.cnblogs.com/lrh-xl/p/5509005.html

你可能感兴趣的文章
【LOJ】#2041. 「SHOI2015」聚变反应炉
查看>>
【BZOJ】3640: JC的小苹果
查看>>
数据库-mysql误删performance_schema
查看>>
Spring boot 配置嵌入式Servlet容器
查看>>
Ext Js【Hello World】 ——4.1 beta 1
查看>>
Unity Lighting - Lighting overview 照明概述
查看>>
系统性测试报告模板
查看>>
[后缀数组][并查集] Luogu P2178 品酒大会
查看>>
react学习总结(一)
查看>>
Sentry 前端的错误监控
查看>>
mysql_fetch_row,mysql_fetch_array,mysql_fetch_object,mysql_fetch_assoc的区别
查看>>
Linux 存储管理2——内存管理
查看>>
spring事物不回滚的问题
查看>>
字符串中最长回文,最笨的解法
查看>>
HTML: 用表格畫出課程表
查看>>
Thread之四:java线程返回结果的方法
查看>>
【转】NSDictionary和NSMutableDictionary用法详解
查看>>
shell脚本解析4----分支语句(if)
查看>>
001编程基础----GDB程序调试
查看>>
<C++ - 继承02> 2018-01-24
查看>>