Skip to content

Instantly share code, notes, and snippets.

@spemer
Created April 11, 2020 16:46
Show Gist options
  • Save spemer/50839a077109860a08b9173fbf621cb6 to your computer and use it in GitHub Desktop.
Save spemer/50839a077109860a08b9173fbf621cb6 to your computer and use it in GitHub Desktop.

Revisions

  1. spemer created this gist Apr 11, 2020.
    21 changes: 21 additions & 0 deletions compress.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    const fs = require("fs");
    const zlib = require("zlib");

    var dirs = ["../public/sitemap"];

    dirs.forEach((dir) => {
    fs.readdirSync(dir).forEach((file) => {
    if (file.endsWith(".xml")) {
    // gzip
    const fileContents = fs.createReadStream(dir + "/" + file);
    const writeStream = fs.createWriteStream(dir + "/" + file + ".gz");
    const zip = zlib.createGzip();

    fileContents
    .pipe(zip)
    .on("error", (err) => console.error(err))
    .pipe(writeStream)
    .on("error", (err) => console.error(err));
    }
    });
    });