You can use DotNetZip from C++/CLI
A program written in C++/CLI can take advantage of any managed library. It"s easy to use DotNetZip from a C++/CLI application. This page will show some examples.
Using C++/CLI, the key difference from VB and C#, is that there is no using statement in C++. C++ applications need to surround the use of the ZipFile class with a try..catch.. and call the ZipFile destructor, or call delete, in the finally clause.
Create a zip file
This example just creates a simple zipfile. It uses the destructor.
1using namespace System; 2using namespace Ionic::Zip; 3 4int main(array<System::String ^> ^args) 5{ 6 Console::WriteLine(L"Hello World"); 7 8 ZipFile ^ zip; 9 try 10 { 11 zip = gcnew ZipFile(); 12 zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry."); 13 zip->AddFile("CreateZipFile.cpp"); 14 zip->Save("test.zip"); 15 } 16 finally 17 { 18 zip->~ZipFile(); 19 } 20 21 Console::WriteLine(L"Press <ENTER> to quit."); 22 Console::ReadLine(); 23 return 0; 24}
This alternative uses the C++ deleee syntax:
1using namespace System; 2using namespace Ionic::Zip; 3 4int main(array<System::String ^> ^args) 5{ 6 Console::WriteLine(L"Hello World"); 7 8 ZipFile ^ zip; 9 try 10 { 11 zip = gcnew ZipFile(); 12 zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry."); 13 zip->AddFile("CreateZipFile.cpp"); 14 zip->Save("test.zip"); 15 } 16 finally 17 { 18 delete zip; 19 } 20 21 Console::WriteLine(L"Press <ENTER> to quit."); 22 Console::ReadLine(); 23 return 0; 24}
Building C++/CLI program that uses DotNetZip
Build a C++/CLI program that uses DotNetZip, just as you would build any C++/CLI program.
The easiest way is to use Visual Studio, and create a C++/CLI project. Right click on the project and select References.... Add a reference to the Ionic.Zip.dll assembly. Click OK, then build your application.
You can also build using command-line tools. To do this, you will need to compile using the c++ source using the cl.exe tool, specifying the /clr option, and specifying where to find the Ionic.Zip.dll assembly. A typical series of steps to build a simple C++/CLI program that uses DotNetZip from one source file, supposing the name of the source file is CreateZipFile.cpp, is:
\vc9\bin\cl.exe /Od /FD /EHa /MDd /Fo".\\" -I\vc9\include /W3 /c /Zi /clr /TP /FU "c:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll" /FU Ionic.Zip.dll CreateZipFile.cpp \vc9\bin\link.exe /OUT:CreateZipFile.exe /DEBUG /ASSEMBLYDEBUG /MANIFEST /MANIFESTFILE:"CreateZipFile.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /PDB:CreateZipFile.pdb /DYNAMICBASE /FIXED:No /NXCOMPAT /MACHINE:X86 /LIBPATH:\vc9\lib /LIBPATH:\winsdk\lib CreateZipFile.obj c:\netsdk2.0\Bin\mt.exe /outputresource:"CreateZipFile.exe;#1" /manifest CreateZipFile.exe.intermediate.manifest
Create a zip file using AES encryption
This example creates a zipfile, using AES 128-bit encryption to encrypt the entries.
1#include "stdafx.h" 2 3using namespace System; 4using namespace Ionic::Zip; 5 6int main(array<System::String ^> ^args) 7{ 8 Console::WriteLine(L"Hello World"); 9 10 ZipFile ^ zip; 11 try 12 { 13 zip = gcnew ZipFile(); 14 zip->Password = verySecret; 15 zip->Encryption = EncryptionAlgorithm::WinZipAes128; 16 zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry."); 17 zip->AddFile("Data.csv"); 18 zip->Save("test.zip"); 19 } 20 finally 21 { 22 zip->~ZipFile(); 23 } 24 25 Console::WriteLine(L"Press <ENTER> to quit."); 26 Console::ReadLine(); 27 return 0; 28}
Use a SaveProgress event from C++
This example creates a zipfile, and uses a SaveProgress event.
1#include "stdafx.h" 2 3using namespace System; 4using namespace System::IO; 5using namespace Ionic::Zip; 6 7 8public ref class DnzHelloCppCli 9{ 10 11private: 12 bool justHadByteUpdate; 13 14public: 15 DnzHelloCppCli() 16 { 17 } 18 19public: 20 void Run() 21 { 22 Console::WriteLine(L"Hello World"); 23 Console::WriteLine("Using DotNetZip version {0}", ZipFile::LibraryVersion); 24 array<String^>^ filesToAdd = System::IO::Directory::GetFiles(".", "*.cpp"); 25 26 ZipFile ^ zip; 27 try 28 { 29 zip = gcnew ZipFile(); 30 zip->Password = "Harbinger"; 31 zip->Encryption = EncryptionAlgorithm::WinZipAes128; 32 zip->SaveProgress += gcnew EventHandler<SaveProgressEventArgs^>(this, &DnzHelloCppCli::SaveProgress); 33 zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry."); 34 zip->AddFiles(filesToAdd, "files"); 35 zip->Save("MyArchive.zip"); 36 } 37 finally 38 { 39 zip->~ZipFile(); 40 } 41 42 Console::WriteLine(L"Press <ENTER> to quit."); 43 Console::ReadLine(); 44 return; 45 } 46 47public: 48 void SaveProgress(Object^ sender, SaveProgressEventArgs^ e) 49 { 50 switch (e->EventType) 51 { 52 case ZipProgressEventType::Saving_Started: 53 { 54 Console::WriteLine("Saving: {0}", e->ArchiveName); 55 break; 56 } 57 case ZipProgressEventType::Saving_BeforeWriteEntry: 58 { 59 if (this->justHadByteUpdate) 60 { 61 Console::WriteLine(); 62 } 63 Console::WriteLine(" Writing: {0} ({1}/{2})", 64 e->CurrentEntry->FileName, 65 (e->EntriesSaved + 1), 66 e->EntriesTotal); 67 this->justHadByteUpdate = false; 68 break; 69 } 70 case ZipProgressEventType::Saving_AfterWriteEntry: 71 { 72 if (e->CurrentEntry->InputStreamWasJitProvided) 73 { 74 e->CurrentEntry->InputStream->Close(); 75 e->CurrentEntry->InputStream = nullptr; 76 } 77 break; 78 } 79 case ZipProgressEventType::Saving_Completed: 80 { 81 this->justHadByteUpdate = false; 82 Console::WriteLine(); 83 Console::WriteLine("Done: {0}", e->ArchiveName); 84 break; 85 } 86 case ZipProgressEventType::Saving_EntryBytesRead: 87 { 88 if (this->justHadByteUpdate) 89 { 90 Console::SetCursorPosition(0, Console::CursorTop); 91 } 92 Console::Write(" {0}/{1} ({2:N0}%)", 93 e->BytesTransferred, 94 e->TotalBytesToTransfer, 95 (((double) e->BytesTransferred) / (0.01 * e->TotalBytesToTransfer))); 96 this->justHadByteUpdate = true; 97 break; 98 } 99 } 100 } 101 102}; 103 104 105int main(array<System::String ^> ^args) 106{ 107 try 108 { 109 DnzHelloCppCli^ me = gcnew DnzHelloCppCli(); 110 me->Run(); 111 } 112 catch (Exception^ ex1) 113 { 114 Console::Error->WriteLine(String::Concat("exception: ", ex1)); 115 } 116 return 0; 117}