EBZip { public static void main(final String[] args) { Options options = new Options(); options.addOption("f", "force-overwrite", false, "force overwrite of output files"); options.addOption("n", "no-overwrite", false, "don't overwrite output files"); options.addOption("i", "information", false, "list information of compressed files"); options.addOption("k", "keep", false, "keep (don't delete) original files"); options.addOption("l", "level", true, "compression level; 0.." + EBZipConstants .EBZIP_MAX_LEVEL); options.addOption("o", "output-directory", true, "output files under DIRECTORY"); options.addOption("q", "quiet", false, "suppress all warnings"); options.addOption("s", "skip-content", true, "skip content; font, graphic, sound or movie"); options.addOption("S", "subbook", true, "target subbook"); options.addOption("z", "compress", false, "compress files"); options.addOption("u", "uncompress", false, "uncompress files"); options.addOption("t", "test", false, "only check for input files"); options.addOption("h", "help", false, "display this help and exit"); options.addOption("v", "version", false, "output version information and exit"); CommandLineParser parser = new PosixParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e) { System.err.println(PROGRAM + ": " + e.getMessage()); System.exit(1); } if (cmd.hasOption("f")) { overwrite = OVERWRITE_FORCE; } if (cmd.hasOption("n")) { overwrite = OVERWRITE_NO; } int action = ACTION_ZIP; if (cmd.hasOption("i")) { action = ACTION_INFO; } if (cmd.hasOption("z")) { action = ACTION_ZIP; } if (cmd.hasOption("u")) { action = ACTION_UNZIP; } keep = cmd.hasOption("k"); quiet = cmd.hasOption("q"); test = cmd.hasOption("t"); if (cmd.hasOption("l")) { String optionValue = cmd.getOptionValue("l"); try { EBZip.level = Integer.parseInt(optionValue); } catch (NumberFormatException e) { System.err.println(PROGRAM + ": invalid compression level `" + optionValue + "'"); System.exit(1); } if (EBZip.level > EBZipConstants.EBZIP_MAX_LEVEL || EBZip.level < 0) { System.err.println(PROGRAM + ": invalid compression level `" + optionValue + "'"); System.exit(1); } } if (cmd.hasOption("o")) { outDir = cmd.getOptionValue("o"); } if (cmd.hasOption("s")) { String arg = cmd.getOptionValue("s"); StringTokenizer st = new StringTokenizer(arg, ","); while (st.hasMoreTokens()) { String skip = st.nextToken().trim().toLowerCase(Locale.ENGLISH); switch (skip) { case "font": skipFont = true; break; case "sound": skipSound = true; break; case "graphic": skipGraphic = true; break; case "movie": skipMovie = true; break; default: System.err.println(PROGRAM + ": invalid content name `" + skip + "'"); System.exit(1); } } } ArrayList<String> list = new ArrayList<>(4); if (cmd.hasOption("S")) { String arg = cmd.getOptionValue("S"); StringTokenizer st = new StringTokenizer(arg, ","); while (st.hasMoreTokens()) { list.add(st.nextToken().trim().toLowerCase(Locale.ENGLISH)); } } if (!list.isEmpty()) { subbooks = list.toArray(new String[list.size()]); } if (cmd.hasOption("h")) { _usage(options); System.exit(0); } if (cmd.hasOption("v")) { _version(); System.exit(0); } String[] paths = cmd.getArgs(); int len = ArrayUtils.getLength(paths); switch (len) { case 0: break; case 1: bookDir = paths[0]; break; default: System.err.println(PROGRAM + ": too many arguments"); _usage(); System.exit(1); } EBZip ebzip = new EBZip(); try { ebzip._exec(action); } catch (Exception e) { System.err.println(PROGRAM + ": " + e.getMessage()); } } private  EBZip(); static void main(final String[] args);  }
@Test public void testEBZip_info() throws Exception { String bookPath = new File(this.getClass().getResource("/data/epwing-zipped").getFile()) .getAbsolutePath(); String expected = "==> " + bookPath + "/test/DATA/HONMON.ebz <==\n" + "10240 -> 2337 bytes (22.8%, ebzip level 0 compression)\n" + "\n" + "==> " + bookPath + "/chimei/DATA/HONMON.ebz <==\n" + "10240 -> 2386 bytes (23.3%, ebzip level 0 compression)\n" + "\n" + "==> " + bookPath + "/CATALOGS <==\n" + "2048 bytes (not compressed)\n\n"; String[] args = {"-i", bookPath}; EBZip.main(args); assertEquals(outContent.toString("UTF-8"), expected); }
@Test public void testEBZip_compress() throws Exception { String bookPath = new File(this.getClass().getResource("/data/epwing").getFile()) .getAbsolutePath(); String outPath = Files.createTempDirectory("testEBZip_compress").toFile().getAbsolutePath(); String expected = "==> compress " + bookPath + "/test/DATA/HONMON <==\n" + "output to " + outPath + "/test/DATA/HONMON.ebz\n" + "completed (10240 / 10240 bytes)\n" + "10240 -> 326 bytes (3.2%)\n\n" + "==> compress " + bookPath + "/chimei/DATA/HONMON <==\n" + "output to " + outPath + "/chimei/DATA/HONMON.ebz\n" + "completed (10240 / 10240 bytes)\n" + "10240 -> 377 bytes (3.7%)\n\n" + "==> copy " + bookPath + "/CATALOGS <==\n" + "output to " + outPath + "/CATALOGS\n" + "completed (2048 / 2048 bytes)\n\n"; String[] args = {"-z", "-k", "-o", outPath, bookPath}; EBZip.main(args); assertEquals(outContent.toString("UTF-8"), expected); String checkPath = Files.createTempDirectory("testEBZip_compress_check").toFile().getAbsolutePath(); String[] args2 = {"-u", "-k", "-o", checkPath, outPath}; EBZip.main(args2); assertTrue(FileUtils2.contentEquals(new File(checkPath + "/test/DATA/HONMON"), new File(bookPath + "/test/DATA/HONMON"))); }
@Test public void testEBZip_uncompress() throws Exception { String bookPath = new File(this.getClass().getResource("/data/epwing-zipped").getFile()) .getAbsolutePath(); String outPath = Files.createTempDirectory("testEBZip_uncompress").toFile().getAbsolutePath(); String expectedPath = new File(this.getClass().getResource("/data/epwing").getFile()) .getAbsolutePath(); String expected = "==> uncompress " + bookPath + "/test/DATA/HONMON.ebz <==\n" + "output to " + outPath + "/test/DATA/HONMON\n" + "completed (10240 / 10240 bytes)\n" + "2337 -> 10240 bytes\n\n" + "==> uncompress " + bookPath + "/chimei/DATA/HONMON.ebz <==\n" + "output to " + outPath + "/chimei/DATA/HONMON\n" + "completed (10240 / 10240 bytes)\n" + "2386 -> 10240 bytes\n\n" + "==> copy " + bookPath + "/CATALOGS <==\n" + "output to " + outPath + "/CATALOGS\n" + "completed (2048 / 2048 bytes)\n\n"; String[] args = {"-u", "-k", "-o", outPath, bookPath}; EBZip.main(args); assertEquals(outContent.toString("UTF-8"), expected); assertTrue(FileUtils2.contentEquals(new File(outPath + "/test/DATA/HONMON"), new File(expectedPath + "/test/DATA/HONMON"))); }