• R/O
  • HTTP
  • SSH
  • HTTPS

dictzip-java: Commit


Commit MetaInfo

Revision3163cc42b8368a08f3b7eaa3cfca6f5dc3fcc71c (tree)
Zeit2016-04-12 09:23:08
AutorHiroshi Miura <miurahr@linu...>
CommiterHiroshi Miura

Log Message

Change multiple fies handle

Signed-off-by: Hiroshi Miura <miurahr@linux.com>

Ändern Zusammenfassung

Diff

--- a/dictzip-cli/src/main/java/org/dict/zip/cli/DictZipUtils.java
+++ b/dictzip-cli/src/main/java/org/dict/zip/cli/DictZipUtils.java
@@ -51,6 +51,11 @@ public final class DictZipUtils {
5151 return name + ".dz";
5252 }
5353
54+
55+ protected static String getString(final String key) {
56+ return AppConsts.RESOURCE_BUNDLE.getString(key);
57+ }
58+
5459 /**
5560 * Utility class should not be instantiated.
5661 */
--- a/dictzip-cli/src/main/java/org/dict/zip/cli/Main.java
+++ b/dictzip-cli/src/main/java/org/dict/zip/cli/Main.java
@@ -25,6 +25,8 @@ package org.dict.zip.cli;
2525 import org.dict.zip.DictZipHeader.CompressionLevel;
2626
2727 import java.io.File;
28+import java.text.MessageFormat;
29+import java.util.List;
2830 import java.util.Locale;
2931 import java.util.ResourceBundle;
3032 import java.io.IOException;
@@ -42,6 +44,48 @@ public final class Main {
4244
4345 private static CommandLine commandLine = new CommandLine();
4446
47+ private static void showHelp() {
48+ System.out.println(AppConsts.getNameAndVersion());
49+ System.out.println(MessageFormat.format(DictZipUtils.getString("help.copyright.template"),
50+ AppConsts.YEAR, AppConsts.AUTHORS));
51+ System.out.println();
52+ System.out.println(MessageFormat.format(DictZipUtils.getString("help.message"),
53+ AppConsts.NAME));
54+ }
55+
56+ private static void doList(String fName) throws IOException {
57+ DictData dict;
58+ dict = new DictData(fName, null);
59+ dict.printHeader();
60+ }
61+
62+ private static void doUnZip(String fName) throws IOException {
63+ DictData dict;
64+ String extractFile = DictZipUtils.uncompressedFileName(fName);
65+ long start = commandLine.options.getStart();
66+ int size = commandLine.options.getSize();
67+ dict = new DictData(extractFile, fName);
68+ dict.doUnzip(start, size);
69+ if (!commandLine.options.isKeep()) {
70+ File targetFile = new File(fName);
71+ if (!targetFile.delete()) {
72+ System.err.println(messages.getString("main.delete.error"));
73+ System.exit(2);
74+ }
75+ }
76+ if (!commandLine.options.isKeep()) {
77+ deleteTarget(fName);
78+ }
79+ }
80+
81+ private static void deleteTarget(String fName) {
82+ File targetFile = new File(fName);
83+ if (!targetFile.delete()) {
84+ System.err.println(messages.getString("main.delete.error"));
85+ System.exit(2);
86+ }
87+ }
88+
4589 /**
4690 * main method.
4791 *
@@ -53,37 +97,36 @@ public final class Main {
5397 if (res != 0) {
5498 System.exit(res);
5599 }
56- for (String fName: commandLine.getTargetFiles()) {
57- try {
58- DictData dict;
59- if (commandLine.options.isList()) {
60- commandLine.options.setKeep(true);
61- dict = new DictData(fName, null);
62- dict.printHeader();
63- } else if (commandLine.options.isDecompress()) {
64- String extractFile = DictZipUtils.uncompressedFileName(fName);
65- long start = commandLine.options.getStart();
66- int size = commandLine.options.getSize();
67- dict = new DictData(extractFile, fName);
68- dict.doUnzip(start, size);
69- } else { // compression.
70- String zippedFile = DictZipUtils.compressedFileName(fName);
71- CompressionLevel level = commandLine.options.getLevel();
100+ try {
101+ DictData dict;
102+ List<String> target = commandLine.getTargetFiles();
103+ if (commandLine.options.isList()) {
104+ if (target.size() != 1) {
105+ showHelp();
106+ System.exit(1);
107+ }
108+ doList(target.get(0));
109+ } else if (commandLine.options.isDecompress()) {
110+ if (target.size() != 1) {
111+ showHelp();
112+ System.exit(1);
113+ }
114+ doUnZip(target.get(0));
115+ } else { // compression.
116+ CompressionLevel level = commandLine.options.getLevel();
117+ String zippedFile = DictZipUtils.compressedFileName(target.get(0));
118+ for (String fName : target) {
72119 dict = new DictData(fName, zippedFile);
73120 dict.doZip(level);
74- }
75- if (!commandLine.options.isKeep()) {
76- File targetFile = new File(fName);
77- if (!targetFile.delete()) {
78- System.err.println(messages.getString("main.delete.error"));
79- System.exit(2);
121+ if (!commandLine.options.isKeep()) {
122+ deleteTarget(fName);
80123 }
81124 }
82- } catch (IOException ex) {
83- System.err.println(messages.getString("main.io.error"));
84- System.err.println(ex.getLocalizedMessage());
85- System.exit(1);
86125 }
126+ } catch (IOException ex) {
127+ System.err.println(messages.getString("main.io.error"));
128+ System.err.println(ex.getLocalizedMessage());
129+ System.exit(1);
87130 }
88131 System.exit(0);
89132 }
Show on old repository browser