1

我有一个使用另一个应用程序(用 Java 编写)创建的 zip 文件,该应用程序使用 deflate 方法将文件压缩到一个 zip 文件中,我验证了诸如“上次修改”之类的信息没有修改为当前日期,并且在使用 Ubuntu 的默认存档解压缩时经理它保持不变。

但是,使用 libzip 解压缩会丢失该数据。有什么方法可以避免这种行为,或者有其他保证元数据持久性的库吗?

解压代码:

void decompress_zip(const std::string& zip, const std::string& out_dir, std::function<void(const std::string&)> fileListener) {
    std::string finput = zip;
    std::string foutput = out_dir;

    if(!boost::filesystem::create_directories(foutput) && !fileExists(foutput))
        throw "Failed to create directory for unzipping";

    foutput += "/tmp.zip";
    if (rename(finput.c_str(), foutput.c_str()))
        throw "Failed to move zip to new dir";
    finput = foutput;

    struct zip *za;
    struct zip_file *zf;
    struct zip_stat sb;
    char buf[100];
    int err;
    int i, len;
    int fd;
    long long sum;

    if ((za = zip_open(finput.c_str(), 0, &err)) == NULL) {
        zip_error_to_str(buf, sizeof(buf), err, errno);
        throw "can't open zip! (" + finput + ")";
    }

    for (i = 0; i < zip_get_num_entries(za, 0); i++) {
        if (zip_stat_index(za, i, 0, &sb) == 0) {
            len = strlen(sb.name);

            if (sb.name[len - 1] == '/') {
                safe_create_dir(sb.name);
            } else {
                zf = zip_fopen_index(za, i, 0);
                if (!zf) {
                    throw "failed to open file in zip! Probably corrupted!!!";
                }

                std::string cFile = out_dir + "/" + std::string(sb.name);
                fd = open(cFile.c_str(), O_RDWR | O_TRUNC | O_CREAT, 0644);
                if (fd < 0) {
                    throw "failed to create output file!";
                }

                sum = 0;
                while (sum != sb.size) {
                    len = zip_fread(zf, buf, 100);
                    if (len < 0) {
                        throw "failed to read file in zip!";
                    }
                    write(fd, buf, len);
                    sum += len;
                }
                close(fd);
                zip_fclose(zf);

                fileListener(cFile);
            }
        }
    }   

    if (zip_close(za) == -1) {
        throw "Failed to close zip archive! " + finput;
    }

    if ( std::remove(foutput.c_str()) )
        throw "Failed to remove temporary zip file! " + foutput;
}
4

1 回答 1

1

我认为libzip只存储数据,而不是元数据。如果需要,您有责任单独存储元数据。

换句话说,它是归档管理器应用程序的一个特性,而不是libzip它本身。

于 2016-07-29T12:52:36.833 回答