001: package SevenZip;
002:
003: public class LzmaAlone {
004: static public class CommandLine {
005: public static final int kEncode = 0;
006: public static final int kDecode = 1;
007: public static final int kBenchmak = 2;
008:
009: public int Command = -1;
010: public int NumBenchmarkPasses = 10;
011:
012: public int DictionarySize = 1 << 23;
013: public boolean DictionarySizeIsDefined = false;
014:
015: public int Lc = 3;
016: public int Lp = 0;
017: public int Pb = 2;
018:
019: public int Fb = 128;
020: public boolean FbIsDefined = false;
021:
022: public boolean Eos = false;
023:
024: public int Algorithm = 2;
025: public int MatchFinder = 1;
026:
027: public String InFile;
028: public String OutFile;
029:
030: boolean ParseSwitch(String s) {
031: if (s.startsWith("d")) {
032: DictionarySize = 1 << Integer.parseInt(s.substring(1));
033: DictionarySizeIsDefined = true;
034: } else if (s.startsWith("fb")) {
035: Fb = Integer.parseInt(s.substring(2));
036: FbIsDefined = true;
037: } else if (s.startsWith("a"))
038: Algorithm = Integer.parseInt(s.substring(1));
039: else if (s.startsWith("lc"))
040: Lc = Integer.parseInt(s.substring(2));
041: else if (s.startsWith("lp"))
042: Lp = Integer.parseInt(s.substring(2));
043: else if (s.startsWith("pb"))
044: Pb = Integer.parseInt(s.substring(2));
045: else if (s.startsWith("eos"))
046: Eos = true;
047: else if (s.startsWith("mf")) {
048: String mfs = s.substring(2);
049: if (mfs.equals("bt2"))
050: MatchFinder = 0;
051: else if (mfs.equals("bt4"))
052: MatchFinder = 1;
053: else if (mfs.equals("bt4b"))
054: MatchFinder = 2;
055: else
056: return false;
057: } else
058: return false;
059: return true;
060: }
061:
062: public boolean Parse(String[] args) throws Exception {
063: int pos = 0;
064: boolean switchMode = true;
065: for (int i = 0; i < args.length; i++) {
066: String s = args[i];
067: if (s.length() == 0)
068: return false;
069: if (switchMode) {
070: if (s.compareTo("--") == 0) {
071: switchMode = false;
072: continue;
073: }
074: if (s.charAt(0) == '-') {
075: String sw = s.substring(1).toLowerCase();
076: if (sw.length() == 0)
077: return false;
078: try {
079: if (!ParseSwitch(sw))
080: return false;
081: } catch (NumberFormatException e) {
082: return false;
083: }
084: continue;
085: }
086: }
087: if (pos == 0) {
088: if (s.equalsIgnoreCase("e"))
089: Command = kEncode;
090: else if (s.equalsIgnoreCase("d"))
091: Command = kDecode;
092: else if (s.equalsIgnoreCase("b"))
093: Command = kBenchmak;
094: else
095: return false;
096: } else if (pos == 1) {
097: if (Command == kBenchmak) {
098: try {
099: NumBenchmarkPasses = Integer.parseInt(s);
100: if (NumBenchmarkPasses < 1)
101: return false;
102: } catch (NumberFormatException e) {
103: return false;
104: }
105: } else
106: InFile = s;
107: } else if (pos == 2)
108: OutFile = s;
109: else
110: return false;
111: pos++;
112: continue;
113: }
114: return true;
115: }
116: }
117:
118: static void PrintHelp() {
119: System.out
120: .println("\nUsage: LZMA <e|d> [<switches>...] inputFile outputFile\n"
121: + " e: encode file\n"
122: + " d: decode file\n"
123: + " b: Benchmark\n"
124: + "<Switches>\n"
125: +
126: // " -a{N}: set compression mode - [0, 1], default: 1 (max)\n" +
127: " -d{N}: set dictionary - [0,28], default: 23 (8MB)\n"
128: + " -fb{N}: set number of fast bytes - [5, 273], default: 128\n"
129: + " -lc{N}: set number of literal context bits - [0, 8], default: 3\n"
130: + " -lp{N}: set number of literal pos bits - [0, 4], default: 0\n"
131: + " -pb{N}: set number of pos bits - [0, 4], default: 2\n"
132: + " -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4\n"
133: + " -eos: write End Of Stream marker\n");
134: }
135:
136: public static void main(String[] args) throws Exception {
137: System.out
138: .println("\nLZMA (Java) 4.42 Copyright (c) 1999-2006 Igor Pavlov 2006-05-15\n");
139:
140: if (args.length < 1) {
141: PrintHelp();
142: return;
143: }
144:
145: CommandLine params = new CommandLine();
146: if (!params.Parse(args)) {
147: System.out.println("\nIncorrect command");
148: return;
149: }
150:
151: if (params.Command == CommandLine.kBenchmak) {
152: int dictionary = (1 << 21);
153: if (params.DictionarySizeIsDefined)
154: dictionary = params.DictionarySize;
155: if (params.MatchFinder > 1)
156: throw new Exception("Unsupported match finder");
157: SevenZip.LzmaBench.LzmaBenchmark(params.NumBenchmarkPasses,
158: dictionary);
159: } else if (params.Command == CommandLine.kEncode
160: || params.Command == CommandLine.kDecode) {
161: java.io.File inFile = new java.io.File(params.InFile);
162: java.io.File outFile = new java.io.File(params.OutFile);
163:
164: java.io.BufferedInputStream inStream = new java.io.BufferedInputStream(
165: new java.io.FileInputStream(inFile));
166: java.io.BufferedOutputStream outStream = new java.io.BufferedOutputStream(
167: new java.io.FileOutputStream(outFile));
168:
169: boolean eos = false;
170: if (params.Eos)
171: eos = true;
172: if (params.Command == CommandLine.kEncode) {
173: SevenZip.Compression.lzma.Encoder encoder = new SevenZip.Compression.lzma.Encoder();
174: if (!encoder.SetAlgorithm(params.Algorithm))
175: throw new Exception("Incorrect compression mode");
176: if (!encoder.SetDictionarySize(params.DictionarySize))
177: throw new Exception("Incorrect dictionary size");
178: if (!encoder.SeNumFastBytes(params.Fb))
179: throw new Exception("Incorrect -fb value");
180: if (!encoder.SetMatchFinder(params.MatchFinder))
181: throw new Exception("Incorrect -mf value");
182: if (!encoder.SetLcLpPb(params.Lc, params.Lp, params.Pb))
183: throw new Exception(
184: "Incorrect -lc or -lp or -pb value");
185: encoder.SetEndMarkerMode(eos);
186: encoder.WriteCoderProperties(outStream);
187: long fileSize;
188: if (eos)
189: fileSize = -1;
190: else
191: fileSize = inFile.length();
192: for (int i = 0; i < 8; i++)
193: outStream
194: .write((int) (fileSize >>> (8 * i)) & 0xFF);
195: encoder.Code(inStream, outStream, -1, -1, null);
196: } else {
197: int propertiesSize = 5;
198: byte[] properties = new byte[propertiesSize];
199: if (inStream.read(properties, 0, propertiesSize) != propertiesSize)
200: throw new Exception("input .lzma file is too short");
201: SevenZip.Compression.lzma.Decoder decoder = new SevenZip.Compression.lzma.Decoder();
202: if (!decoder.SetDecoderProperties(properties))
203: throw new Exception("Incorrect stream properties");
204: long outSize = 0;
205: for (int i = 0; i < 8; i++) {
206: int v = inStream.read();
207: if (v < 0)
208: throw new Exception("Can't read stream size");
209: outSize |= ((long) v) << (8 * i);
210: }
211: if (!decoder.Code(inStream, outStream, outSize))
212: throw new Exception("Error in data stream");
213: }
214: outStream.flush();
215: outStream.close();
216: inStream.close();
217: } else
218: throw new Exception("Incorrect command");
219: return;
220: }
221: }
|