Skip to content

Instantly share code, notes, and snippets.

@Amice13
Last active August 31, 2024 13:00
Show Gist options
  • Select an option

  • Save Amice13/a7ffc3b1e43d14861c97eb937128decb to your computer and use it in GitHub Desktop.

Select an option

Save Amice13/a7ffc3b1e43d14861c97eb937128decb to your computer and use it in GitHub Desktop.

Revisions

  1. Amice13 revised this gist Aug 31, 2024. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions extractFilesFromFB2.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    const fs = require('fs')
    const path = require('path')

    const source = 'filename.fb2'
    const targetFolder = 'result'

    @@ -9,7 +10,8 @@ if (!fs.existsSync('result')) fs.mkdirSync(targetFolder)
    const pattern = /<binary[^>]*?id="([^"]*)"[^>]*?>(.*?)<\/binary>/gs

    const matches = xml.matchAll(pattern)
    for (let match of matches) {
    for (const match of matches) {
    const [_, filename, content] = [...match]
    fs.writeFileSync(path.join(targetFolder, filename), Buffer.from(content, 'base64'))
    const filePath = path.join(targetFolder, filename)
    fs.writeFileSync(filePath, Buffer.from(content, 'base64'))
    }
  2. Amice13 created this gist Aug 31, 2024.
    15 changes: 15 additions & 0 deletions extractFilesFromFB2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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 (let match of matches) {
    const [_, filename, content] = [...match]
    fs.writeFileSync(path.join(targetFolder, filename), Buffer.from(content, 'base64'))
    }