【问题标题】:How to archive and compress data with "PhysicsFS"如何使用“PhysicsFS”归档和压缩数据
【发布时间】:2012-07-04 01:01:11
【问题描述】:

我正在查看“PhysicsFS”文档并寻找存档和压缩数据的方法,但找不到。这可能吗?我是否可以这样做

【问题讨论】:

    标签: c++ archive


    【解决方案1】:

    PhysicsFS 压缩支持

    PhysicsFS 支持从安装在其提供的“虚拟文件系统”中任意点的 zip 文件中读取文件。这可以有效地从 ZIP 存档中解压。

    但是,PhysicsFS 不支持添加或修改 ZIP 存档的内容。它只允许在其文档中称为“写入目录”的地方写入未压缩的文件。

    所以,总结一下:PhysicsFS 只支持从 ZIP 档案中读取,不支持写入它。 对于您自己的压缩:如果需要,只需使用外部压缩器压缩所有写入的文件。


    PhysicsFS 使用情况

    有一个PhysicsFS的小教程here

    使用非常简单:

    // initialize the lib
    PHYSFS_init(NULL);
    
    // "mount" a zip file in the root directory
    PHYSFS_AddToSearchPath("myzip.zip", 1);
    
    // set a directory for writing
    PHYSFS_setWriteDir(const char *newDir);
    
    // open a file for reading
    PHYSFS_file* myfile = PHYSFS_openRead("myfile.txt");
    
    // open a file for writing
    PHYSFS_file* myfile = PHYSFS_openWrite("output_file.bin");
    
    // get a file size
    PHYSFS_sint64 file_size = PHYSFS_fileLength(myfile);
    
    // read data from a file (decompress only if path is inside a zip mount point)
    char* myBuf = new char[file_size];
    int length_readed = PHYSFS_read(myfile, myBuf, 1, file_size);
    
    // write data to a file (uncompressed)
    char* myBuf = new char[new_file_size];
    //...fill myBuf...
    int length_writed = PHYSFS_write(myfile, myBuf, 1, new_file_size);
    
    // close a file
    PHYSFS_close(myfile);
    
    // deinitialize the lib
    PHYSFS_deinit();
    

    【讨论】:

    • 谢谢,但在您的示例中,我只能看到如何读取和写入数据。而不是如何存档或压缩。
    • 显然,从“安装为目录的 zip 文件”中读取文件解压缩它,而压缩则由您自己完成。我正在编辑我的答案。
    • 好的。存档呢。这是我的主要目标。这个库有可能
    • 是的。您在 PhysicsFS 的虚拟文件系统中“挂载”一个 zip 文件,然后您可以从该 zip 存档中读取。但是对于写作,你写在 PhysicsFS 所谓的“写目录”上,然后,独立于 PhysicsFS,你可以将整个写目录归档为 zip 文件(例如:使用 zip 压缩器)。然后最终你可以使用 PhysicsFS 挂载这个新的 zip。所以最后:PhysicsFS 不支持写入 zip 存档,只能从中读取。
    • @themean 我已经澄清了我的答案。
    【解决方案2】:

    你的意思是“PhysFS”吗?

    这是一个从 zipfile 读取文件的库。基本上就像 Quake 引擎一样。

    【讨论】:

    • 它们是同一个库。 PhysFS 只是个绰号。
    猜你喜欢
    • 1970-01-01
    • 2016-08-05
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2021-01-01
    • 2017-02-11
    相关资源
    最近更新 更多