From 6a690e6ea84bda19cc6e75d9cb8718443858b248 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 18 Mar 2023 18:37:53 +0800 Subject: [PATCH] feat: set book name --- src/main.java | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/main.java b/src/main.java index 78d8817..223723f 100644 --- a/src/main.java +++ b/src/main.java @@ -1,21 +1,36 @@ import mobimeta.*; import java.io.File; +import java.util.Arrays; +import java.util.Iterator; import java.util.List; class Main { static String splitLine = "================================================================"; - public static void showMeta(File file) { - System.out.println(splitLine); - System.out.println("File name: " + file.getName()); - MobiMeta meta; + private static MobiMeta readFile(File file) { + MobiMeta meta = null; try { meta = new MobiMeta(file); } catch (MobiMetaException err) { - System.err.println("Error: " + err.getMessage()); - return; + System.err.println("Error reading file: " + err.getMessage()); + System.exit(1); } + return meta; + } + + private static void saveFile(MobiMeta meta, File file) { + try { + meta.saveToNewFile(file); + } catch (MobiMetaException err) { + System.err.println("Error saving file: " + err.getMessage()); + } + } + + private static void showMeta(File file) { + System.out.println(splitLine); + System.out.println("File name: " + file.getName()); + MobiMeta meta = readFile(file); String encoding = meta.getCharacterEncoding(); List exthList = meta.getEXTHRecords(); System.out.println("Book name: " + meta.getFullName()); @@ -48,13 +63,35 @@ class Main { System.out.println(splitLine); } + private static void setBookName(MobiMeta meta, String name) { + String encoding = meta.getCharacterEncoding(); + List exthList = meta.getEXTHRecords(); + boolean flag = false; + for (EXTHRecord rec : exthList) { + if (rec.getRecordType() == 503) { + rec.setData(name, encoding); + flag = true; + } + } + if (!flag) { // without updated title + exthList.add(new EXTHRecord(503, name, encoding)); + } + meta.setFullName(name); + meta.setEXTHRecords(); + } + public static void main(String[] args) { String testFile = "XXRS.azw3"; File target = new File(testFile); - showMeta(target); +// MobiMeta meta = readFile(target); +// setBookName(meta, "生若栩栩"); +// showMeta(target); + +// saveFile(meta, new File("XXRS_new.azw3")); + } }