Password Encrypted Zip files in C#

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


Similar Articles