Click here to Skip to main content
Click here to Skip to main content
Go to top

Encrypted Zipping of Files in C# and Java

, 14 Sep 2008
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Neill Harper
Software Developer (Junior)
United Kingdom United Kingdom
I am an iSeries Synon/RPG programmer, who likes to program in C# and Java in my spare time.

Comments and Discussions

 
GeneralThank you and bug fix Pinmemberasvet25-Apr-09 22:38 
GeneralOne appointment PinmemberCierzo15-Oct-08 1:37 
GeneralRe: One appointment [modified] PinmemberNeill Harper15-Oct-08 9:06 
Generalplease do not use ASCII encoding PinmemberUnruled Boy15-Sep-08 18:50 
GeneralRe: please do not use ASCII encoding PinmemberNeill Harper16-Sep-08 7:20 
Questionextract? PinmemberUnruled Boy15-Sep-08 18:44 
AnswerRe: extract? PinmemberNeill Harper16-Sep-08 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 14 Sep 2008
Article Copyright 2008 by Neill Harper
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid