2016-07-29 65 views
0

我有一個使用deflate方法將文件壓縮成一個zip文件的另一個應用程序(用Java編寫)創建的zip文件,我證實「最後修改」等信息未修改爲當前日期,並在使用Ubuntu的默認歸檔管理器進行解壓縮時保持不變。解壓縮libzip會丟失文件元數據

但是,使用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; 
} 

回答

0

我覺得libzip只存儲數據,而不是元數據。如果您需要,您可以單獨存儲元數據。

換句話說,這是歸檔管理器應用程序的一個功能,而不是libzip本身。

+0

由於沒有其他答案,我花了整整一天的時間進行研究,所以我擔心你是對的,並會將你的答案標記爲正確答案。 –