Using EACompression Zip Component


Importance Notice: To use EACompression in .NET Compact Framework 2.0, please use EACompressionCF2.dll instead of EACompression.dll

Add Reference of EACompression to Visual Stuido.NET Project

To use EACompression Zip Component in your project, the first step is to "Add reference of EACompression to your project". Create/open a new project with Visual Studio.NET, choose menu -> "Project" -> "Add Reference" -> ".NET" -> "Browse...", and select EACompression.dll from your disk, click "Open" -> "OK", the reference of EACompression will be added to your project, and you can start to use EACompression Zip Component in your project.

Deploying EACompression with Application

After compiling your project, a copy of EACompression.dll will be generated by compiler in the same folder of your application executable file. You just need to pack all the *.dll and *.exe in the folder to installer. As EACompression is a pure .NET Component, it doesn't require "Regsvr32" (self-register) to register the dll.

Deploying EACompression with ASP.NET/Web Application

The EACompression.dll should be copied to [website root folder]\bin folder or [virtual directory root]\bin folder. If the project is created by Visual Studio.NET + FrontPage Extension directly, Visual Studio.NET will deploy EACompression.dll automatically.

Example

[Visual Basic, C#, C++] The following example demonstrates how to compress/decompress file with EACompression Zip Component. This sample doesn't demonstrates the events, please refer to Samples section to get the full samples of EACompression.

[Visual Basic]
Imports EACompression

Sub Unzip()
    Try
        Dim oZip As New ZipArchive("TryIt")
        oZip.Load("c:\test.zip")
        Dim zs() As ZipFile = oZip.Files
        Dim count As Integer = zs.Length
        Dim password As String = ""
        Dim overwrite As Boolean = True

        For i As Integer = 0 To count - 1
            Dim z As ZipFile = zs(i)
            oZip.ExtractTo(z, password, "c:\unzipped", overwrite)
        Next

        'another way is:
        'oZip.ExtractAll( "c:\uuzipped", password ) 

        'if you want to remove one of the file from this zip archive.
        'oZip.Remove( zs(0) )
    Catch ep As Exception
        Console.Write(ep.Message)
    End Try
End Sub

Sub Zip()
    Try
        Dim oZip As New ZipArchive("TryIt")
        oZip.Create("c:\test.zip", True)   'create a new zip file
        Dim password As String = ""
        'add a single file to zip file.
        oZip.AddFile("c:\test.gif", "test.gif", password)
        'add all files and sub-directory in temp folder to zip archive
        oZip.AddDirectory("c:\temp", password)
    Catch ep As Exception
        Console.Write(ep.Message)
    End Try
End Sub

[C#]
using System;
using System.Collections;
using EACompression;

void Unzip( )
{
    try
    {
        ZipArchive oZip = new ZipArchive( "TryIt" );
        oZip.Load( "c:\\test.zip" );

        ZipFile [] zs = oZip.Files;
        int count = zs.Length;
        string password = "";
        bool overwrite = true;
        for( int i = 0; i < count; i++ )
        {
            ZipFile z = zs[i];
            oZip.ExtractTo( z, password, "c:\\unzipped", overwrite );
        }

        //another way is:
        //oZip.ExtractAll( "c:\\uuzipped", password ); 

        //if you want to remove one of the file from this zip archive.
        //oZip.Remove( zs[0] );
    }
    catch( Exception ep )
    {
        Console.Write( ep.Message );
    }
}

void Zip()
{
    try
    {
        ZipArchive oZip = new ZipArchive( "TryIt" );
        oZip.Create( "c:\\test.zip", true ); //create a new zip file;
        string password = "";
        //add a single file to zip file.
        oZip.AddFile( "c:\\test.gif", "test.gif", password );
        //add all files and sub-directory in temp folder to zip archive
        oZip.AddDirectory( "c:\\temp", password ); 
    }
    catch( Exception ep )
    {
        Console.Write( ep.Message );
    }
}

[C++]
using namespace System;
using namespace System::Collections;
using namespace EACompression; 

void Unzip( )
{
    try
    {
        ZipArchive *oZip = new ZipArchive( S"TryIt" );
        oZip->Load( S"c:\\test.zip" );

        ZipFile *zs[] = oZip->Files;
        int count = zs->Length;
        String *password = S"";
        bool overwrite = true;
        for( int i = 0; i < count; i++ )
        {
            ZipFile *z = zs[i];
            oZip->ExtractTo( z, password, S"c:\\unzipped", overwrite );
        }

        //another way is:
        //oZip->ExtractAll( S"c:\\uuzipped", password ); 

        //if you want to remove one of the file from this zip archive.
        //oZip->Remove( zs[0] );
    }
    catch( Exception *ep )
    {
        Console::Write( ep->Message );
    }
}

void Zip()
{
    try
    {
        ZipArchive *oZip = new ZipArchive( S"TryIt" );
        oZip->Create( S"c:\\test.zip", true ); //create a zip file;
        String *password = S"";
        //add a single file to zip file.
        oZip->AddFile( S"c:\\test.gif", S"test.gif", password );
        //add all files and sub-directory in temp folder to zip archive
        oZip->AddDirectory( S"c:\\temp", password ); 
    }
    catch( Exception *ep )
    {
        Console::Write( ep->Message );
    }
}

See Also

Handle file overwriting and path structure
Encrypt and decrypt file with password
EACompression Namespace References
EACompression Zip Component Samples