www.pudn.com > jrar-0.40-source-icons.zip > ExtractionRarProcess.java


/* 
 * Copyright (C) 2003-2004 Andrew Smith 
 *  
 * This program is free software; you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation; either version 2 of the License, or (at your option) any later 
 * version. 
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details. 
 *  
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
 * Place, Suite 330, Boston, MA 02111-1307 USA 
 */ 
 
import java.io.BufferedWriter; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
 
public class ExtractionRarProcess extends RarProcess implements Runnable { 
 
    String currentFile, currentDir, filesToExtract; 
 
    int finished = 0; 
 
    boolean shouldIStop = false; 
 
    public ExtractionRarProcess(String f, String fte, String d, Jrar j) { 
        currentFile = f; 
        filesToExtract = fte; 
        currentDir = d; 
        jrar = j; 
         
         
    } 
 
    public void run() { 
        jrar.setJGUIButtonsState(false); 
        jrar.addCancelButtonToJGUIStatusBar(this); 
        jrar.updateJGUIStatusBar("" + JrarConstants.EXTRACTIONMESSAGE); 
         
        try { 
            String command = JrarConstants.extractionCommand  + "\"" 
            	+ currentFile + "\" " +  filesToExtract + " " + currentDir;  
             
            jrar.addToDebugWindow("command being run - " + command); 
 
            p = Runtime.getRuntime().exec("rar " + command); 
 
            jrar.addToDebugWindow("Starting startInputListener"); 
            startInputListener(new InputStreamReader(p.getErrorStream())); 
        } catch (Exception e) { 
            jrar.updateJGUIStatusBar("Error T: " + e); 
            jrar.addToDebugWindow("Error T: " + e); 
            p.destroy(); 
        } 
         
        if (!shouldIStop) { 
            jrar.addToDebugWindow("Finished ExtractionRarProcess"); 
            jrar.extractionRarProcessFinishedSucessfully(); 
        } 
        else { 
            jrar.addToDebugWindow("ExtractionRarProcess stopped by user"); 
            p.destroy(); 
            jrar.extractionRarProcessStopped(); 
        } 
    } 
 
    private void startInputListener(InputStreamReader ipsr) { 
        String s = ""; 
        jrar.addToDebugWindow("in startInputListener"); 
        try { 
            int temp; 
            do { 
                temp = ipsr.read(); 
                System.out.print((char) temp); 
                s += (char) temp; 
 
                String[] sp = s.split("\n"); 
 
                if (sp[sp.length - 1].endsWith("[Q]uit")) { 
                    jrar.addToDebugWindow("overwrite?"); 
                    temp = ipsr.read(); 
                    s += (char) temp; 
                    sp = s.split("\n"); 
                    overwriteRequest(sp); 
                } 
 
            } while ((temp != -1) && (!shouldIStop)); 
        } catch (Exception e) { 
            jrar.addToDebugWindow("Error PSH: " + e); 
        } 
        if (shouldIStop) { 
            jrar.addToDebugWindow("thread canceled by user"); 
        } 
    } 
 
    private void overwriteRequest(String[] splitOutput) { 
 
        String tempFileName = splitOutput[splitOutput.length - 2].substring(0, 
                splitOutput[splitOutput.length - 2].length() - 32); 
        char answer = jrar.getOverwriteInput(tempFileName); 
 
        try { 
            BufferedWriter opw = new BufferedWriter(new OutputStreamWriter(p 
                    .getOutputStream())); 
            opw.write(answer); 
            opw.newLine(); 
            opw.flush(); 
        } catch (Exception e) { 
            jrar.updateJGUIStatusBar("Error O: " + e); 
            jrar.addToDebugWindow("Error O: " + e); 
        } 
    } 
 
    public void setStop(boolean b) { 
        jrar.addToDebugWindow("setting shouldIStop to " + b); 
        shouldIStop = b; 
    } 
}