【问题标题】:Can you help with creating a zip archive with the LZMA (7zip) SDK?您可以帮助使用 LZMA (7zip) SDK 创建一个 zip 存档吗?
【发布时间】:2023-03-25 06:30:02
【问题描述】:

我正在尝试使用LZMA SDK 创建一个 zip 存档(.zip 或 .7z 格式)。我已经下载并构建了 SDK,我只想使用 dll 导出来压缩或解压缩一些文件。当我使用 LzamCompress 方法时,它返回 0 (SZ_OK),就好像它工作正常一样。但是,在我将缓冲区写入文件并尝试打开它后,我收到一个错误,即文件无法作为存档打开。

这是我目前使用的代码。任何建议将不胜感激。

#include "lzmalib.h"

typedef unsigned char byte;

using namespace std;

int main()
{
    int length = 0;
    char *inBuffer;

    byte *outBuffer = 0;
    size_t outSize;
    size_t outPropsSize = 5;
    byte * outProps = new byte[outPropsSize];

    fstream in;
    fstream out;

    in.open("c:\\temp\\test.exe", ios::in | ios::binary);

    in.seekg(0, ios::end);
    length = in.tellg();
    in.seekg(0, ios::beg);

    inBuffer = new char[length];

    outSize = (size_t) length / 20 * 21 + ( 1 << 16 ); //allocate 105% of file size for destination buffer

    if(outSize != 0)
    {
        outBuffer = (byte*)malloc((size_t)outSize);
        if(outBuffer == 0)
        {
            cout << "can't allocate output buffer" << endl;
            exit(1);
        }
    }

    in.read(inBuffer, length);
    in.close();

    int ret = LzmaCompress(
        outBuffer, /* output buffer */
        &outSize, /* output buffer size */
        reinterpret_cast<byte*>(inBuffer),/* input buffer */
        length, /* input buffer size */
        outProps, /* archive properties out buffer */
        &outPropsSize,/* archive properties out buffer size */
        5, /* compression level, 5 is default */
        1<<24,/* dictionary size, 16MB is default */
        -1, -1, -1, -1, -1/* -1 means use default options for remaining arguments */
    );

    if(ret != SZ_OK)
    {
        cout << "There was an error creating the archive." << endl;
        exit(1);
    }

    out.open("test.zip", ios::out | ios::binary);

    out.write(reinterpret_cast<char*>(outBuffer), (int)(outSize));
    out.close();

    delete inBuffer;
    delete outBuffer;
}

【问题讨论】:

  • 你是如何打开文件的?您使用的是 7zip 归档程序还是 LZMA SDK 的其他功能?
  • 7zip 和 windows zip 文件夹实用程序。
  • 我也在尝试这样做,但仍然没有成功。需要注意的几点.. 1)最初的 5 个字节将是标头属性(outProps),接下来的 8 个字节是长度。 2)然后是压缩数据。当我以这种方式写作时,它也会失败。我认为我们还需要添加“7”、“z”。我不确定。
  • 我放弃了,写了一个类来与7zip.exe接口:(
  • 这真的是最简单的方法吗? :[我也可能这样做......

标签: c++ lzma


【解决方案1】:

我不具体了解 LZMA,但根据我对压缩的一般了解,看起来您正在将压缩的比特流写入一个没有任何头信息的文件,这会让解压缩程序知道比特流是怎样的压缩。

LzmaCompress() 函数可能将此信息写入 outProps。 SDK 中应该有另一个函数,它将获取 outBuffer 中的压缩比特流和 outProps 中的属性,并从中创建适当的存档。

【讨论】:

  • 我是这么想的,但 dll 只导出 LzmaCompress 和 LzmaDecompress。
  • 如果你可以用 LzmaDecompress 解压,那会解决你的问题还是你必须生成一个兼容 7zip 的存档?
  • 我认为使用 LzmaDecompress 解压缩会很好,但是当我尝试这样做时,即使函数返回 SZ_OK,输出大小仍为 0
  • 几年前我使用 zlib 完成了这项工作。我必须自己编写所有的 PKZIP 元信息,zlib 只是生成了压缩的文件数据。显然这是不同的工具,但 LZMA 是一种压缩算法,而不是存档格式。所以我对 LzmaCompress 函数的期望是它不会生成存档文件,只会生成一些压缩数据。 7zip 文件以两个字节“7”、“z”和 PKZIP 开头,“P”、“K”、“\03”、“\04”。如果您没有看到这些,则它不是档案。至少不是你想要的任何一种格式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多