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
sadsa sadasPosted Sep 6, 2015, 12:49 PM
THIS IS JAVA NOT C# !!!!!!
anujkumar agrahariPosted Jul 24, 2012, 8:26 AM
hisdd
anujkumar agrahariPosted Jul 24, 2012, 8:25 AM
hisdd
anujkumar agrahariPosted Jul 24, 2012, 8:25 AM
hisdd
anujkumar agrahariPosted Jul 24, 2012, 8:25 AM
hisdd
Priyadarshini BeditedPosted Jul 15, 2012, 2:06 PMEdited Jul 15, 2012, 2:07 PM
DotNetZip library does great work. It is very easy, simple and fast as well. I used it in my data analyzing utility to analyze files inside zip files and found it to be more fast & efficient than what I expected. Check this out, http://programmingfree.blogspot.in/2012/07/create-analyze-and-manipulate-zip-files.html
anish psPosted Sep 25, 2009, 3:50 AM
Hi Neil, I couldnt extract the encrypted files,It shows corrupted can u pls find out some probale causes for this. This is only when i uses encryption
dinoPosted Nov 25, 2008, 11:02 PM
There is an open-source library that does this: DotNetZip. It does PKZIP encryption. It does Unicode. You can read an existing zip file. Create new ones. Modify existing ones. Add or remove entries. It produces self-extracting archives. The API for DotNEtZip is similar to what you have done here - there is ZipFile class (which is IDisposable) with methods like AddFile() AddDirectory() and so on. You can set a password on a file or on an archive. It zips to or from streams or files. It unzips to or from streams or files. See http://www.codeplex.com/DotNetZip
Mohammad ShalabiPosted Nov 25, 2008, 12:46 AM
Eventhough i did not look at the zipframeweok, but , by just looking at your sample, i like your design. the resposibility is clear, the system is easy to use. From your sample i coiuld not figure out where do you set the zip password, but that is not an issue. The zip file have two resposibilities the first one is a collection for file entry and the second one is zipping responsibility. at this stage stage this looks good since you just have AddFile for your collection, but later may be you need to refactor that. good design and good luck.
EugenioPosted Oct 14, 2008, 5:52 AM
C#Source.zip link must be broken