从Nim中的tar.gz存档中读取文件 [英] reading files from tar.gz archive in Nim

查看:96
本文介绍了从Nim中的tar.gz存档中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一种使用Nim编程语言(版本0.11.2)从tar.gz存档中读取文件的方法.说我有一个档案

Looking for a way to read in a file from a tar.gz archive using the Nim programming language (version 0.11.2). Say I have an archive

/my/path/to/archive.tar.gz

和该存档中的文件

my/path/to/archive/file.txt

我的目标是能够在Nim中逐行读取文件的内容.在Python中,我可以使用tarfile模块来执行此操作.在Nim中,有libzip和zlib模块,但是文档很少,也没有示例.还有zipfiles模块,但是我不确定它是否能够处理tar.gz档案.

My goal is to be able to read the contents of the file line by line in Nim. In Python I can do this with the tarfile module. In Nim there are the libzip and zlib modules, but the documentation is minimal and there are no examples. There's also the zipfiles module, but I'm not sure if this is capable of working with tar.gz archives.

推荐答案

据我所知,libzip和zlib不能用于读取tar文件(afaik它们仅支持zip存档和/或原始字符串压缩,而tar.gz需要gzip + tar).不幸的是,似乎还没有Nim库可以读取tar.gz档案.

To my knowledge, libzip and zlib cannot be used to read tar files (afaik they only support zip archives and/or raw string compression, while a tar.gz requires gzip + tar). Unfortunately it looks like there are no Nim libraries yet which read tar.gz archives.

如果您对基于tar的快速,肮脏的解决方案还可以,则可以执行以下操作:

If you are okay with a quick-and-dirty tar-based solution, you can do this:

import osproc

proc extractFromTarGz(archive: string, filename: string): string =
  # -z extracts
  # -f specifies filename
  # -z runs through gzip
  # -O prints to STDOUT
  result = execProcess("tar -zxf " & archive & " " & filename & " -O")

let content = extractFromTarGz("test.tar.gz", "some/subpath.txt")

如果您想要一个干净而灵活的解决方案,这将是为 libarchive 库;).

If you want a clean and flexible solution, this would be a good opportunity to write a wrapper for the libarchive library ;).

这篇关于从Nim中的tar.gz存档中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆