'Create archive from readable, add file and pipe to writable

The goal is to receive an archive form the client, add a file, and upload it to Cloud Storage without creating a temporary file. The client and the server both use the archiver library. The problem with the code below is that file2.txt does not get added to the archive.

Client:

import archiver from "archiver";

const archive = archiver("zip", {
    zlib: { level: 9 },
});

archive.append("string cheese!", { name: "file1.txt" });

await fetch(`/archive`, {
    method: "POST",
    body: archive,
});

Server:

import archiver from "archiver";
import { Storage } from "@google-cloud/storage";
import { Router } from "express";

const router = Router();
const storage = new Storage();

router.post("/", (req, res) => {
  const archive = archiver("zip", {
    zlib: { level: 9 },
  });

  const cloudFile = storage.bucket("archives").file("archive.zip");

  req.pipe(archive, {
    end: false,
  });

  archive.pipe(cloudFile.createWriteStream());

  req.on("end", async () => {
    archive.append("string cheese!", { name: "file2.txt" });
    archive.finalize();
    archive.end();
  });
});


Solution 1:[1]

as i can see from the documentations they are using fs.createReadStream(file1) to reach out file system however you can achieve this if you get Buffer from the received data

const multer  = require('multer')
const upload = multer()

router.post("/", upload.none(), function (req, res, next) {

// append a file from buffer
const buffer = req.files[0].buffer
archive.append(buffer, { name: 'file3.txt' });
})

also a tip if you working in serverless env most likely you don't have access to file system to read, write and clean but at some cases you have access to /temp directory it worth a quick search if this is the case

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hazem-a1