Vala:如何在不解压缩的情况下检索 zip 存档中所有文件的名称?

Vala: How to retrieve the names of all files in a zip archive without unzipping it?

vala 中是否有类似 erlang 中的 zip:list_dir 的函数?我找到了 libgsf,但我不想解压 zip 文件。

您可以使用 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编译。

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

您还可以将 libarchive 限制为仅允许压缩文件。我翻译了 example from the upstream wiki.