Help Center/ Huawei Cloud Astro Zero/ FAQs/ Application Backend Development/ How Do I Return the Content of a ZIP File in the Huawei Cloud Astro Zero Script?
Updated on 2025-03-20 GMT+08:00

How Do I Return the Content of a ZIP File in the Huawei Cloud Astro Zero Script?

Example script for returning the content of a .zip file:

import * as zip from 'zip'
import * as buffer from 'buffer'
import * as text from 'text';
import * as context from 'context';

let bytes = text.bytes('I want to be binary');

let buff = buffer.from("some data");

let fileMap = {
    "/testdir/test.txt": "this is the data in test.txt",
    "/testdir/demo/bytes.txt": bytes,
    "/testdir/demo/buff.txt": buff.bytes(),
}

let stream = zip.compression(fileMap);

let resp = context.getHttp().response;
resp.setHeader("Content-Type", "application/zip");
let size = len(stream);
resp.setHeader('Content-Length', <string><any>size);
// Only the ZIP binary stream is returned to the browser. The browser needs to parse the stream based on Content-Type.
resp.setBody(stream);