Subversion Repositories XServices

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 brianR 1
/*
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
17
package net.brutex.xservices.util;
18
 
19
import de.innosystec.unrar.Archive;
20
import de.innosystec.unrar.exception.RarException;
21
import de.innosystec.unrar.rarfile.FileHeader;
22
import java.io.File;
23
import java.io.FileOutputStream;
24
import java.io.IOException;
25
import java.util.List;
26
import org.apache.tools.ant.BuildException;
27
import org.apache.tools.ant.Task;
28
 
29
/**
30
 *
31
 * @author Brian Rosenberger, bru@brutex.de
32
 */
33
public class UnRarTask extends Task {
34
 
35
    private File dst = null;
36
    private File src = null;
37
 
38
    public File getDst() {
39
        return dst;
40
    }
41
 
42
    public void setDst(File dst) {
43
        this.dst = dst;
44
    }
45
 
46
    public File getSrc() {
47
        return src;
48
    }
49
 
50
    public void setSrc(File src) {
51
        this.src = src;
52
    }
53
 
54
 
55
    @Override
56
    public void execute() {
57
        if(src==null ) throw new BuildException("Please supply a source archive file.");
58
        if(!src.exists()) throw new BuildException("Archive '"+src.getName()+"' does not exist.");
59
 
60
        try {
61
            if(dst==null) dst = new File(src.getParent());
62
            Archive ar = new Archive(src);
63
            List<FileHeader> list = ar.getFileHeaders();
64
            for(FileHeader h : list) {
65
                ar.extractFile(h, new FileOutputStream(new File(dst.getAbsolutePath()+"/"+h.getFileNameString())));
66
            }
67
        } catch (RarException ex) {
68
            throw new BuildException(ex.getMessage(), ex);
69
        } catch (IOException ex) {
70
            throw new BuildException(ex.getMessage(), ex);
71
        }
72
    }
73
 
74
}