5 static public class CommandLine
7 public static final int kEncode = 0;
8 public static final int kDecode = 1;
9 public static final int kBenchmak = 2;
11 public int Command = -1;
12 public int NumBenchmarkPasses = 10;
14 public int DictionarySize = 1 << 23;
15 public boolean DictionarySizeIsDefined = false;
22 public boolean FbIsDefined = false;
24 public boolean Eos = false;
26 public int Algorithm = 2;
27 public int MatchFinder = 1;
30 public String OutFile;
32 boolean ParseSwitch(String s)
34 if (s.startsWith("d"))
36 DictionarySize = 1 << Integer.parseInt(s.substring(1));
37 DictionarySizeIsDefined = true;
39 else if (s.startsWith("fb"))
41 Fb = Integer.parseInt(s.substring(2));
44 else if (s.startsWith("a"))
45 Algorithm = Integer.parseInt(s.substring(1));
46 else if (s.startsWith("lc"))
47 Lc = Integer.parseInt(s.substring(2));
48 else if (s.startsWith("lp"))
49 Lp = Integer.parseInt(s.substring(2));
50 else if (s.startsWith("pb"))
51 Pb = Integer.parseInt(s.substring(2));
52 else if (s.startsWith("eos"))
54 else if (s.startsWith("mf"))
56 String mfs = s.substring(2);
57 if (mfs.equals("bt2"))
59 else if (mfs.equals("bt4"))
61 else if (mfs.equals("bt4b"))
71 public boolean Parse(String[] args) throws Exception
74 boolean switchMode = true;
75 for (int i = 0; i < args.length; i++)
82 if (s.compareTo("--") == 0)
87 if (s.charAt(0) == '-')
89 String sw = s.substring(1).toLowerCase();
97 catch (NumberFormatException e)
106 if (s.equalsIgnoreCase("e"))
108 else if (s.equalsIgnoreCase("d"))
110 else if (s.equalsIgnoreCase("b"))
117 if (Command == kBenchmak)
121 NumBenchmarkPasses = Integer.parseInt(s);
122 if (NumBenchmarkPasses < 1)
125 catch (NumberFormatException e)
145 static void PrintHelp()
148 "\nUsage: LZMA <e|d> [<switches>...] inputFile outputFile\n" +
149 " e: encode file\n" +
150 " d: decode file\n" +
153 // " -a{N}: set compression mode - [0, 1], default: 1 (max)\n" +
154 " -d{N}: set dictionary - [0,28], default: 23 (8MB)\n" +
155 " -fb{N}: set number of fast bytes - [5, 273], default: 128\n" +
156 " -lc{N}: set number of literal context bits - [0, 8], default: 3\n" +
157 " -lp{N}: set number of literal pos bits - [0, 4], default: 0\n" +
158 " -pb{N}: set number of pos bits - [0, 4], default: 2\n" +
159 " -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4\n" +
160 " -eos: write End Of Stream marker\n"
164 public static void main(String[] args) throws Exception
166 System.out.println("\nLZMA (Java) 4.42 Copyright (c) 1999-2006 Igor Pavlov 2006-05-15\n");
174 CommandLine params = new CommandLine();
175 if (!params.Parse(args))
177 System.out.println("\nIncorrect command");
181 if (params.Command == CommandLine.kBenchmak)
183 int dictionary = (1 << 21);
184 if (params.DictionarySizeIsDefined)
185 dictionary = params.DictionarySize;
186 if (params.MatchFinder > 1)
187 throw new Exception("Unsupported match finder");
188 SevenZip.LzmaBench.LzmaBenchmark(params.NumBenchmarkPasses, dictionary);
190 else if (params.Command == CommandLine.kEncode || params.Command == CommandLine.kDecode)
192 java.io.File inFile = new java.io.File(params.InFile);
193 java.io.File outFile = new java.io.File(params.OutFile);
195 java.io.BufferedInputStream inStream = new java.io.BufferedInputStream(new java.io.FileInputStream(inFile));
196 java.io.BufferedOutputStream outStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(outFile));
201 if (params.Command == CommandLine.kEncode)
203 SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
204 if (!encoder.SetAlgorithm(params.Algorithm))
205 throw new Exception("Incorrect compression mode");
206 if (!encoder.SetDictionarySize(params.DictionarySize))
207 throw new Exception("Incorrect dictionary size");
208 if (!encoder.SeNumFastBytes(params.Fb))
209 throw new Exception("Incorrect -fb value");
210 if (!encoder.SetMatchFinder(params.MatchFinder))
211 throw new Exception("Incorrect -mf value");
212 if (!encoder.SetLcLpPb(params.Lc, params.Lp, params.Pb))
213 throw new Exception("Incorrect -lc or -lp or -pb value");
214 encoder.SetEndMarkerMode(eos);
215 encoder.WriteCoderProperties(outStream);
220 fileSize = inFile.length();
221 for (int i = 0; i < 8; i++)
222 outStream.write((int)(fileSize >>> (8 * i)) & 0xFF);
223 encoder.Code(inStream, outStream, -1, -1, null);
227 int propertiesSize = 5;
228 byte[] properties = new byte[propertiesSize];
229 if (inStream.read(properties, 0, propertiesSize) != propertiesSize)
230 throw new Exception("input .lzma file is too short");
231 SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
232 if (!decoder.SetDecoderProperties(properties))
233 throw new Exception("Incorrect stream properties");
235 for (int i = 0; i < 8; i++)
237 int v = inStream.read();
239 throw new Exception("Can't read stream size");
240 outSize |= ((long)v) << (8 * i);
242 if (!decoder.Code(inStream, outStream, outSize))
243 throw new Exception("Error in data stream");
250 throw new Exception("Incorrect command");