如何使用zlib.gzipSync(buffer [,options])来压缩文件,以及如何使用zlib.gunzipSync(buffer [,options])来解压缩文件?

LT站点

嗨,大家好

我一直在讨论在同步模式下使用zlib的想法,(对于我来说,成为同步模式很重要)

我已经尝试过多次并失败了,节点文档还不是很清楚,并且缺少使用示例

我打算做的是:

压缩文本文件“ myfile.txt”的功能,该文件包含一些数据作为文本“ Some text”并将其另存为“ myfile.txt.gz”

function zip(fullPathToFile){
  const zlib= require('zlib');

  //some cool stuff...using:

  zlib.gzipSync(buffer[, options])

}

用于将“ myfile.txt.gz”解压缩为“ myfile.txt”的函数

function unZip(fullPathToFile){
  const zlib= require('zlib');

  //some cool stuff...using:

  zlib.gunzipSync(buffer[, options])

}

全部在同一个目录中

任何想法?

感谢您的阅读和帮助

乔纳森·罗莎(Jonathan Rosa)

我首先使用fs.readFileSync来读取文件,然后将Buffer(数据)作为的第一个参数插入zlib.gzipSync出来的还有一个缓冲区(压缩数据);我使用将其写入文件fs.writeFileSync

const fs = require("fs");
const zlib = require("zlib");
function zip(path) {
    let data = fs.readFileSync(path);
    data = zlib.gzipSync(data);
    fs.writeFileSync(`${path}.gz`, data);
}

为了减压,更换zlib.gzipSynczlib.gunzipSync是你需要做的唯一的编辑。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章