[17387] | 1 | package SevenZip;
|
---|
| 2 |
|
---|
| 3 | public class LzmaAlone
|
---|
| 4 | {
|
---|
| 5 | static public class CommandLine
|
---|
| 6 | {
|
---|
| 7 | public static final int kEncode = 0;
|
---|
| 8 | public static final int kDecode = 1;
|
---|
| 9 | public static final int kBenchmak = 2;
|
---|
| 10 |
|
---|
| 11 | public int Command = -1;
|
---|
| 12 | public int NumBenchmarkPasses = 10;
|
---|
| 13 |
|
---|
| 14 | public int DictionarySize = 1 << 23;
|
---|
| 15 | public boolean DictionarySizeIsDefined = false;
|
---|
| 16 |
|
---|
| 17 | public int Lc = 3;
|
---|
| 18 | public int Lp = 0;
|
---|
| 19 | public int Pb = 2;
|
---|
| 20 |
|
---|
| 21 | public int Fb = 128;
|
---|
| 22 | public boolean FbIsDefined = false;
|
---|
| 23 |
|
---|
| 24 | public boolean Eos = false;
|
---|
| 25 |
|
---|
| 26 | public int Algorithm = 2;
|
---|
| 27 | public int MatchFinder = 1;
|
---|
| 28 |
|
---|
| 29 | public String InFile;
|
---|
| 30 | public String OutFile;
|
---|
| 31 |
|
---|
| 32 | boolean ParseSwitch(String s)
|
---|
| 33 | {
|
---|
| 34 | if (s.startsWith("d"))
|
---|
| 35 | {
|
---|
| 36 | DictionarySize = 1 << Integer.parseInt(s.substring(1));
|
---|
| 37 | DictionarySizeIsDefined = true;
|
---|
| 38 | }
|
---|
| 39 | else if (s.startsWith("fb"))
|
---|
| 40 | {
|
---|
| 41 | Fb = Integer.parseInt(s.substring(2));
|
---|
| 42 | FbIsDefined = true;
|
---|
| 43 | }
|
---|
| 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"))
|
---|
| 53 | Eos = true;
|
---|
| 54 | else if (s.startsWith("mf"))
|
---|
| 55 | {
|
---|
| 56 | String mfs = s.substring(2);
|
---|
| 57 | if (mfs.equals("bt2"))
|
---|
| 58 | MatchFinder = 0;
|
---|
| 59 | else if (mfs.equals("bt4"))
|
---|
| 60 | MatchFinder = 1;
|
---|
| 61 | else if (mfs.equals("bt4b"))
|
---|
| 62 | MatchFinder = 2;
|
---|
| 63 | else
|
---|
| 64 | return false;
|
---|
| 65 | }
|
---|
| 66 | else
|
---|
| 67 | return false;
|
---|
| 68 | return true;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public boolean Parse(String[] args) throws Exception
|
---|
| 72 | {
|
---|
| 73 | int pos = 0;
|
---|
| 74 | boolean switchMode = true;
|
---|
| 75 | for (int i = 0; i < args.length; i++)
|
---|
| 76 | {
|
---|
| 77 | String s = args[i];
|
---|
| 78 | if (s.length() == 0)
|
---|
| 79 | return false;
|
---|
| 80 | if (switchMode)
|
---|
| 81 | {
|
---|
| 82 | if (s.compareTo("--") == 0)
|
---|
| 83 | {
|
---|
| 84 | switchMode = false;
|
---|
| 85 | continue;
|
---|
| 86 | }
|
---|
| 87 | if (s.charAt(0) == '-')
|
---|
| 88 | {
|
---|
| 89 | String sw = s.substring(1).toLowerCase();
|
---|
| 90 | if (sw.length() == 0)
|
---|
| 91 | return false;
|
---|
| 92 | try
|
---|
| 93 | {
|
---|
| 94 | if (!ParseSwitch(sw))
|
---|
| 95 | return false;
|
---|
| 96 | }
|
---|
| 97 | catch (NumberFormatException e)
|
---|
| 98 | {
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 | continue;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | if (pos == 0)
|
---|
| 105 | {
|
---|
| 106 | if (s.equalsIgnoreCase("e"))
|
---|
| 107 | Command = kEncode;
|
---|
| 108 | else if (s.equalsIgnoreCase("d"))
|
---|
| 109 | Command = kDecode;
|
---|
| 110 | else if (s.equalsIgnoreCase("b"))
|
---|
| 111 | Command = kBenchmak;
|
---|
| 112 | else
|
---|
| 113 | return false;
|
---|
| 114 | }
|
---|
| 115 | else if(pos == 1)
|
---|
| 116 | {
|
---|
| 117 | if (Command == kBenchmak)
|
---|
| 118 | {
|
---|
| 119 | try
|
---|
| 120 | {
|
---|
| 121 | NumBenchmarkPasses = Integer.parseInt(s);
|
---|
| 122 | if (NumBenchmarkPasses < 1)
|
---|
| 123 | return false;
|
---|
| 124 | }
|
---|
| 125 | catch (NumberFormatException e)
|
---|
| 126 | {
|
---|
| 127 | return false;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | else
|
---|
| 131 | InFile = s;
|
---|
| 132 | }
|
---|
| 133 | else if(pos == 2)
|
---|
| 134 | OutFile = s;
|
---|
| 135 | else
|
---|
| 136 | return false;
|
---|
| 137 | pos++;
|
---|
| 138 | continue;
|
---|
| 139 | }
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 | static void PrintHelp()
|
---|
| 146 | {
|
---|
| 147 | System.out.println(
|
---|
| 148 | "\nUsage: LZMA <e|d> [<switches>...] inputFile outputFile\n" +
|
---|
| 149 | " e: encode file\n" +
|
---|
| 150 | " d: decode file\n" +
|
---|
| 151 | " b: Benchmark\n" +
|
---|
| 152 | "<Switches>\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"
|
---|
| 161 | );
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | public static void main2(String[] args) throws Exception {
|
---|
| 165 | main( args );
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public static void main(String[] args) throws Exception
|
---|
| 169 | {
|
---|
| 170 | System.out.println("\nLZMA (Java) 4.42 Copyright (c) 1999-2006 Igor Pavlov 2006-05-15\n");
|
---|
| 171 |
|
---|
| 172 | if (args.length < 1)
|
---|
| 173 | {
|
---|
| 174 | PrintHelp();
|
---|
| 175 | return;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | CommandLine params = new CommandLine();
|
---|
| 179 | if (!params.Parse(args))
|
---|
| 180 | {
|
---|
| 181 | System.out.println("\nIncorrect command");
|
---|
| 182 | return;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | if (params.Command == CommandLine.kBenchmak)
|
---|
| 186 | {
|
---|
| 187 | int dictionary = (1 << 21);
|
---|
| 188 | if (params.DictionarySizeIsDefined)
|
---|
| 189 | dictionary = params.DictionarySize;
|
---|
| 190 | if (params.MatchFinder > 1)
|
---|
| 191 | throw new Exception("Unsupported match finder");
|
---|
| 192 | SevenZip.LzmaBench.LzmaBenchmark(params.NumBenchmarkPasses, dictionary);
|
---|
| 193 | }
|
---|
| 194 | else if (params.Command == CommandLine.kEncode || params.Command == CommandLine.kDecode)
|
---|
| 195 | {
|
---|
| 196 | java.io.File inFile = new java.io.File(params.InFile);
|
---|
| 197 | java.io.File outFile = new java.io.File(params.OutFile);
|
---|
| 198 |
|
---|
| 199 | java.io.BufferedInputStream inStream = new java.io.BufferedInputStream(new java.io.FileInputStream(inFile));
|
---|
| 200 | java.io.BufferedOutputStream outStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(outFile));
|
---|
| 201 |
|
---|
| 202 | boolean eos = false;
|
---|
| 203 | if (params.Eos)
|
---|
| 204 | eos = true;
|
---|
| 205 | if (params.Command == CommandLine.kEncode)
|
---|
| 206 | {
|
---|
| 207 | SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
|
---|
| 208 | if (!encoder.SetAlgorithm(params.Algorithm))
|
---|
| 209 | throw new Exception("Incorrect compression mode");
|
---|
| 210 | if (!encoder.SetDictionarySize(params.DictionarySize))
|
---|
| 211 | throw new Exception("Incorrect dictionary size");
|
---|
| 212 | if (!encoder.SeNumFastBytes(params.Fb))
|
---|
| 213 | throw new Exception("Incorrect -fb value");
|
---|
| 214 | if (!encoder.SetMatchFinder(params.MatchFinder))
|
---|
| 215 | throw new Exception("Incorrect -mf value");
|
---|
| 216 | if (!encoder.SetLcLpPb(params.Lc, params.Lp, params.Pb))
|
---|
| 217 | throw new Exception("Incorrect -lc or -lp or -pb value");
|
---|
| 218 | encoder.SetEndMarkerMode(eos);
|
---|
| 219 | encoder.WriteCoderProperties(outStream);
|
---|
| 220 | long fileSize;
|
---|
| 221 | if (eos)
|
---|
| 222 | fileSize = -1;
|
---|
| 223 | else
|
---|
| 224 | fileSize = inFile.length();
|
---|
| 225 | for (int i = 0; i < 8; i++)
|
---|
| 226 | outStream.write((int)(fileSize >>> (8 * i)) & 0xFF);
|
---|
| 227 | encoder.Code(inStream, outStream, -1, -1, null);
|
---|
| 228 | }
|
---|
| 229 | else
|
---|
| 230 | {
|
---|
| 231 | int propertiesSize = 5;
|
---|
| 232 | byte[] properties = new byte[propertiesSize];
|
---|
| 233 | if (inStream.read(properties, 0, propertiesSize) != propertiesSize)
|
---|
| 234 | throw new Exception("input .lzma file is too short");
|
---|
| 235 | SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
|
---|
| 236 | if (!decoder.SetDecoderProperties(properties))
|
---|
| 237 | throw new Exception("Incorrect stream properties");
|
---|
| 238 | long outSize = 0;
|
---|
| 239 | for (int i = 0; i < 8; i++)
|
---|
| 240 | {
|
---|
| 241 | int v = inStream.read();
|
---|
| 242 | if (v < 0)
|
---|
| 243 | throw new Exception("Can't read stream size");
|
---|
| 244 | outSize |= ((long)v) << (8 * i);
|
---|
| 245 | }
|
---|
| 246 | if (!decoder.Code(inStream, outStream, outSize))
|
---|
| 247 | throw new Exception("Error in data stream");
|
---|
| 248 | }
|
---|
| 249 | outStream.flush();
|
---|
| 250 | outStream.close();
|
---|
| 251 | inStream.close();
|
---|
| 252 | }
|
---|
| 253 | else
|
---|
| 254 | throw new Exception("Incorrect command");
|
---|
| 255 | return;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|