source: other-projects/trunk/7z-ant/src/org/apache/tools/ant/SevenZip.java@ 17387

Last change on this file since 17387 was 17387, checked in by oranfry, 16 years ago

Modified ant task which works with the updated 7zip API

File size: 4.7 KB
Line 
1package org.apache.tools.ant;
2
3import java.io.File;
4import java.util.Vector;
5
6import SevenZip.LzmaAlone;
7
8/**
9 * @author Jean Lycoops
10 * Bansions 33
11 * B-4845 Sart-lez-Spa
12 * Belgium
13 * [email protected]
14 */
15public class SevenZip extends Task {
16 private String input;
17 private String output;
18 private static final int ENCODE = 0;
19 private static final int DECODE = 1;
20 private static final int BENCHMARK = 2;
21 private static final int NONE = -1;
22 private String a;
23 private String d;
24 private String fb;
25 private String lc;
26 private String lp;
27 private String pb;
28 private String mf;
29 private String eos;
30 private static final String[] TASKS = { "encode", "decode", "benchmark" };
31 private static final String[] MATCH_FINDERS = { "bt2", "bt4", "bt4b" };
32 private int task;
33 public SevenZip() {
34 super();
35 this.input = null;
36 this.output = null;
37 this.task = NONE;
38 this.a = null;
39 this.d = null;
40 this.fb = null;
41 this.lc = null;
42 this.lp = null;
43 this.pb = null;
44 this.mf = null;
45 this.eos = null;
46 }
47 private void parseInt(String value, String tag, String switch_, int min, int max, Vector args) throws BuildException {
48 if (value == null) return;
49 try {
50 Integer parsed = new Integer(value);
51 if ((parsed.intValue() < min) || (parsed.intValue() > max))
52 throw new BuildException(tag + " out if range [" + min + "-" + max + "]");
53 args.addElement(switch_ + value);
54 } catch (NumberFormatException numberFormatException) {
55 new BuildException(tag + " is'nt a numeric");
56 }
57 }
58 private void parseMatchfinder(String value, Vector args) throws BuildException {
59 if (value == null) return;
60 for (int i=0; i<MATCH_FINDERS.length; i++) {
61 if (MATCH_FINDERS[i].equalsIgnoreCase(value)) {
62 args.addElement("-mf" + value);
63 return;
64 }
65 }
66 throw new BuildException("unvalid match finder : " + value);
67 }
68 public void execute() {
69 this.log("LZMA# 4.12 beta Copyright (c) 1999-2004 Igor Pavlov 2004-12-10");
70 this.log("JAVA LZMA Encoder 4.12 myspace");
71 long timestamp = System.currentTimeMillis();
72 Vector vargs = new Vector();
73 if (this.task == NONE) throw new BuildException("work requested.");
74 vargs.addElement(TASKS[this.task].substring(0, 1));
75 if (this.input == null) throw new BuildException("input requested.");
76 if (this.output == null) throw new BuildException("output requested.");
77 if (!new File(this.input).exists()) throw new BuildException("input does'nt exists.");
78 this.parseInt(this.a, "mode", "-a", 0, 2, vargs);
79 this.parseInt(this.d, "dictionnary", "-d", 0, 28, vargs);
80 this.parseInt(this.fb, "fastbytes", "-fb", 5, 255, vargs);
81 this.parseInt(this.lc, "litteralcontextbits", "-lc", 0, 8, vargs);
82 this.parseInt(this.lp, "litteralposbits", "-lp", 0, 4, vargs);
83 this.parseInt(this.pb, "posbits", "-pb", 0, 4, vargs);
84 this.parseMatchfinder(this.mf, vargs);
85 if (this.eos != null) vargs.addElement("-eos");
86 vargs.addElement(this.input);
87 vargs.addElement(this.output);
88 String[] args = new String[vargs.size()];
89 //String[] args = new String[vargs.size()+1];
90 for (int arg=0; arg<vargs.size(); arg++) {
91 args[arg] = (String)vargs.elementAt(arg);
92 }
93 //args[vargs.size()] = "-mx=9";
94 try {
95 this.log(vargs.toString());
96 LzmaAlone.main2(args);
97 long duration = System.currentTimeMillis() - timestamp;
98 if (duration > 9999) {
99 this.log("time : " + (duration / 1000) + " s");
100 } else {
101 this.log("time : " + duration + " ms");
102 }
103
104 } catch (Exception exception) {
105 throw new BuildException(exception);
106 }
107 }
108 public void setTask(String task) {
109 for (int i=0; i<TASKS.length; i++) {
110 if (task.trim().equalsIgnoreCase(TASKS[i])) this.task = i;
111 }
112 }
113 public void setOutput(String output) {
114 this.output = output.trim();
115 }
116 public void setInput(String input) {
117 this.input = input.trim();
118 }
119 public void setMode(String mode) {
120 this.a = mode.trim();
121 }
122 public void setDictionnary(String dictionnary) {
123 this.d = dictionnary.trim();
124 }
125 public void setFastbytes(String fastbytes) {
126 this.fb = fastbytes.trim();
127 }
128 public void setLiteralcontextbits(String literalcontextbits) {
129 this.lc = literalcontextbits;
130 }
131 public void setLiteralposbits(String literalposbits) {
132 this.lp = literalposbits.trim();
133 }
134 public void setPosbits(String posbits) {
135 this.pb = posbits.trim();
136 }
137 public void setMatchfinder(String matchfinder) {
138 this.mf = matchfinder.trim();
139 }
140 public void setEndofstreammarker(String endofstreammarker) {
141 this.eos = "true";
142 }
143}
Note: See TracBrowser for help on using the repository browser.