Sample code for 30+ languages & platforms
SQL Server

Unzip an Email's Zip Attachments

See more Email Object Examples

Demonstrates the Chilkat Email.UnzipAttachments method, which unzips and replaces any Zip file attachments with their expanded contents. For example, if an email contains a single Zip holding three files, that Zip attachment is replaced by the three files as individual attachments. This example loads an email and expands its Zip attachments.

Background: Senders often bundle several files into one .zip to keep a message tidy or to compress large content. UnzipAttachments reverses that automatically, so downstream processing can work with the individual files directly rather than having to detect, extract, and re-inject archive contents. It is the inverse of ZipAttachments.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the UnzipAttachments method, which unzips and replaces any Zip file
    --  attachments with their expanded contents.  For example, a single Zip containing 3 files
    --  is replaced by those 3 files as individual attachments.

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'qa_data/eml/with_zip_attachment.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    EXEC sp_OAGetProperty @email, 'NumAttachments', @iTmp0 OUT
    PRINT 'NumAttachments before = ' + @iTmp0

    --  Expand any Zip attachments in place.
    EXEC sp_OAMethod @email, 'UnzipAttachments', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    EXEC sp_OAGetProperty @email, 'NumAttachments', @iTmp0 OUT
    PRINT 'NumAttachments after = ' + @iTmp0

    --  Note: The path "qa_data/..." is a relative local filesystem path,
    --  relative to the current working directory of the running application.

    EXEC @hr = sp_OADestroy @email


END
GO