Click here to Skip to main content
14,355,618 members

#zlib - Modifying Archives

Rate this:
4.35 (9 votes)
Please Sign up or sign in to vote.
4.35 (9 votes)
14 May 2007
A way to modify Zip archives without extracting them completely.

Demo app


This article shortly explains some classes that give you the possibility to modify an archive without completely extracting it. The way I solved it is not really beautiful, but it works. I used a mix of the existing C# wrapper code for zlib (together with minizip) and some of my own code. A better way would be writing a wrapper class for libzip. And probably, the best way would be enhancing SharpZipLib so that it can modify archives. So feel free to work on that ;-).

Also note that these classes do not support enhanced settings like compression levels or archive comments because I didn't need them. But you could easily add these features.


Everyone who needs compression/decompression for C# most probably would use the SharpZipLib from IC#Code. I also did that. Until now. I have been using that library for compressing the data my program generates. Fortunately, the output was all of the same type, so I was able to save it in the same file. But now I have different types of data that belong to different parts of the program, so splitting up the data into files would be a good way to achieve this. I wanted to modify this archive in memory, because it is rather ugly to completely extract it. I've been searching around for a while, and I only found an old C# wrapper class from Gerry Shaws. I knew that SharpZipLib is no solution for me, so I downloaded it and tried to get it working. There were some bugs in it that caused some weird behaviour which vanished after I replaced all the unsafe parts with normal C# code (you have to now, Gerry's code is a little bit weird ;). And well, besides that, I adjusted the code heavily to my needs. I replaced his rather hard to handle ZipReader and ZipWriter classes with the ZipStream and ZipArchive classes, and that allows a way more easier way to read and write to an archive. It kind of hides the ugliness of the unmanaged zlib functions :P.

Using the code

I have made modifying of the archives (hopefully) very simple. You just need to instantiate an Archive class that gives you access to an array of streams.

// Don't forget to add this two lines
using System.IO;
using OrganicBit.Zip;

ZipArchive Archive = new ZipArchive("", FileAccess.ReadWrite);

byte[] Data = new byte[Archive["test.txt"].Length];


Console.WriteLine("Contents of 'test.txt'\n"+ 
Console.WriteLine("Contents of the new file:");

Data = System.Text.Encoding.Default.GetBytes(Console.ReadLine());



Notice that even if does not exist, you have to open it with FileAccess.Write, and then a new archive will be created automatically. Also, when you write to an existing archive and the file is not in the archive yet, it will be created.

To see what is in an archive, you can simply enumerate your instance:

// Besides the Name, a ZipEntry
// instance also offers lots of other information
foreach(ZipEntry entry in Archive) 

You can also check whether a file exists, or delete some:

  Console.WriteLine("File test.txt is there");

// Archive.Delete() deletes the whole archive
// when it doesn't contain any other files
bool success = Archive.DeleteFile("test.txt");
  Console.WriteLine("File has been deleted");
else Console.WriteLine("File is not in archive");


  • Can only create/modify .zip archives.
  • Hard-coded settings (comment/compression level/etc.).
  • No encryption.
  • Most probably limited to 2 GB archives or even less.

Points of interest

The unlovely part of zlib together with minizip is the fact that you can't open an archive for reading and writing simultaneously. I hid that fact in my classes, and exposed only the Read and Write functions. But behind that, my class closes and reopens the archive whenever you switch from a read to a write function or vice versa. Second, zlib/minizip do not support any possibility to modify and/or delete files. But there is a link on the minizip homepage to a small example in C++ on how to delete files, written by Ivan A. Krestinin. I just converted that function to C#, and added it to my ZipArchive class. So, whenever you write to an existing entry, the old file gets deleted first!


...go to the following people:

  • The zlib and minizip developers.
  • Gerry Shaw's C# Wrapper for zlib, on which my work is based on.
  • Ivan A. Krestinin, for his example on how to delete files from an archive.


  • 2006-04-08
    • Inserted the missing LGPL header.
    • Fixed two severe bugs.
  • 2006-04-25
    • Article creation.


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


About the Author

Austria Austria
No Biography provided

Comments and Discussions

GeneralThanks, and BadImageFormatExceptions Pin
AMagill17-Jul-07 9:36
memberAMagill17-Jul-07 9:36 
QuestionHow to load image from zip to picturebox? c# Pin
dejau12-May-07 13:50
memberdejau12-May-07 13:50 
AnswerRe: How to load image from zip to picturebox? c# Pin
TyronM13-May-07 2:57
memberTyronM13-May-07 2:57 
Generalzlib.dll Pin
the-unforgiven11-May-07 9:25
memberthe-unforgiven11-May-07 9:25 
GeneralRe: zlib.dll Pin
TyronM12-May-07 5:05
memberTyronM12-May-07 5:05 
GeneralRe: zlib.dll Pin
the-unforgiven13-May-07 11:03
memberthe-unforgiven13-May-07 11:03 
GeneralRe: zlib.dll Pin
TyronM14-May-07 0:23
memberTyronM14-May-07 0:23 
Questioncreate new zip file Pin
hamid_m1-Oct-06 8:13
memberhamid_m1-Oct-06 8:13 
AnswerRe: create new zip file Pin
TyronM20-Oct-06 10:17
memberTyronM20-Oct-06 10:17 
GeneralRe: create new zip file Pin
hamid_m20-Oct-06 11:29
memberhamid_m20-Oct-06 11:29 
GeneralWrong solution file Pin
TyronM28-Apr-06 8:09
memberTyronM28-Apr-06 8:09 

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.

Posted 27 Apr 2006


53 bookmarked