如何使用 7z DLL 压缩许多小数据块并将其附加到文件中

发布于 2024-11-07 13:59:07 字数 171 浏览 0 评论 0原文

我想使用 7z DLL 将少量数据附加到一个压缩文件中。目前我最好的猜测是解压缩 7z 文件,附加数据,然后重新压缩它。显然,如果 7z 文件的大小变大(例如 1 GB)并且我想每秒保存一个新块,那么这不是一个性能良好的解决方案。我怎样才能以更好的方式做到这一点?

我可以使用 7z DLL 支持的任何压缩格式。

I would like to use the 7z DLL to append small amounts of data to one compressed file. At the moment my best guess would be uncompress the 7z file, append the data, and recompress it. Obviously, this is not a good solution performance wise, if the size of the 7z file becomes large (say 1 gb) and I want do save a new chunk every second. How could I do this in a better way?

I could use any compression format supported by the 7z DLL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£冰雨忧蓝° 2024-11-14 13:59:07

看一下 Python LZMA 绑定(LZMA 是 7z 的压缩算法名称) ,你应该做你想做的事情,而不需要 ctypes 的东西。

编辑

待确认,但快速查看py7zlib。 py 显示仅支持读取 7z 文件,不支持写入。然而,在 src 目录中有一个 pylzma_compressfile.c,所以也许有一些事情要做。

编辑2

pylzma.compressfile 函数似乎在那里,很好。

Have a look at the Python LZMA bindings (LZMA is the compression algorithm name of 7z), you should do what you want without ctypes stuff.

EDIT

To be confirmed, but a quick look at py7zlib.py shows only support for reading 7z files, not writing. However in the src dir there's a pylzma_compressfile.c, so maybe there's something to do.

EDIT 2

The pylzma.compressfile function seems to be there, so fine.

白况 2024-11-14 13:59:07

这不是我的答案。

如何使用 Python 中的 DLL 文件?

我想ctypes 是正确的选择。

以下 ctypes 示例来自我编写的实际代码(在 Python 2.5 中)。到目前为止,这是我发现执行您要求的操作的最简单方法。

import ctypes

# Load DLL into memory.

hllDll = ctypes.WinDLL ("c:\\PComm\\ehlapi32.dll")

# Set up prototype and parameters for the desired function call.
# HLLAPI

hllApiProto = ctypes.WINFUNCTYPE (ctypes.c_int,ctypes.c_void_p,
    ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),

# Actually map the call ("HLLAPI(...)") to a Python name.

hllApi = hllApiProto (("HLLAPI", hllDll), hllApiParams)

# This is how you can actually call the DLL function.
# Set up the variables and call the Python name with them.

p1 = ctypes.c_int (1)
p2 = ctypes.c_char_p (sessionVar)
p3 = ctypes.c_int (1)
p4 = ctypes.c_int (0)
hllApi (ctypes.byref (p1), p2, ctypes.byref (p3), ctypes.byref (p4))

ctypes 包含所有 C 类型数据类型(int、char、short、void*、...),并且可以按值或引用传递。它还可以返回特定的数据类型,尽管我的示例没有这样做(HLL API 通过修改按引用传递的变量来返回值)。

This is NOT MY ANSWER.

How can I use a DLL file from Python?

I think ctypes is the way to go.

The following example of ctypes is from actual code I've written (in Python 2.5). This has been, by far, the easiest way I've found for doing what you ask.

import ctypes

# Load DLL into memory.

hllDll = ctypes.WinDLL ("c:\\PComm\\ehlapi32.dll")

# Set up prototype and parameters for the desired function call.
# HLLAPI

hllApiProto = ctypes.WINFUNCTYPE (ctypes.c_int,ctypes.c_void_p,
    ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),

# Actually map the call ("HLLAPI(...)") to a Python name.

hllApi = hllApiProto (("HLLAPI", hllDll), hllApiParams)

# This is how you can actually call the DLL function.
# Set up the variables and call the Python name with them.

p1 = ctypes.c_int (1)
p2 = ctypes.c_char_p (sessionVar)
p3 = ctypes.c_int (1)
p4 = ctypes.c_int (0)
hllApi (ctypes.byref (p1), p2, ctypes.byref (p3), ctypes.byref (p4))

The ctypes stuff has all the C-type data types (int, char, short, void*, ...) and can pass by value or reference. It can also return specific data types although my example doesn't do that (the HLL API returns values by modifying a variable passed by reference).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文