SIGN UP MEMBER LOGIN:    
ARTICLE

Password Encrypted Zip files in C#

Posted by neill harper Articles | Java and .NET October 27, 2008
This article contains the source code for creating Zip files in C# that are password protected using PKWare encryption.
Reader Level:
Download Files:
 

This article provides the source code for creating zip files in C# and Java.

There are only really two main classes on interest:

ZIP File: This object represents a ZIP file, which can contain one or more other files (i.e. text files, jpegs anything (I think...).

File Entry: This object represents a file stored inside a ZIP File. The File Entry can be stored as is, compressed, as is encrypted, compressed encrypted. This is controlled by the constructor called to create the File Entry.

Examples:

Creating a ZIP file with one file in it, using Stored (i.e. no compression) and no password i.e. not encrypted.

using System;
using System.Collections.Generic;
using System.Text;
using ZipFramework;

//Creates a ZipFile object that will ultimately be saved at D:\\myfirstZip.zip

ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip");

//Creates a FileEntry object that represents the file D:\\text.txt
// the file will NOT be compressed or have a password

FileEntry file = new FileEntry("D:\\text.txt",Method.Stored);

//Add the FileEntry object to the ZIPFile object

zip.AddFile(file);

//Finally actually create the Zip file

zip.CreateZIP();
 

Creating a ZIP file with one file in it, using Deflate compression without a password.

using System;
using System.Collections.Generic;
using System.Text;
using ZipFramework;

//Creates a ZipFile object that will ultimately be saved at D:\\myfirstZip.zip

ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip");

//Creates a FileEntry object that represents the file D:\\text.txt
// the file will be compressed using the deflate algorithm (note this is the only algorithm the library supports)

FileEntry file = new FileEntry("D:\\text.txt",Method.Deflated);

//Add the FileEntry object to the ZIPFile object

zip.AddFile(file);

//Finally actually create the Zip file

zip.CreateZIP();

Final example:

Creating a ZIP file with one file in it, using deflate compression and encrypted with a password

using System;
using System.Collections.Generic;
using System.Text;
using ZipFramework;

//Creates a ZipFile object that will ultimately be saved at D:\\myfirstZip.zip

ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip");

//Creates a FileEntry object that represents the file D:\\text.txt
// the file will be compressed using the deflate algorithm (note this is the only algorithm the library supports) and the file will also be encrypted with the supplied password.

FileEntry file = new FileEntry("D:\\text.txt", "HARPER",Method.Deflated);

//Add the FileEntry object to the ZIPFile object

zip.AddFile(file);

//Finally actually create the Zip file

zip.CreateZIP();

Note the Add File (file) method can be called multiple times to add as many file to the ZIP File as you like. Those files can have different compression methods and passwords to other files.

You can see from the source that the C# version is a lot easier to write/code than the Java version as it uses properties instead of getter and setter methods (yes I know the CLR uses getters and setters in the background!! but that's hidden from me in c# yeah!!)

Also Java does not support unsigned data types, which to be honest was a bit of a pain... in places I have just used the signed equivalents (this is pretty dangerous) and in others I have used the signed version up i.e. UInt16 (2 byte) in java is replaced with long (4 bytes). This meant a lot of & 0xFFFF etc style bit masking... apart from that the two libraries are pretty similar.

I don't know why but the C# version performs much (really loads) better than the java version when encryption is involved. Encrypting a 16meg file on the C# version takes 2-3 Seconds, the same file on the Java version takes 2-3 minutes.

I really don't know why this is and haven't been able to improve the performance of the Java version... I'd really welcome feed back on that (surely C# can't really be that much faster, can it???)

I'd really like it, if you took the source code (C#) to pieces, show me all of the school boy errors I've made and help me make the C# version even faster, also feel free to enhance, ie. add support of unzipping etc..

One area where I think real improvements can be made is in the deflate and encryption. You will see in the source that FileEntry keeps the compressed version in temporary files, could this be done in memory (even for big files) or is there a fancy memory file buffer in C# that I don't know of.

Large parts of the CRC (Cyclic Redundancy Check) are implemented using the java.util.zip.CRC32 class (yes even in the C# version, luckily Microsoft implements this as part of its J# support) You can get access to many java classes in .Net by adding vjslib as a reference to your project. (Note CRC is the only java class I use the rest is in C#)

Anyway I hope you enjoy using and improving this source as much as I enjoyed writing it.
Finally for more info on the Zip standard see http://www.pkware.com/documents/casestudies/APPNOTE.TXT

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

Hi Anish

Are you using the C# or the java version. and what software are you using to open the encrypted zip file?

Posted by neill harper Sep 25, 2009

Hi Neil,
I couldnt extract the encrypted files,It shows corrupted can u pls find out some probale causes for this.
This is only when i uses encryption

Posted by anish ps Sep 25, 2009

There is an open-source library that does this: DotNetZip. It does PKZIP encryption. It does Unicode. You can read an existing zip file. Create new ones. Modify existing ones. Add or remove entries. It produces self-extracting archives. The API for DotNEtZip is similar to what you have done here - there is ZipFile class (which is IDisposable) with methods like AddFile() AddDirectory() and so on. You can set a password on a file or on an archive. It zips to or from streams or files. It unzips to or from streams or files. See http://www.codeplex.com/DotNetZip

Posted by dino Nov 25, 2008

Hi the password for each zip file is set, on the constructor for each ZipFileEntry.

 

I haven't been able to do any more work on this API lately, mainly because my girlfriend dropped a cup of tea on my laptop..... I have learned a lesson about backing up source code.

 

 

Posted by neill harper Nov 25, 2008

Eventhough i did not look at the zipframeweok, but , by just looking at your sample, i like your design. the resposibility is clear, the system is easy to use. From your sample i coiuld not figure out where do you set the zip password, but that is not an issue. The zip file have two resposibilities the first one is a collection for file entry and the second one is zipping responsibility. at this stage stage this looks good since you just have AddFile for your collection, but later may be you need to refactor that. good design and good luck.

Posted by Mohammad Shalabi Nov 25, 2008
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor