小凉

3 0642700 3 0642770 5 34202 13942 4314 0624

java 文件压缩和解压(ZipInputStream, ZipOutputStream)

  最近在看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 zipPutputStream
11         // /home/liangruihua/ziptest/1.zip -- the out put file path and
12         // name
13         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 file
19         zos.putNextEntry ( new ZipEntry ( "1.txt" ) ) ;
20 
21         // set the declear
22         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 directory
12         if ( file.isDirectory ( ) )
13         {
14             // get the every file in the directory
15             File [ ] files = file.listFiles ( ) ;
16 
17             for ( int i = 0 ; i < files.length ; i ++ )
18             {
19                 // new the BuuferedInputStream
20                 BufferedInputStream bis = new BufferedInputStream (
21                         new FileInputStream (
22                                 files [ i ] ) ) ;
23                 // the file entry ,set the file name in the zip
24                 // file
25                 zos.putNextEntry ( new ZipEntry ( file
26                         .getName ( )
27                         + file.separator
28                         + 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 stream
39                 bis.close ( ) ;
40             }
41 
42         }
43         // close the zip output stream
44         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 instance
10         ZipFile zipFile = new ZipFile ( file ) ;
11 
12         // create a ZipInputStream instance
13         ZipInputStream zis = new ZipInputStream ( new FileInputStream (
14                 file ) ) ;
15 
16         // create a ZipEntry instance , lay the every file from
17         // decompress file temporarily
18         ZipEntry entry = null ;
19 
20         // a circle to get every file
21         while ( ( entry = zis.getNextEntry ( ) ) != null )
22         {
23             System.out.println ( "decompress file :"
24                     + entry.getName ( ) ) ;
25 
26             // define the path to set the file
27             File outFile = new File ( "/home/liangruihua/ziptest/"
28                     + entry.getName ( ) ) ;
29 
30             // if the file's parent directory wasn't exits ,than
31             // create the directory
32             if ( ! outFile.getParentFile ( ).exists ( ) )
33             {
34                 outFile.getParentFile ( ).mkdir ( ) ;
35             }
36 
37             // if the file not exits ,than create the file
38             if ( ! outFile.exists ( ) )
39             {
40                 outFile.createNewFile ( ) ;
41             }
42 
43             // create an input stream
44             BufferedInputStream bis = new BufferedInputStream (
45                     zipFile.getInputStream ( entry ) ) ;
46 
47             // create an output stream
48             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 stream
59             bis.close ( ) ;
60             bos.close ( ) ;
61         }
62         zis.close ( ) ;
63 
64     }
65 }

 

posted on 2016-05-19 16:01  小凉  阅读(41148)  评论(3编辑  收藏  举报

导航