node.js如何解开.tgz存档中的文件夹中的.csv文件

发布于 2025-02-09 18:54:11 字数 286 浏览 1 评论 0 原文

我有一个存档 - “ list_users.tgz”,其中有一个文件夹“ out”,并且此文件夹是我想解动和使用的“ list_users.csv”文件。

对于.csv文件的读取 - 我有代码:

const data = fs.readFileSync('/home/some_project/list_users.csv', 'utf8');
const records = parse(data, {columns: true});

但是如何在此之前从存档中生成.csv文件?

I have an archive - "list_users.tgz" and inside it there is a folder "out" and inside this folder is the "list_users.csv" file that I want to unzip and use.

For the reading of the .csv file - I have code:

const data = fs.readFileSync('/home/some_project/list_users.csv', 'utf8');
const records = parse(data, {columns: true});

But how to generate the .csv file from the archive before that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱着落日 2025-02-16 18:54:11

这很简单:

await tar.x({file: fileDir});

有用的链接与更多方案 -

整个代码:

const tar = require('tar');
const fs = require('fs');
const parse = require('csv-parse/sync').parse;

async function getUsers() {
    fileDir = '/mnt/out/list_users.tgz';
    await tar.x({file: fileDir});
    const data = fs.readFileSync('<directory where your app.js file is>', 'utf8');
    const records = parse(data, {columns: true});
    return records;

It was as simple as that:

await tar.x({file: fileDir});

Helpful link with more scenarios - https://gist.github.com/noyobo/b0ed1722a59302cfc99c2ec93be6807c

The entire code:

const tar = require('tar');
const fs = require('fs');
const parse = require('csv-parse/sync').parse;

async function getUsers() {
    fileDir = '/mnt/out/list_users.tgz';
    await tar.x({file: fileDir});
    const data = fs.readFileSync('<directory where your app.js file is>', 'utf8');
    const records = parse(data, {columns: true});
    return records;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文