0

I have a simple Node app with a function that downloads a WordPress theme zip file and console logs the contents of the theme.json file.

const StreamZip = require('node-stream-zip');
var AdmZip = require("adm-zip");
const axios = require('axios');

const f = async () => {
    const url = 'https://downloads.wordpress.org/theme/twentytwentytwo.1.2.zip';
    const body = await axios.get(url, {
        responseType: 'arraybuffer',
    });
    var zip = new AdmZip(body.data);
    var zipEntries = zip.getEntries(); // an array of ZipEntry records

    zipEntries.forEach(function (zipEntry) {
        if (zipEntry.entryName == "twentytwentytwo/theme.json") {
            const tp = zipEntry.getData();
            const tmp = Buffer.from(tp, 'base64').toString('utf8')
            console.log(tmp);
        }
    });
};
f();

However, as can be seen in the output below there are some unexpected characters included. I'm not sure what these are, or how to remove them from the output.

ä
        "version": 2,
        "customTemplates": Ä
                ä
                        "name": "blank",
                        "title": "Blank",
                        "postTypes": Ä
                           "page",
                           "post"
                        Ü
                ü,
                ä
                        "name": "page-large-header",
                        "title": "Page (Large Header)",
                        "postTypes": Ä
                           "page"
                        Ü
                ü,
                ä
                        "name": "single-no-separators",
                        "title": "Single Post (No Separators)",
                        "postTypes": Ä
                           "post"
                        Ü
                ü,
                ä
                        "name": "page-no-separators",
                        "title": "Page (No Separators)",
                        "postTypes": Ä
                           "page"
                        Ü
                ü
        Ü,
VLAZ
  • 26,331
  • 9
  • 49
  • 67
dgwyer
  • 739
  • 8
  • 19
  • `zipEntry.getData().toString('utf8')` from https://stackoverflow.com/a/36406671/3807365 might work – IT goldman Jul 12 '22 at 12:28
  • Where are you running this exactly? Powershell? How is the node file saved? As UTF-8? I tried to reproduce this but saving the node file as ISO still resulted in the proper JSON reponse. (to be clear, where you are getting the umlauts you should see `{[`, `{`, `}` and `]}` so this is clearly a text encoding issue. –  Jul 12 '22 at 12:29
  • Also, if you do `fs.writeFileSync('theme.json', tmp);`, is the file contents also messed up? –  Jul 12 '22 at 12:32
  • Saving it to a file works, thanks. There are no formatting issues. BTW, I'm running the Node app via zsh on macOS. – dgwyer Jul 12 '22 at 17:02

0 Answers0