1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.compress.bzip2.example;
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  import org.apache.commons.compress.bzip2.CBZip2InputStream;
26  
27  /***
28   * This simple example shows how to use the Bzip2 classes to uncompress a file.
29   *
30   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
31   * @author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
32   * @version $Revision: 155439 $ $Date: 2005-02-26 13:18:41 +0000 (Sat, 26 Feb 2005) $
33   */
34  public class Bzip2Uncompress
35  {
36      public static void main( final String[] args )
37      {
38        try
39        {
40          if( 2 != args.length )
41          {
42              System.out.println( "java Bzip2Uncompress <input> <output>" );
43              System.exit( 1 );
44          }
45          final File source = new File( args[ 0 ] );
46          final File destination = new File( args[ 1 ] );
47          final FileOutputStream output =
48              new FileOutputStream( destination );
49          final CBZip2InputStream input = new CBZip2InputStream( new FileInputStream( source ) );
50          copy( input, output );
51          input.close();
52          output.close();
53        }catch(Exception e){
54          e.printStackTrace();
55          System.exit(1);       
56        
57        }
58      }
59  
60      /***
61       * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
62       */
63      private static void copy( final InputStream input,
64                                final OutputStream output )
65          throws IOException
66      {
67          final byte[] buffer = new byte[ 8024 ];
68          int n = 0;
69          while( -1 != ( n = input.read( buffer ) ) )
70          {
71              output.write( buffer, 0, n );
72          }
73      }
74  }