Skip to content

Instantly share code, notes, and snippets.

@Amice13
Last active August 31, 2024 13:00
Show Gist options
  • Save Amice13/a7ffc3b1e43d14861c97eb937128decb to your computer and use it in GitHub Desktop.
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
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