Skip to content

zip.Stream 的 addEntry 不能在多个事件循环中使用 #60

Open
@weijiafu14

Description

@weijiafu14

const { zip } = require('compressing');
const fs = require('fs')
const zipStream = new zip.Stream();
zipStream.addEntry('test.html')
setTimeout(() => {
zipStream.addEntry('yarn.lock')
setTimeout(() => {
zipStream.addEntry('package.json')
setTimeout(() => {
zipStream.pipe(fs.createWriteStream('test.zip'));
}, 3000)
}, 3000)
}, 3000)

这段代码最终压缩包里只会有test.html ,原因是每次addEntry后都会调用 finalize ,会使依赖的yazl.zipFile 调用end,调用了end后,会使zipFile处理完最后一个文件后如果没有其他Entry就关闭流,导致压缩结束,所以处理完已添加的文件前如果 addEntry 没调用到,流就会关闭,再调用就不起作用,
所以安全起见,addEntry 要一次性同步调完,或者需要异步用yazl

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions