JAVA 文件压缩和解压(ZIPINPUTSTREAM, ZIPOUTPUTSTREAM)
来源:互联网 发布:cocos2dx-js教程 编辑:程序博客网 时间:2024/05/20 11:21
最近在看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 }
阅读全文
0 0
- JAVA 文件压缩和解压(ZIPINPUTSTREAM, ZIPOUTPUTSTREAM)
- JAVA 文件压缩和解压(ZIPINPUTSTREAM, ZIPOUTPUTSTREAM)
- 利用ZipOutputStream和ZipInputStream直接压缩和解压字符串
- 用ZipInputStream和ZipOutputStream实现文件及文件夹的压缩解压
- Java IO--压缩流ZipOutputStream/ZipInputStream
- Java IO 压缩流(ZipOutputStream/ZipInputStream)
- 支持中文的zip压缩和解压,修改自带jdk ZipInputStream、ZipOutputStream、ZipEntry代码
- ZipOutputStream 和 ZipInputStream 例子
- 压缩解压缩工具 ZipInputStream ZipOutputStream
- Java IO操作——掌握压缩流的使用(ZipOutputStream、ZipFile、ZipInputStream)[java.util包中]
- ZipInputStream & ZipOutputStream
- 【Java.IO】I/O 【字节】【处理流】 - 之 - 【压缩流】 - ZipInputStream,ZipOutputStream
- Java IO 之 ZipFile/ZipInputStream/ZipOutputStream
- 让ZipOutputStream和ZipInputStream支持中文
- 让ZipOutputStream和ZipInputStream支持中文
- java压缩和解压文件
- Java中用原生ZipInputStream压缩加压zip文件
- 文件的压缩和解压(java工具类)
- PHP基础之变量(2)
- 判断cpu是大端模式还是小端模式
- C语言练习
- 编辑器vim的相关指令
- C语言构建线性表
- JAVA 文件压缩和解压(ZIPINPUTSTREAM, ZIPOUTPUTSTREAM)
- The maximum column width for an individual cell is 255 characters. 的解决方案
- caioj 1362 NOIP2015普及组 第三题 求和
- MySQL之日志管理篇
- 总结RxJava操作符
- 我的HCIE日记
- 【Java】把字节数B转化为KB、MB、GB的方法
- K-th Nya Number (数位dp+二分)
- 树莓派硬件启动失败log记录查看方法