home *** CD-ROM | disk | FTP | other *** search
- // Memory Compression.cpp : Defines the entry point for the console application.
- //
- // This file is part of the Memory Compression sample using
- // The Xceed Zip Compression Library 4
- // Copyright (c) 1998-1999 Xceed Software Inc.
- //
- // This sample shows how to compress a memory buffer and add it to a zip file
- // as a regular file entry.
-
-
- #include "stdafx.h" // IMPORTANT: Take a look at this file
- #include "zipDispIds.h" // DISPIDs for methods, properties and events
- #include "zipAtlFuncInfo.h" // Event information structures
-
-
- // The following line imports the type library from XceedZip.dll, and creates
- // wrapper classes for the controls (in XceedZip.tlh and XceedZip.tli).
-
- #import "XceedZip.dll" no_namespace named_guids
-
-
- // Change these defines to customize the sample
-
- #define ZIP_FILENAME _T( "c:\\test.zip" )
-
-
- // This identifies our sink object (can be any id)
-
- #define DISPID_ZIP_SINK 1
-
-
- // Usually, a sink object derives from IDispEventImpl, which uses type info from the
- // event source's typelib to get the funcinfo. But IDispEventImpl::GetUserDefinedType()
- // only handles TKIND_ALIAS. In many of our events, we have TKIND_ENUM parameters.
- // This is why the sink object derives from IDispEventSimpleImpl, which does not use
- // type information. We need to provide a _ATL_FUNC_INFO for each event we want to
- // handle. Fortunately, you can get all these informations in "zipAtlFuncInfo.h"
-
- class CZipEventSink : public IDispEventSimpleImpl< DISPID_ZIP_SINK, CZipEventSink, &DIID__IXceedZipEvents >
- {
- public:
- //
- // QueryMemoryFile event: This event is triggered by the XceedZip control as long as
- // the bFileProvided is set to true. Each time this is the case
- // a new virtual entry is added to the zip file. For each of
- // these virtual entries, at least one "ZippingMemoryFile" event
- // will be triggered, for you to provide the data to compress.
- void _stdcall QueryMemoryFile( long * lUserTag,
- BSTR * sFilename,
- BSTR * sComment,
- long * lAttributes,
- DATE * dtLastModified,
- DATE * dtLastAccessed,
- DATE * dtCreated,
- VARIANT_BOOL * bEncrypted,
- BSTR * sPassword,
- VARIANT_BOOL * bFileProvided )
- {
- if( m_bMustProvideFile )
- {
- m_bMustProvideFile = false;
-
- // You must realloc the provided pointers!
- SysReAllocString( sFilename, L"UserMessage.txt" ); // Fake file entry
-
- *bFileProvided = VARIANT_TRUE;
- }
- else
- {
- *bFileProvided = VARIANT_FALSE;
- }
-
- // Little internal trick: All calls to QueryMemoryFile happen before the first
- // call to ZippingMemoryFile. We take the opportunity to reset internal flags
- // used in ZippingMemoryFile.
- m_bFirstCall = true;
- }
-
- //
- // ZippingMemoryFile event: This event is paired with each fake entry specified in the
- // above event. This is where you specify the data to compress.
- // If you cannot provide all the data at once (e.g. when
- // compreesing a stream oriented provider), you can set the
- // bEndOfData parameter to false, and another call to this event
- // will be made by XceedZip for the same entry.
- //
- void _stdcall ZippingMemoryFile( long lUserTag,
- BSTR sFilename,
- VARIANT * vaDataToCompress,
- VARIANT_BOOL * bEndOfData )
- {
- if( m_bFirstCall )
- {
- printf( "Please enter the message you want to add to the zip file\n"
- "as the %S entry.\n"
- "Enter \"end\" on a single line to end the message.\n",
- sFilename );
-
- m_bFirstCall = false;
- }
-
- // Retrieve a line from the console
- char szLine[ 200 ];
- gets( szLine );
-
- if( !lstrcmpi( szLine, "end" ) )
- {
- // Nothing to compress for the last call of this entry
- // It is legal to empty vaDataToCompress!
- VariantClear( vaDataToCompress );
- *bEndOfData = VARIANT_TRUE;
-
- // Let's reactivate the "first call" flag for next entry!
- m_bFirstCall = true;
- }
- else
- {
- // gets removed the \n. Add it back!
- lstrcat( szLine, "\n" );
-
- // We create a safearray that will contain the data to be zipped
- long lSize = lstrlen( szLine );
- SAFEARRAY* psa = SafeArrayCreateVector( VT_I1, 0, lSize );
-
- BYTE* pcData;
-
- SafeArrayAccessData( psa, ( void** )&pcData );
-
- // We copy the line in the safe array buffer
- CopyMemory( pcData, szLine, lSize );
-
- SafeArrayUnaccessData( psa );
-
- // We set the variant vaDataToCompress to the safearray we just created.
- vaDataToCompress->vt = VT_UI1 | VT_ARRAY;
- vaDataToCompress->parray = psa;
-
- // Since it wasn't the "end" keyword, we tell XceedZip that we did not
- // provide all the data for this entry yet.
- *bEndOfData = VARIANT_FALSE;
- }
-
- // Little trick again: Since a call to this event means no more calls to
- // QueryMemoryFile, we reset the "must provide file" flag for next
- // zipping operation.
- m_bMustProvideFile = true;
- }
-
- // The sink map is used by IDispEventSimpleImpl<>. The map contains information about
- // each event handler we want to provide.
-
- BEGIN_SINK_MAP( CZipEventSink )
- SINK_ENTRY_INFO(DISPID_ZIP_SINK, DIID__IXceedZipEvents, XCD_ZIP_DISPID_QUERYMEMORYFILE, QueryMemoryFile, &QueryMemoryFile_Info)
- SINK_ENTRY_INFO(DISPID_ZIP_SINK, DIID__IXceedZipEvents, XCD_ZIP_DISPID_ZIPPINGMEMORYFILE, ZippingMemoryFile, &ZippingMemoryFile_Info)
- END_SINK_MAP()
-
- CZipEventSink( void )
- {
- m_bMustProvideFile = true;
- m_bFirstCall = true;
- }
-
- private:
- bool m_bMustProvideFile;
- bool m_bFirstCall;
- };
-
-
- int main(int argc, char* argv[])
- {
- CoInitialize( NULL );
-
- try
- {
- // Create our instance of the XceedZip control.
- IXceedZipPtr pZip( CLSID_XceedZip );
-
- // Create our event sink that will handle Xceed Zip events, and connect
- // it to our instance of XceedZip
- CZipEventSink xEventSink;
- xEventSink.DispEventAdvise( pZip );
-
- // Xceed Zip never erases a zip file. It updates at most.
- DeleteFile( ZIP_FILENAME );
-
- // Set the zip file we want to work on
- pZip->ZipFilename = ZIP_FILENAME;
-
- // You can zip regular files too! The QueryMemoryFile event is triggered
- // after processing regular files to zip. So you may want to zip regular files
- // and add a "memory" file too. In this sample, we'll only zip a memory buffer,
- // so we leave the FilesToProcess property empty!
- pZip->FilesToProcess = "";
-
- // And we start a good old zipping operation!
- pZip->Zip();
-
- xEventSink.DispEventUnadvise( pZip );
- }
- catch( const _com_error& xErr )
- {
- // The generated wrapper classes throw _com_error exceptions when a COM error
- // occurs.
-
- printf( "\nCOM Error 0x%08X ( %s ).\n", xErr.Error(), xErr.ErrorMessage() );
- }
- catch ( ... )
- {
- printf( "\nUnhandled Exception.\n" );
- }
-
- CoUninitialize();
-
- printf( "\nPress any key to quit...\n" );
- getch();
-
- return 0;
- }
-
-