Extract In-Memory Streams with 7z.dll API
This guide provides an overview of how to extract archive streams
entirely within memory using the 7z.dll library. While
7z.dll does not provide high-level, procedural helper
functions for memory extraction, it exposes low-level COM-style entry
points and interfaces that allow developers to handle archive processing
in memory by wrapping buffers into custom stream objects.
The Exported Functions of 7z.dll
7z.dll is designed as a minimalist C-style dynamic link
library that exposes only a handful of exported functions. The functions
exported by the DLL are:
CreateObject: The primary API function used for archive processing. It instantiates the internal COM-style classes (such as archive handlers) and queries the requested interface.- Signature:
STDAPI CreateObject(const GUID *clsID, const GUID *interfaceID, void **outObject);
- Signature:
GetNumberOfFormats: Returns the total number of archive formats supported by the compiled DLL.- Signature:
STDAPI GetNumberOfFormats(UINT32 *numFormats);
- Signature:
GetHandlerProperty: Retrieves metadata and configuration properties for a specific archive format handler index.- Signature:
STDAPI GetHandlerProperty(PROPID propID, PROPVARIANT *value);
- Signature:
GetHandlerProperty2: An extended version ofGetHandlerPropertythat accepts a format index parameter to query properties for specific archive codecs.- Signature:
STDAPI GetHandlerProperty2(UInt32 formatIndex, PROPID propID, PROPVARIANT *value);
- Signature:
Implementing In-Memory Extraction via CreateObject
Because CreateObject only provides raw interface
pointers, extracting an archive strictly in memory requires implementing
7-Zip's custom I/O interfaces inside your application. You do not write
to or read from disk; instead, you wrap memory buffers within these
interfaces.
1. Required Interfaces
IInArchive: Obtained directly fromCreateObject. It controls archive operations, including opening the archive format and triggering decompression.IInStream: Must be implemented by the caller. This interface wraps your in-memory input buffer (the raw archive bytes). It derives fromISequentialInStreamand requires implementations forReadandSeekoperations so7z.dllcan navigate the archive structure.IArchiveExtractCallback: Must be implemented by the caller to handle extraction events, check completion statuses, and direct the output destination.ISequentialOutStream: Must be implemented by the caller to receive the decompressed payload. TheIArchiveExtractCallback::GetStreammethod supplies an instance of this interface to7z.dll, directing the decompressed data into an allocated memory buffer via itsWritemethod.
2. The Extraction Workflow
- Load the Library: Load
7z.dllusingLoadLibraryand locate theCreateObjectentry point viaGetProcAddress. - Create the Archive Handler: Call
CreateObjectusing the specific format Class ID (such asCLSID_CFormat7zorCLSID_CFormatZip) to obtain anIInArchivepointer. - Open the Stream: Instantiate your custom
IInStreamwrapper over the archive's memory buffer. Pass this instance toIInArchive::Open, specifying a maximum scan size. - Initiate Extraction: Call
IInArchive::Extract. Pass the indices of the items to decompress, a test/extract mode flag, and an instance of your customIArchiveExtractCallback. - Capture Output: When
7z.dlldecompressess an item, it invokesIArchiveExtractCallback::GetStream. Return your customISequentialOutStreamimplementation, which appends incoming bytes directly to your target memory buffer.