JAVA 文件压缩和解压(ZIPINPUTSTREAM, ZIPOUTPUTSTREAM)
来源:互联网 发布:c语言韩信点兵怎么理解 编辑:程序博客网 时间:2024/06/01 07:25
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还可以对文件夹进行压缩和解压。
ZipInputStream位于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 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工具类)
- Dialog的几种用法
- mybatis-config.xml的配置详解
- 设计模式之工厂方法模式
- 无法解析的外部符号 _Netbios解决方法
- c++ ABCBank项目(四)
- JAVA 文件压缩和解压(ZIPINPUTSTREAM, ZIPOUTPUTSTREAM)
- linux下实现同步的---条件变量
- Myeclipse SVN查看版本历史时Item is not readable svn:条目不可读的问题
- optimize table 表优化问题
- 谈谈Java反射机制
- windows程序设计读书笔记二
- javascript-Doctype, img图片预加载, js文档碎片
- JNI分析
- Spring之IOC讲解