Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Encrypted Zipping of Files in C# and Java

0.00/5 (No votes)
14 Sep 2008 1  
Source code to create a compressed, encrypted password protected zip file in C# and Java

Introduction

This article provides the source code for a zipping framework in both, C# and Java. The library is not complete but does implement everything required to create a zip file (optionally compressed) with multiple files that are optionally password encrypted.

Background

I wanted to understand how zip files work and build a library that could create zip files. I also enjoy comparing and contrasting different programming languages. So I decided to create this library in C# and Java. I do intend some day to do the library in C++... but I am aware that there are a lot of C++ zip libraries already.

Using the Code

There are only really two main classes of interest:

  • ZIPFile: This object represents a ZIP file, which can contain one or more other files (i.e. text files, JPEGs, anything (I think...).
  • FileEntry: This object represents a file stored inside a ZIP file. The FileEntry can be stored as is, compressed, as is encrypted, compressed encrypted. This is controlled by the constructor called to create the FileEntry.

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(); 

Points of Interest

I learnt a lot about the differences between C# and Java, especially that C# properties are really, really easier to code as opposed to Java getters and setters. For a long time, the C# version was much faster than the Java version. (i.e. for a 16 meg encrypted file C# 2-3 seconds, Java = 2-3 mins). I finally tracked this down to IO and it seems in C# the FileStream object takes care of buffering for you?? whereas in Java, you have to wrap the stream in a buffer stream. Nonetheless, the C# version is still slightly faster than the Java version (but this is probably due to my coding, rather than major differences in C# and Java).

Another strange thing that I can't explain is that when using the Deflate compression the Java code is much better at compressing the data (this worries me as I would expect C# and Java deflating to be exactly the same... but evidence suggests this is not the case (if anyone can explain this, please let me know.) Java does not support unsigned data types which was a real pain, and I am not 100% sure that my Java implementation is bullet proof because of this. This library really is just enough to get a zip file out that works. If anyone want to enhance (especially where I have used non standard coding practices) then please do so. Also if anyone wants to add unzipping, then that would be great!!)

History

  • 14th September, 2008: First version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here