'Get contents of in-memory zip archive without saving the zip

I'm getting a ZIP archive from S3 using the aws s3 node SDK. In this zip file there is a single .json file where I want to get the contents from. I don't want to save this file to storage, but only get the contents of this zip file.

Example: File.zip contains a single file:

  • file.json with contents({"value":"abcd"})

I currently have:

const { S3Client, GetObjectCommand} = require("@aws-sdk/client-s3");
const s3Client = new S3Client({ region: 'eu-central-1'});
const file = await s3Client.send(new GetObjectCommand({Bucket:'MyBucket', Key:'file.zip'}));

file.body now contains a Readable stream with the contents of the zip file. I now want to transfer this Readable stream into {"value":"abcd"}

Is there a library or piece of code that can help me do this and produce the result without having to save the file to disk?



Solution 1:[1]

You could use the package archiver or zlib (zlib is integrated in nodejs)

a snippet from part of my project looks like this:


import { unzipSync } from 'zlib';

// Fetch data and get buffer
const res = await fetch('url')
const zipBuffer = await res.arrayBuffer()

// Unzip data and convert to utf8
const unzipedBuffer = await unzipSync(zipBuffer)
const fileData = unzipedBuffer.toString('utf8')

Now fileData is the content of your zipped file as a string, you can use JSON.parse(fileData) to get the content as a json and work with it

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 Maximilian Dolbaum