瓦拉:如何在不解压缩的情况下检索zip存档中所有文件的名称? [英] Vala: How to retrieve the names of all files in a zip archive without unzipping it?

查看:110
本文介绍了瓦拉:如何在不解压缩的情况下检索zip存档中所有文件的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vara中是否有诸如erlang中的 zip:list_dir 之类的功能?我找到了libgsf,但是我不想解压缩该zip文件。

Is there any function in vala like zip:list_dir in erlang ? I found libgsf, but I don't want to decompress the zip file.

推荐答案

您可以使用 libarchive

void check_ok (Archive.Result r) throws IOError {
    if (r == Archive.Result.OK)
        return;
    if (r == Archive.Result.WARN)
        return;
    throw new IOError.FAILED ("libarchive returned an error");
}

int main () {

    try {
        var a = new Archive.Read ();
        check_ok (a.support_filter_all ());
        check_ok (a.support_format_all ());
        check_ok (a.open_filename ("archive.zip", 10240));

        unowned Archive.Entry entry;
        while (a.next_header (out entry) == Archive.Result.OK) {
            stdout.printf ("%s\n", entry.pathname ());
            a.read_data_skip ();
        }
    }
    catch (IOError e) {
        stderr.printf (e.message + "\n");
        return 1;
    }

    return 0;
}

使用 valac ListZip.vala --pkg libarchive进行编译--pkg gio-2.0

GIO仅用于 IOError 错误域。实际上,您希望使用一些更具描述性的消息来扩展 check_ok 方法,该操作已失败。

GIO is only needed for the IOError errordomain. In reality, you'd want to extend the check_ok method with some more descriptive message which operation has failed.

您还可以将libarchive限制为仅允许zip文件。我已经从上游Wiki翻译了示例

You can also restrict libarchive to only allow zip files. I have translated the example from the upstream wiki.

这篇关于瓦拉:如何在不解压缩的情况下检索zip存档中所有文件的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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