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.CBZip2OutputStream;
26  
27  /***
28   * This simple example shows how to use the Bzip2 classes to compress a file.
29   *
30   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
31   * @version $Revision: 155439 $ $Date: 2005-02-26 13:18:41 +0000 (Sat, 26 Feb 2005) $
32   */
33  public class Bzip2Compress
34  {
35      public static void main( final String[] args )
36          throws Exception
37      {
38          if( 2 != args.length )
39          {
40              System.out.println( "java Bzip2Compress <input> <output>" );
41              System.exit( 1 );
42          }
43  
44          final File source = new File( args[ 0 ] );
45          final File destination = new File( args[ 1 ] );
46          final CBZip2OutputStream output =
47              new CBZip2OutputStream( new FileOutputStream( destination ) );
48          final FileInputStream input = new FileInputStream( source );
49          copy( input, output );
50          input.close();
51          output.close();
52      }
53  
54      /***
55       * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
56       */
57      private static void copy( final InputStream input,
58                                final OutputStream output )
59          throws IOException
60      {
61          final byte[] buffer = new byte[ 8024 ];
62          int n = 0;
63          while( -1 != ( n = input.read( buffer ) ) )
64          {
65              output.write( buffer, 0, n );
66          }
67      }
68  }