001/*
002 * Copyright (c) 1999-2001 Keiron Liddle, Aftex Software
003 *
004 * This library is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License as published by the Free Software Foundation; either
007 * version 2.1 of the License, or (at your option) any later version.
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012 * Lesser General Public License for more details.
013 *
014*/
015/**
016 * Does all the compress and decompress pre-operation stuff.
017 * Sets up the streams and file header characters.
018 * Uses multiply overloaded methods to call for the compress/decompress.
019 */
020package com.aftexsw.util.bzip;
021
022import org.apache.excalibur.bzip2.*;
023
024import java.io.*;
025import java.lang.*;
026
027public final class BZip {
028        final public static void decompress(File infile, File outfile) {
029                decompress(infile, outfile, dummyProg);
030        }
031
032        final public static void compress(File infile, File outfile, int blockSize) {
033                compress(infile, outfile, blockSize, dummyProg);
034        }
035
036        final public static void decompress(String infilename, String outfilename, ProgressListener prog) {
037                File theinFile = new File(infilename);
038                File theoutFile = new File(outfilename);
039                decompress(theinFile, theoutFile, prog);
040        }
041
042        final public static void decompress(File infile, File outfile, ProgressListener prog) {
043                try {
044                        FileOutputStream fos = new FileOutputStream(outfile);
045                        FileInputStream fis = new FileInputStream(infile);
046                        decompress(fis, fos, prog);
047                        fis.close();
048                        fos.close();
049                        //infile.close();
050                        //outfile.close();
051                }
052                catch(IOException e) {
053                        e.printStackTrace();
054                }
055        }
056
057        final public static void decompress(InputStream instream, File outfile, ProgressListener prog) {
058                try {
059                        FileOutputStream fos = new FileOutputStream(outfile);
060                        decompress(instream, fos, prog);
061                        fos.close();
062                        //outfile.close();
063                }
064                catch(IOException e) {
065                        e.printStackTrace();
066                }
067        }
068
069        final public static void decompress(File infile, OutputStream outstream, ProgressListener prog) {
070                try {
071                        FileInputStream fis = new FileInputStream(infile);
072                        decompress(fis, outstream, prog);
073                        fis.close();
074                        //infile.close();
075                }
076                catch(IOException e) {
077                        e.printStackTrace();
078                }
079        }
080
081        /**
082         * Use the BZip2InputStream to uncompress data.
083        */
084        final public static void decompress(InputStream instream, OutputStream outstream, ProgressListener prog) {
085                try {
086                        BufferedOutputStream bos = new BufferedOutputStream(outstream);
087                        BufferedInputStream bis = new BufferedInputStream(instream);
088                        int b = bis.read();
089                        if(b != 'B')
090                                return;
091                        b = bis.read();
092                        if(b != 'Z')
093                                return;
094                        CBZip2InputStream bzis = new CBZip2InputStream(bis);
095                        int ch = bzis.read();
096                        while(ch != -1) {
097                                bos.write(ch);
098                                ch = bzis.read();
099                        }
100                        bos.flush();
101                } catch(IOException e) {
102                        e.printStackTrace();
103                }
104        }
105
106        final public static boolean test(String infilename) {
107                File theinFile = new File(infilename);
108                return test(theinFile);
109        }
110
111        final public static boolean test(File infile) {
112                try {
113                        FileInputStream fis = new FileInputStream(infile);
114                        BufferedInputStream bis = new BufferedInputStream(fis);
115                        int b = bis.read();
116                        if(b != 'B') {
117                                System.out.println("incorrect start characters");
118                                return false;
119                        }
120                        b = bis.read();
121                        if(b != 'Z') {
122                                System.out.println("incorrect start characters");
123                                return false;
124                        }
125                        //CBZip2Decompress cbd = new CBZip2Decompress(dummyProg);
126                        //return cbd.testStream(bis);
127                } catch(IOException e) {
128                        e.printStackTrace();
129                }
130                return false;
131        }
132
133        final public static ProgressListener dummyProg = new ProgressListener() {
134                                public void setProgress(long val) {}
135                        }
136                        ;
137
138        final public static void compress(String infilename, String outfilename, int blockSize, ProgressListener prog) {
139                File theinFile = new File(infilename);
140                File theoutFile = new File(outfilename);
141                compress(theinFile, theoutFile, blockSize, prog);
142        }
143
144        final public static void compress(File infile, File outfile, int blockSize, ProgressListener prog) {
145                try {
146                        FileOutputStream fos = new FileOutputStream(outfile);
147                        FileInputStream fis = new FileInputStream(infile);
148                        compress(fis, fos, blockSize, prog);
149                } catch(IOException e) {
150                        e.printStackTrace();
151                }
152        }
153
154        final public static void compress(InputStream instream, File outfile, int blockSize, ProgressListener prog) {
155                try {
156                        FileOutputStream fos = new FileOutputStream(outfile);
157                        compress(instream, fos, blockSize, prog);
158                } catch(IOException e) {
159                        e.printStackTrace();
160                }
161        }
162
163        final public static void compress(File infile, OutputStream outstream, int blockSize, ProgressListener prog) {
164                try {
165                        FileInputStream fis = new FileInputStream(infile);
166                        compress(fis, outstream, blockSize, prog);
167                } catch(IOException e) {
168                        e.printStackTrace();
169                }
170        }
171        /**
172         * Use the CBZip2OutputStream to compress data.
173        */
174        final public static void compress(InputStream instream, OutputStream outstream, int blockSize, ProgressListener prog) {
175                try {
176                        BufferedOutputStream bos = new BufferedOutputStream(outstream);
177                        bos.write('B');
178                        bos.write('Z');
179                        BufferedInputStream bis = new BufferedInputStream(instream);
180                        int ch = bis.read();
181                        CBZip2OutputStream bzos = new CBZip2OutputStream(bos, blockSize);
182                        while(ch != -1) {
183                                bzos.write(ch);
184                                ch = bis.read();
185                        }
186                        bzos.close();
187                        bos.flush();
188                        bos.close();
189                        outstream.flush();
190                        outstream.close();
191                } catch(IOException e) {
192                        e.printStackTrace();
193                }
194                System.gc();
195        }
196}