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);
  • GetNumberOfFormats: Returns the total number of archive formats supported by the compiled DLL.
    • Signature: STDAPI GetNumberOfFormats(UINT32 *numFormats);
  • GetHandlerProperty: Retrieves metadata and configuration properties for a specific archive format handler index.
    • Signature: STDAPI GetHandlerProperty(PROPID propID, PROPVARIANT *value);
  • GetHandlerProperty2: An extended version of GetHandlerProperty that accepts a format index parameter to query properties for specific archive codecs.
    • Signature: STDAPI GetHandlerProperty2(UInt32 formatIndex, PROPID propID, PROPVARIANT *value);

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 from CreateObject. 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 from ISequentialInStream and requires implementations for Read and Seek operations so 7z.dll can 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. The IArchiveExtractCallback::GetStream method supplies an instance of this interface to 7z.dll, directing the decompressed data into an allocated memory buffer via its Write method.

2. The Extraction Workflow

  1. Load the Library: Load 7z.dll using LoadLibrary and locate the CreateObject entry point via GetProcAddress.
  2. Create the Archive Handler: Call CreateObject using the specific format Class ID (such as CLSID_CFormat7z or CLSID_CFormatZip) to obtain an IInArchive pointer.
  3. Open the Stream: Instantiate your custom IInStream wrapper over the archive's memory buffer. Pass this instance to IInArchive::Open, specifying a maximum scan size.
  4. Initiate Extraction: Call IInArchive::Extract. Pass the indices of the items to decompress, a test/extract mode flag, and an instance of your custom IArchiveExtractCallback.
  5. Capture Output: When 7z.dll decompressess an item, it invokes IArchiveExtractCallback::GetStream. Return your custom ISequentialOutStream implementation, which appends incoming bytes directly to your target memory buffer.