1

Currently, I tried to make zip file(or any format of compressed file) containing few files that I want to put into zip file.

I thought it would work with adm-zip module.
but I found out that the way adm-zip module put files into zip is buffer.
It takes a lot of memory when I put files that size is very huge. In the result, My server stopped working.
Below is What I'd done.

var zip = new AdmZip();
zip.addLocalFile('../largeFile', 'dir1');  //put largeFile into /dir1 of zip
zip.addLocalFile('../largeFile2', 'dir1');
zip.addLocalFile('../largeFile3', 'dir1/dir2');
zip.writeZip(/*target file name*/ `./${threadId}.zip`);

Is there any solution to solve this situation?

4

1 回答 1

0

解决内存问题的最佳做法是使用流而不是将所有文件加载到内存中,例如

import {
  createReadStream,
  createWriteStream
} from 'fs'
import { createGzip } from 'zlib'

const [, , src, dest] = process.argv
const srcStream = createReadStream(src)
const gzipStream = createGzip()
const destStream = createWriteStream(dest)

srcStream
  .pipe(gzipStream)
  .pipe(destStream)
于 2022-02-26T20:06:22.147 回答