Subversion Repositories XServices

Rev

Rev 6 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.brutex.xservices.util;

import de.innosystec.unrar.Archive;
import de.innosystec.unrar.exception.RarException;
import de.innosystec.unrar.rarfile.FileHeader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 *
 * @author Brian Rosenberger, bru@brutex.de
 */
public class UnRarTask extends Task {

    private File dst = null;
    private File src = null;

    public File getDst() {
        return dst;
    }

    public void setDst(File dst) {
        this.dst = dst;
    }

    public File getSrc() {
        return src;
    }

    public void setSrc(File src) {
        this.src = src;
    }


    @Override
    public void execute() {
        if(src==null ) throw new BuildException("Please supply a source archive file.");
        if(!src.exists()) throw new BuildException("Archive '"+src.getName()+"' does not exist.");

        try {
            if(dst==null) dst = new File(src.getParent());
            Archive ar = new Archive(src);
            List<FileHeader> list = ar.getFileHeaders();
            for(FileHeader h : list) {
                ar.extractFile(h, new FileOutputStream(new File(dst.getAbsolutePath()+"/"+h.getFileNameString())));
            }
        } catch (RarException ex) {
            throw new BuildException(ex.getMessage(), ex);
        } catch (IOException ex) {
            throw new BuildException(ex.getMessage(), ex);
        }
    }

}