Click here to Skip to main content
15,895,709 members
Articles / Web Development / HTML

Cabinet File (*.CAB) Compression and Extraction

Rate me:
Please Sign up or sign in to vote.
4.93/5 (217 votes)
23 Mar 2012CPOL38 min read 3.2M   26.5K   573  
How to implement creation and extraction of Microsoft CAB files
// LibCompress.h

#pragma once

#include "CompressT.hpp"

using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Collections;

namespace CabLib
{
	public __gc class Compress
	{
	public:
		__gc struct kCurStatus
		{
			UINT  u32_TypeStatus;          // statusFile or statusFolder or statusCabinet
			ULONG u32_TotCompressedSize;   // Total bytes which have been written to CAB file
			ULONG u32_TotUncompressedSize; // Total bytes which have been written to CAB file
			ULONG cb1;                     // for details see: "Cabinet Doku.doc"
			ULONG cb2;                     // for details see: "Cabinet Doku.doc"
			ULONG u32_FolderPercent;       // valid for statusFolder: 0% =start of compression, 100% =ready

			~kCurStatus() {}       // Destructor is required!
		};

		Compress();

		static void EnumFiles(ArrayList* i_FileList, String *s_Path, String *s_Filter, bool b_Subfolders);

		void SetTempDirectory(String* s_TempDir);
		void SetEncryptionKey(String* s_Key);
		void CompressFileList(ArrayList* i_FileList, String* s_CabFile, bool b_UtcTime, int s32_SplitSize);
		void CompressFolder  (String* s_Folder, String* s_CabFile, String* s_Filter, bool b_UtcTime, int s32_SplitSize);
		
		__delegate int  delUpdateStatus(kCurStatus *pk_CurStatus);
		__delegate int  delFilePlaced(String *s_File, int s32_FileSize, bool bContinuation);

		__event delUpdateStatus  *evUpdateStatus;
		__event delFilePlaced    *evFilePlaced;

		// event handler must be static!
		static BOOL OnGetNextCabinet(PCCAB pccab, ULONG cbPrevCab, char* s8_CabNameFormatter, void* p_Param);
		static int  OnFilePlaced(PCCAB pccab, char *pszFile, long cbFile, BOOL fContinuation, void* p_Param);
		static long OnUpdateStatus(kCurStatus *pk_CurStatus, void*p_Param);

	private:
		static void    RecursiveEnumFiles(ArrayList* i_FileList, String* s_Path, String* s_Filter, bool b_Subfolders);
		static String* TerminatePath(String* s_Path);

		static Compress* mp_This  = 0;
		static Mutex*    mi_Mutex = new Mutex();

		String* ms_TempDir;
		String* ms_Encrypt;
	};
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) ElmüSoft
Chile Chile
Software Engineer since 40 years.

Comments and Discussions