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;
ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip");
FileEntry file = new FileEntry("D:\\text.txt",Method.Stored);
zip.AddFile(file);
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;
D:\\myfirstZip.zip
ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip");
FileEntry file = new FileEntry("D:\\text.txt",Method.Deflated);
zip.AddFile(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;
ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip");
FileEntry file = new FileEntry("D:\\text.txt", "HARPER",Method.Deflated);
zip.AddFile(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