【问题标题】:Vala: How to retrieve the names of all files in a zip archive without unzipping it?Vala:如何在不解压缩的情况下检索 zip 存档中所有文件的名称?
【发布时间】:2016-09-24 03:52:04
【问题描述】:

vala 中有没有像 erlang 中的 zip:list_dir 这样的函数?我找到了 libgsf,但我不想解压缩 zip 文件。

【问题讨论】:

    标签: zip compression gnome vala


    【解决方案1】:

    您可以使用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 限制为仅允许 zip 文件。我已经翻译了example from the upstream wiki

    【讨论】:

    • GCC 警告我 archive_read_finish 已被弃用。有什么办法可以消除这些警告吗?
    • libarchive.vapi 文件需要更新,archive_read_finish 函数在 libarchive 3.0.2 中被重命名为 archive_free。您可以将此报告给bugzilla.gnome.org(产品 Vala),以便 Vala 开发人员可以修复它。
    • 只要 libarchive 开发人员不删除 archive_read_finish 您仍然可以按原样使用 vapi。
    • 因为我把所有的部分放在一起,我现在自己添加了一个错误报告:bugzilla.gnome.org/show_bug.cgi?id=771916
    猜你喜欢
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2023-02-14
    • 2012-03-18
    • 1970-01-01
    • 2023-03-21
    • 2022-11-21
    • 1970-01-01
    相关资源
    最近更新 更多