Handle file overwriting and path structure


File Overwriting

If you extract files from zip archives to local disk, you should consider file overwriting problem. The following code sets the "overwrite" parameter to true. If the file already exists, the ExtractTo method overwrites the file. If the "overwrite" parameter is set to false, if the file already exists, the ExtractTo method will throw an exception.

Example Codes

[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];
            if( !z.IsDirectory )
                oZip.ExtractTo( z, password, "c:\\unzipped", overwrite );
        }
    }
    catch( Exception ep )
    {
        Console.Write( ep.Message );
    }
}

To make your applicaton more user friendly, you are suggested to use the following code.

Example Codes

[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];
            if( !z.IsDirectory )
            {
                string fullname = String.Format( "c:\\unzipped\\{0}", z.Name );
                if( File.Exists( fullname ))
                {
                    if( MessageBox.Show( String.Format( "File {0} is existed,\r\n are you sure to overwrite it ",
                        z.Name ), "", MessageBoxButtons.YesNo ) == DialogResult.No )
                    {
                        // do not overwrite this file and continue to next file.
                        continue;
                    }
                }
                oZip.ExtractTo( z, password, "c:\\unzipped", overwrite );
            }
        }
    }
    catch( Exception ep )
    {
        Console.Write( ep.Message );
    }
}

However, ZipArchive.ExtractAll method doesn't provide a "overwrite" parameter, by default ExtractAll method will overwrite existed file. If you want to ask the user before overwritting the file, you should use OnOverwrite event. Please refer to the following sample code.

Example Codes

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

private void OnOverwrite( 
    object sender, 
    ZipFile z, 
    string target,
    ref bool overwrite )
{

    if( MessageBox.Show( String.Format( "File {0} is existed,\r\n are you sure to overwrite it ",
        z.Name ), "", MessageBoxButtons.YesNo ) == DialogResult.Yes )
    {
        overwrite = true;
    }
    else
    {
        overwrite = false;
    }
}

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

        ZipFile [] zs = oZip.Files;
        string password = "";
        bool overwrite = true;
        oZip.ExtractAll( "c:\\unzipped", password ); 
    }
    catch( Exception ep )
    {
        Console.Write( ep.Message );
    }
}

Path Structure

If you add a single file to zip archive, you don't have to consider the path structure.

Example Codes

[C#]
using System;
using System.Collections;
using EACompression;
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:\\temp\\test.gif", "", password );
    }
    catch( Exception ep )
    {
        Console.Write( ep.Message );
    }
}

However, if you want to keep the path structure in zip archive, you should use the following code.

Example Codes

[C#]
using System;
using System.Collections;
using EACompression;
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:\\temp\\test.gif", "temp\\test.gif", password );
    }
    catch( Exception ep )
    {
        Console.Write( ep.Message );
    }
}

If you use ZipArchive.AddDirectory method, the path structure will be keeped automatically.

Compare the following code to the above one to see the difference of ZipArchive.ExtractTo method.

Example Codes

[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;
        bool keepStructure = true;
        for( int i = 0; i < count; i++ )
        {
            ZipFile z = zs[i];
            if( !z.IsDirectory )
            {
                oZip.ExtractTo( z, password, "c:\\unzipped", overwrite, keepStructure );
                // suppose the path structure of this file is temp\test.gif
                // if keepStructure is true, this file will be saved to c:\unzipped\temp\test.gif
                // if keepStructure is false, this file will be saved to c:\unzipped\test.gif
            }
        }
    }
    catch( Exception ep )
    {
        Console.Write( ep.Message );
    }
}

ZipArchive.ExtractAll method always keeps the path structure.

See Also

Using EACompression Zip Component
Encrypt and decrypt file with password
EACompression Namespace References
EACompression Zip Component Samples