Last active
August 31, 2024 13:00
-
-
Save Amice13/a7ffc3b1e43d14861c97eb937128decb to your computer and use it in GitHub Desktop.
This script extracts all binary files from fb2 book and stores them in a dedicated folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
const source = 'filename.fb2' | |
const targetFolder = 'result' | |
const xml = fs.readFileSync(source).toString() | |
if (!fs.existsSync('result')) fs.mkdirSync(targetFolder) | |
const pattern = /<binary[^>]*?id="([^"]*)"[^>]*?>(.*?)<\/binary>/gs | |
const matches = xml.matchAll(pattern) | |
for (const match of matches) { | |
const [_, filename, content] = [...match] | |
const filePath = path.join(targetFolder, filename) | |
fs.writeFileSync(filePath, Buffer.from(content, 'base64')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment