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.


using namespace System;
using namespace Ionic::Zip;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

    ZipFile ^ zip;
    try
    {
        zip = gcnew ZipFile();
        zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry.");
        zip->AddFile("CreateZipFile.cpp");
        zip->Save("test.zip");
    }
    finally
    {
        zip->~ZipFile();
    }

    Console::WriteLine(L"Press <ENTER> to quit.");
    Console::ReadLine();
    return 0;
}

This alternative uses the C++ deleee syntax:


using namespace System;
using namespace Ionic::Zip;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

    ZipFile ^ zip;
    try
    {
        zip = gcnew ZipFile();
        zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry.");
        zip->AddFile("CreateZipFile.cpp");
        zip->Save("test.zip");
    }
    finally
    {
        delete zip;
    }

    Console::WriteLine(L"Press <ENTER> to quit.");
    Console::ReadLine();
    return 0;
}

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.


#include "stdafx.h"

using namespace System;
using namespace Ionic::Zip;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

    ZipFile ^ zip;
    try
    {
        zip = gcnew ZipFile();
        zip->Password = verySecret;
        zip->Encryption = EncryptionAlgorithm::WinZipAes128;
        zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry.");
        zip->AddFile("Data.csv");
        zip->Save("test.zip");
    }
    finally
    {
        zip->~ZipFile();
    }

    Console::WriteLine(L"Press <ENTER> to quit.");
    Console::ReadLine();
    return 0;
}

Use a SaveProgress event from C++

This example creates a zipfile, and uses a SaveProgress event.



#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace Ionic::Zip;


public ref class DnzHelloCppCli
{

private:
    bool justHadByteUpdate;

public:
    DnzHelloCppCli()
        {
        }

public:
    void Run()
        {
            Console::WriteLine(L"Hello World");
            Console::WriteLine("Using DotNetZip version {0}", ZipFile::LibraryVersion);
            array<String^>^ filesToAdd = System::IO::Directory::GetFiles(".", "*.cpp");

            ZipFile ^ zip;
            try
            {
                zip = gcnew ZipFile();
                zip->Password = "Harbinger";
                zip->Encryption = EncryptionAlgorithm::WinZipAes128;
                zip->SaveProgress += gcnew EventHandler<SaveProgressEventArgs^>(this, &DnzHelloCppCli::SaveProgress);
                zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry.");
                zip->AddFiles(filesToAdd, "files");
                zip->Save("MyArchive.zip");
            }
            finally
            {
                zip->~ZipFile();
            }

            Console::WriteLine(L"Press <ENTER> to quit.");
            Console::ReadLine();
            return;
        }

public:
    void SaveProgress(Object^ sender, SaveProgressEventArgs^ e)
        {
            switch (e->EventType)
            {
                case ZipProgressEventType::Saving_Started:
                {
                    Console::WriteLine("Saving: {0}", e->ArchiveName);
                    break;
                }
                case ZipProgressEventType::Saving_BeforeWriteEntry:
                {
                    if (this->justHadByteUpdate)
                    {
                        Console::WriteLine();
                    }
                    Console::WriteLine("  Writing: {0} ({1}/{2})",
                                       e->CurrentEntry->FileName,
                                       (e->EntriesSaved + 1),
                                       e->EntriesTotal);
                    this->justHadByteUpdate = false;
                    break;
                }
                case ZipProgressEventType::Saving_AfterWriteEntry:
                {
                    if (e->CurrentEntry->InputStreamWasJitProvided)
                    {
                        e->CurrentEntry->InputStream->Close();
                        e->CurrentEntry->InputStream = nullptr;
                    }
                    break;
                }
                case ZipProgressEventType::Saving_Completed:
                {
                    this->justHadByteUpdate = false;
                    Console::WriteLine();
                    Console::WriteLine("Done: {0}", e->ArchiveName);
                    break;
                }
                case ZipProgressEventType::Saving_EntryBytesRead:
                {
                    if (this->justHadByteUpdate)
                    {
                        Console::SetCursorPosition(0, Console::CursorTop);
                    }
                    Console::Write("     {0}/{1} ({2:N0}%)",
                                   e->BytesTransferred,
                                   e->TotalBytesToTransfer,
                                   (((double) e->BytesTransferred) / (0.01 * e->TotalBytesToTransfer)));
                    this->justHadByteUpdate = true;
                    break;
                }
            }
        }

};


int main(array<System::String ^> ^args)
{
    try
    {
        DnzHelloCppCli^ me = gcnew DnzHelloCppCli();
        me->Run();
    }
    catch (Exception^ ex1)
    {
        Console::Error->WriteLine(String::Concat("exception: ", ex1));
    }
    return 0;
}