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.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 }