File password encryption is simple, please refer to the following code.
[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 = "test";
//add a single file to zip file.
oZip.AddFile( "c:\\temp\\test.gif", "", password );
}
catch( Exception ep )
{
Console.Write( ep.Message );
}
}
Decryption of a single file from a zip archive is easy, please refer to the following code.
[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 = "test";
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 );
}
}
Note: Only one password can be set in ZipArchive.ExtractAll method. If the files in a zip archive have different password, OnPasswordError event should be used. Please refer to the following sample.
[C#]
using System;
using System.Collections;
using EACompression;
private void OnPasswordError(
object sender,
ZipFile z,
ref string password )
{
// here frm2 is a form for user to input password, please refer to the total source code
// in zip.csharp sample.
Form2 frm2 = new Form2();
frm2.lblPrompt.Text = String.Format( "Password is incorrect, please retype password",
z.Name );
if( frm2.ShowDialog() == DialogResult.OK )
{
//input a new password to decrypt, if the password is not changed in this event,
//an InvalidZipPasswordException exception will be thrown.
password = frm2.textPassword.Text;
}
}
void Unzip( )
{
try
{
ZipArchive oZip = new ZipArchive( "TryIt" );
oZip.OnPasswordError += new ZipArchive.OnPasswordErrorEventHandler( OnPasswordError );
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 );
}
}
See Also
Using EACompression Zip Component
Handle file overwriting and path structure
EACompression Namespace References
EACompression Zip Component Samples