From a7b2d34b99b11fd6f7709f1bde33b4aa9a320aae Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 18 Mar 2023 18:46:31 +0800 Subject: [PATCH] feat: support edit author and ISBN field --- src/main.java | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/main.java b/src/main.java index 223723f..cffbba3 100644 --- a/src/main.java +++ b/src/main.java @@ -20,6 +20,7 @@ class Main { } private static void saveFile(MobiMeta meta, File file) { + meta.setEXTHRecords(); try { meta.saveToNewFile(file); } catch (MobiMetaException err) { @@ -73,24 +74,58 @@ class Main { flag = true; } } - if (!flag) { // without updated title + if (!flag) { // without `updated title` field exthList.add(new EXTHRecord(503, name, encoding)); } meta.setFullName(name); - meta.setEXTHRecords(); } + private static void setAuthor(MobiMeta meta, String author) { + String encoding = meta.getCharacterEncoding(); + List exthList = meta.getEXTHRecords(); + boolean flag = false; + for (EXTHRecord rec : exthList) { + if (rec.getRecordType() == 100) { + rec.setData(author, encoding); + flag = true; + } + } + if (!flag) { // without `author` field + exthList.add(new EXTHRecord(100, author, encoding)); + } + } + + private static void setISBN(MobiMeta meta, String isbn) { + String encoding = meta.getCharacterEncoding(); + List exthList = meta.getEXTHRecords(); + boolean flag = false; + for (EXTHRecord rec : exthList) { + if (rec.getRecordType() == 104) { + rec.setData(isbn, encoding); + flag = true; + } + } + if (!flag) { // without `ISBN` field + exthList.add(new EXTHRecord(104, isbn, encoding)); + } + } + +// private static void setASIN(MobiMeta meta, String asin) { +// +// } + public static void main(String[] args) { - String testFile = "XXRS.azw3"; +// String testFile = "XXRS.azw3"; + String testFile = "XXRS_new.azw3"; File target = new File(testFile); showMeta(target); // MobiMeta meta = readFile(target); // setBookName(meta, "生若栩栩"); -// showMeta(target); - +// setAuthor(meta, "大叙"); +// setISBN(meta, "666"); // saveFile(meta, new File("XXRS_new.azw3")); }