OSDN Git Service

Move checkDictZipInputStream
[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,2022 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 org.dict.zip.DictZipFiles;
26 import org.dict.zip.DictZipHeader.CompressionLevel;
27
28 import java.io.File;
29 import java.util.Locale;
30 import java.util.ResourceBundle;
31 import java.io.IOException;
32
33 /**
34  * dictzip/dictunzip main class.
35  * @author Hiroshi Miura
36  */
37 public final class Main {
38     /**
39      * The localized strings are kept in a separate file.
40      */
41     private static ResourceBundle messages = ResourceBundle.getBundle(
42             "org/dict/zip/cli/Bundle", Locale.getDefault());
43
44     private static CommandLine commandLine = new CommandLine();
45
46     /**
47      * main method.
48      *
49      * @param argv command line argument
50      */
51     public static void main(final String[] argv) {
52         int res = commandLine.parse(argv);
53         // If error in command line, exit with code
54         if (res > 1) {
55             System.exit(res);
56         } else if (res == 1) {
57             // normal exit.
58             System.exit(0);
59         }
60         for (String fName: commandLine.getTargetFiles()) {
61             try {
62                 DictData dict;
63                 if (commandLine.options.isList()) {
64                     commandLine.options.setKeep(true);
65                     dict = new DictData(fName, null);
66                     dict.printHeader();
67                 } else if (commandLine.options.isTest()) {
68                     boolean result = false;
69                     try {
70                         result = DictZipFiles.checkDictZipFile(fName);
71                     } catch (IOException e) {
72                         System.err.println(e.getMessage());
73                         System.exit(2);
74                     }
75                     if (result) {
76                         System.exit(0);
77                     } else {
78                         System.err.println(messages.getString("main.test.error"));
79                         System.exit(1);
80                     }
81                 } else if (commandLine.options.isDecompress()) {
82                     String extractFile = DictZipUtils.uncompressedFileName(fName);
83                     long start = commandLine.options.getStart();
84                     int size = commandLine.options.getSize();
85                     dict = new DictData(extractFile, fName);
86                     dict.doUnzip(start, size);
87                 } else { // compression.
88                     String zippedFile = DictZipUtils.compressedFileName(fName);
89                     CompressionLevel level = commandLine.options.getLevel();
90                     dict = new DictData(fName, zippedFile);
91                     dict.doZip(level);
92                 }
93                 if (!commandLine.options.isKeep()) {
94                     File targetFile = new File(fName);
95                     if (!targetFile.delete()) {
96                         System.err.println(messages.getString("main.delete.error"));
97                         System.exit(2);
98                     }
99                 }
100             } catch (IOException ex) {
101                 System.err.println(messages.getString("main.io.error"));
102                 System.err.println(ex.getLocalizedMessage());
103                 System.exit(1);
104             }
105         }
106         System.exit(0);
107     }
108
109     private Main() {
110     }
111
112 }