Skip to content

Instantly share code, notes, and snippets.

@ff6347
Last active September 14, 2021 04:58
Show Gist options
  • Save ff6347/e27d9aa934b54b8293b2e5af0f3e81f2 to your computer and use it in GitHub Desktop.
Save ff6347/e27d9aa934b54b8293b2e5af0f3e81f2 to your computer and use it in GitHub Desktop.
//@ts-check
import got from "got";
import stream from "stream";
import zlib from "zlib";
import fs from "fs";
const fileList = ["file.gz", "file1.gz", "file2.gz", "file3.gz"];
const tmpFolderPath = "/tmp";
async function main() {
const transferTasks = fileList.map(
(file) =>
new Promise((resolve, reject) => {
const outfile = file.replace(".gz", "");
const inFilePath = `http://example.com/path/to/${file}`;
const rstream = got.stream(inFilePath);
const fswstream = fs.createWriteStream(`${tmpFolderPath}/${outfile}`);
stream.pipeline(rstream, zlib.createGunzip(), fswstream, (err) => {
if (err) {
reject();
} else {
resolve();
}
});
})
);
await Promise.all(transferTasks).catch((err) => {
console.error("Error in promise all transferTask", err);
});
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment