1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }