OSDN Git Service

4f3ba7cf98d479f9fbae8a1a1da4db5d8221e51c
[dictzip-java/dictzip-java.git] / dictzip-cli / src / main / java / org / dict / zip / cli / Main.java
1 /*
2  * DictZip command line interface.
3  *
4  * This is a part of DictZip-java library.
5  *
6  * Copyright (C) 2016 Hiroshi Miura
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 package org.dict.zip.cli;
24
25 import java.io.File;
26 import java.util.Locale;
27 import java.util.ResourceBundle;
28 import java.io.IOException;
29
30 /**
31  * dictzip/dictunzip main class.
32  * @author Hiroshi Miura
33  */
34 public final class Main {
35     /**
36      * The localized strings are kept in a separate file.
37      */
38     private static ResourceBundle messages = ResourceBundle.getBundle(
39             "org/dict/zip/cli/Bundle", Locale.getDefault());
40
41     private static CommandLine commandLine = new CommandLine();
42
43     /**
44      * main method.
45      *
46      * @param argv command line argument
47      */
48     public static void main(final String[] argv) {
49         int res = commandLine.parse(argv);
50         // If error in command line, exit with code
51         if (res != 0) {
52             System.exit(res);
53         }
54         for (String fName: commandLine.getTargetFiles()) {
55             try {
56                 DictData dict;
57                 if (commandLine.options.isList()) {
58                     commandLine.options.setKeep(true);
59                     dict = new DictData(fName, null);
60                     dict.printHeader();
61                 } else if (commandLine.options.isDecompress()) {
62                     String extractFile = DictZipUtils.uncompressedFileName(fName);
63                     long start = commandLine.options.getStart();
64                     int size = commandLine.options.getSize();
65                     dict = new DictData(extractFile, fName);
66                     dict.doUnzip(start, size);
67                 } else { // compression.
68                     String zippedFile = DictZipUtils.compressedFileName(fName);
69                     dict = new DictData(fName, zippedFile);
70                     dict.doZip();
71                 }
72                 if (!commandLine.options.isKeep()) {
73                     File targetFile = new File(fName);
74                     if (!targetFile.delete()) {
75                         System.err.println(messages.getString("main.delete.error"));
76                         System.exit(2);
77                     }
78                 }
79             } catch (IOException ex) {
80                 System.err.println(messages.getString("main.io.error"));
81                 System.err.println(ex.getLocalizedMessage());
82                 System.exit(1);
83             }
84         }
85         System.exit(0);
86     }
87
88     private Main() {
89     }
90
91 }