文档首页/ Astro轻应用 AstroZero/ 常见问题/ 应用后端开发/ 在AstroZero脚本中,如何返回zip压缩文件内容?
更新时间:2024-06-19 GMT+08:00
分享

在AstroZero脚本中,如何返回zip压缩文件内容?

返回zip压缩文件内容脚本示例如下:

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);
// 这里只是把zip二进制流返回给浏览器,浏览器需要依据Content-Type来解析
resp.setBody(stream);

相关文档