csv 将解压缩流转换为常规流

ezykj2lf  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(125)
let decipher = crypto.createDecipheriv(type, file.metadata?.key, file.metadata?.iv)
    let decompress = new fflate.Decompress()

    let decipherStream = fs.createReadStream(location).pipe(decipher) 
    decipherStream.on('data', (data) => decompress.push(data))
    decipherStream.on('finish', () => decompress.push(new Uint8Array(), true))

    decompress.ondata = (data: any, final: any) => {
      if (!final) res.write(data)
      if (final) res.send()
    } 
    res.attachment(file.originalName)

字符串
我有这段工作代码来获取和解压缩一个文件,并将其作为API响应发送,我需要使它与不同的进程一起工作,而不是作为API响应。
这是我的尝试:

let decipher = crypto.createDecipheriv(type, file.metadata?.key, file.metadata?.iv)
  let decompress = new fflate.Decompress()

  let decipherStream = fs.createReadStream(location).pipe(decipher) 
  decipherStream.on('data', (data) => decompress.push(data))
  decipherStream.on('finish', () => decompress.push(new Uint8Array(), true))
  let stream = new Readable()
  stream._read = function (){}
  decompress.ondata = (data: any, final: any) => {
    console.log(data)
      if (!final) stream.push(data)
      if (final) stream.push(null)
    } 
    let headernames:any = [] 
    let jsonobjs:any = []
    let jsonout = ''

    await csv().fromStream(stream)
    .on('headers', (headers: any) => {
     headernames = headers;
     console.log("Headers are", headernames);
    })
    .on('data', (data: any) => {
     jsonout = data.toString('utf8');
     jsonobjs.push(jsonout);
    })
    .on('done', () => {
     console.log("jsonout:",jsonout);
     return jsonobjs;
    })


这只发送csv中的第一行数据,然后它挂起,任何帮助都将不胜感激。

hiz5n14c

hiz5n14c1#

对于其他有关于fflate库和csvtoJson库的具体问题的人,我找到了解决方案,而不是试图将流放入CSV()。fromStream我使用CSV().fromString,并提供了对fflate的回调。解压缩函数:

let decipher = crypto.createDecipheriv('aes-256-cbc', file.metadata?.key, file.metadata?.iv)
  let stringData = ''
  const utfDecode = new fflate.DecodeUTF8(async (data, final) => {

    stringData += data;

    if (final && stringData){
    await csv().fromString(stringData)
    .on('headers', (headers: any) => {
     headernames = headers;
     console.log("Headers are", headernames);
    })
    .on('data', (data: any) => {
     jsonout = data.toString();
     jsonobjs.push(jsonout);
    })
    .on('done', () => {
     console.log("jsonout:",jsonout);
     return(jsonobjs);
    })
    }else if (final && !stringData){
      return {status: false, contents: "csv file is empty"}
    }
    
  });

  let decompress = new fflate.Decompress((chunk, final) => {
    console.log(chunk, 'was encoded with GZIP, Zlib, or DEFLATE');
    utfDecode.push(chunk, final);
  });

  let decipherStream = fs.createReadStream(location).pipe(decipher) 
  decipherStream.on('data', (data) => decompress.push(data))
  decipherStream.on('finish', () => decompress.push(new Uint8Array(), true))

字符串

相关问题