VC++實現(xiàn)CAB壓縮解壓文件
來源:易賢網(wǎng) 閱讀:1712 次 日期:2015-08-26 15:04:01
溫馨提示:易賢網(wǎng)小編為您整理了“VC++實現(xiàn)CAB壓縮解壓文件”,方便廣大網(wǎng)友查閱!

C++實現(xiàn)CAB壓縮解壓文件 示例參考

// This is an ID which is written (invisible) into the CAB file

UINT u32_CabID = 12345;

// Store filetimes as UTC in CAB files

BOOL b_StoreUtcTime = TRUE;

// Change this to split the archive into multiple files (200000 --> CAB split size = 200kB)

// ATTENTION: In this case Parameter 1 of CreateFCIContext MUST contain "%d"

UINT u32_SplitSize = 0x7FFFFFFF;

// You can specify your own key for CAB encryption here (the longer the more secure, up to 5000 characters)

// Set empty EncryptionKey ("") to get a CAB file without encryption

char* s8_EncryptionKey = "";

//char* s8_EncryptionKey = "AH%KJ/76?KJ逪謀\獰dghf7(ZTbjasdf82iz<sx87qpc5ba&m;-@^l#";

// ########################## Initialization ############################

char s8_WinDir[MAX_PATH];

GetWindowsDirectoryA(s8_WinDir, sizeof(s8_WinDir));

char s8_Explorer[MAX_PATH];

char s8_Notepad [MAX_PATH];

sprintf(s8_Explorer, "%s\\Explorer.exe", s8_WinDir);

sprintf(s8_Notepad, "%s\\Notepad.exe", s8_WinDir);

char s8_WorkDir[MAX_PATH];

GetCurrentDirectoryA(sizeof(s8_WorkDir), s8_WorkDir);

char s8_CompressDir[MAX_PATH];

char s8_DecryptDir [MAX_PATH];

char s8_ExtractDir [MAX_PATH];

sprintf(s8_CompressDir, "%s\\_Compressed", s8_WorkDir);

sprintf(s8_DecryptDir, "%s\\_Decrypted", s8_WorkDir);

sprintf(s8_ExtractDir, "%s\\_Extracted", s8_WorkDir);

char s8_CompressFile[MAX_PATH];

strcpy(s8_CompressFile, s8_CompressDir);

strcat(s8_CompressFile, "\\Packed_%d.cab");

Cabinet::CCompress i_Compress;

Cabinet::CExtract i_ExtrDecrypt;

Cabinet::CExtractResource i_ExtrResource;

i_Compress. SetEncryptionKeyA(s8_EncryptionKey);

i_ExtrDecrypt.SetDecryptionKeyA(s8_EncryptionKey);

#ifdef UNICODE

printf("UNICODE compiled\n");

#else

printf("MBCS compiled\n");

#endif

// ########################## Compress demo ############################

// This will pack Explorer.exe and Notepad.exe into a CAB file with subfolders

printf("\n CAB FILE COMPRESSION\n\n");

if (!i_Compress.CreateFCIContextA(s8_CompressFile, b_StoreUtcTime, u32_SplitSize, u32_CabID))

{

printf("Compress ERROR: Could not create FCI context:\n%s\n", i_Compress.LastErrorA());

goto _RESOURCE;

}

if (!i_Compress.AddFileA(s8_Explorer, "FileManager\\Explorer.exe", 0))

{

printf("Compress ERROR: Could not add Explorer.exe to cabinet:\n%s\n", i_Compress.LastErrorA());

goto _RESOURCE;

}

if (!i_Compress.AddFileA(s8_Notepad, "TextManager\\Notepad.exe", 0))

{

printf("Compress ERROR: Could not add Notepad.exe to cabinet:\n%s\n", i_Compress.LastErrorA());

goto _RESOURCE;

}

if (!i_Compress.FlushCabinet(FALSE))

{

printf("Compress ERROR: Could not flush Cabinet:\n%s\n", i_Compress.LastErrorA());

goto _RESOURCE;

}

printf("SUCCESS: Compressed Explorer.exe and Notepad.exe into cabinet in\n\"%s\"\n", s8_CompressDir);

// ########################## Decrypt demo ############################

// This will decrypt the CAB file which was encrypted above

if (!s8_EncryptionKey[0]) // Skip the following if the file was not encrypted

goto _RESOURCE;

printf("\n----------------------------------------------------------------\n");

printf("\n CAB FILE DECRYPTION\n\n");

if (!i_ExtrDecrypt.CreateFDIContext())

{

printf("Decrypt ERROR: Could not create FDI context:\n%s\n", i_ExtrDecrypt.LastErrorA());

goto _RESOURCE;

}

// replace "%d" -> "1"

sprintf(s8_CompressFile, s8_CompressFile, 1);

// Now extract into subdirectory "_Decrypted" and the corresponding subdirectories in the CAB file

if (!i_ExtrDecrypt.ExtractFileA(s8_CompressFile, s8_DecryptDir))

{

printf("Decrypt ERROR: Not all files could be extracted:\n%s\n", i_ExtrDecrypt.LastErrorA());

goto _RESOURCE;

}

printf("SUCCESS: Decrypted all files from the above encrypted cabinet into\n\"%s\"\n", s8_DecryptDir);

// ########################## Extract resource demo ############################

// This will extract the file "Test.cab" in the embedded resources into a directory

_RESOURCE:

printf("\n----------------------------------------------------------------\n");

printf("\n CAB RESOURCE EXTRACTION\n\n");

if (!i_ExtrResource.CreateFDIContext())

{

printf("Extract ERROR: Could not create FDI context:\n%s\n", i_ExtrResource.LastErrorA());

goto _WAIT;

}

/*

// Now extract into subdirectory "_Extracted" and the corresponding subdirectories in the CAB file

if (!i_ExtrResource.ExtractResourceA(0, ID_CAB_TEST, "CABFILE", s8_ExtractDir))

{

printf("Extract ERROR: Not all files could be extracted:\n%s\n", i_ExtrResource.LastErrorA());

goto _WAIT;

}*/

printf("SUCCESS: Extracted all files from cabinet resource Test.cab into\n\"%s\"\n", s8_ExtractDir);

_WAIT:

// Under some cirumstances the DOS window disappears immediately, so you cannot read anything!

printf("\n\nHit any key to exit\n");

getch();

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:VC++實現(xiàn)CAB壓縮解壓文件
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)