Click here to Skip to main content
15,880,543 members
Articles / Programming Languages / Java
Article

Encrypted Zipping of Files in C# and Java

Rate me:
Please Sign up or sign in to vote.
4.47/5 (9 votes)
14 Sep 2008CPOL3 min read 75.1K   5.3K   55   8
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):

C#
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:

C#
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:

C#
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)


Written By
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

 
QuestionDecrypting and encrypted zipped file Pin
Member 1280282219-Oct-16 4:53
Member 1280282219-Oct-16 4:53 
GeneralThank you and bug fix Pin
asvet25-Apr-09 22:38
asvet25-Apr-09 22:38 
GeneralOne appointment Pin
Cierzo15-Oct-08 1:37
Cierzo15-Oct-08 1:37 
GeneralRe: One appointment [modified] Pin
Neill Harper15-Oct-08 9:06
Neill Harper15-Oct-08 9:06 
Generalplease do not use ASCII encoding Pin
Huisheng Chen15-Sep-08 18:50
Huisheng Chen15-Sep-08 18:50 
GeneralRe: please do not use ASCII encoding Pin
Neill Harper16-Sep-08 7:20
Neill Harper16-Sep-08 7:20 
Questionextract? Pin
Huisheng Chen15-Sep-08 18:44
Huisheng Chen15-Sep-08 18:44 
AnswerRe: extract? Pin
Neill Harper16-Sep-08 7:17
Neill Harper16-Sep-08 7:17 

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

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