Click here to Skip to main content
14,846,774 members
Articles / Programming Languages / C#
Posted 12 Jun 2011

Tagged as


150 bookmarked

C# Use Zip Archives without External Libraries

Rate me:
Please Sign up or sign in to vote.
4.92/5 (80 votes)
12 Jun 2011CPOL3 min read
C# use Zip archives without external libraries


I found a lot of articles on how to access Zip archives in C# but all with significant disadvantages. The main problem is that Microsoft has Zip archives implemented in the operating system but there is no official API that we can use. In C# for example, we have the System.IO.Compression.GZip but there is no adequate System.IO.Compression.Zip class.

There are some free .NET compression libraries like SharpZipLib and .NET Zip Library, but this leads to additional installation effort and licensing problems.

It is also possible to use the free J# Library. J# has included Zip to keep compatible with the Java libraries. But to bundle a 3.6 MB DLL vjslib.dll, just to support Zip, seems like a really goofy hack.

Since .NET 3.0, we can use the System.IO.Packaging ZipPackage class in WindowsBase.DLL. It's just 1.1 MB, and it just seems to fit a lot better than importing Java libraries.

Problem only that the ZipPackage class isn't a generic Zip implementation, it's a packaging library for formats like XPS and Office Open XML that happen to use Zip.

To access simple Zip archives with ZipPackage fails because the content is checked for Package conventions.

For example, there has to be a file [Content_Types].xml in the root and only files with specified extensions are accessible. Filenames with special characters and spaces are not allowed and the access time is not the best because of the additional Package link logic.

However, the assembly WindowsBase.DLL is preinstalled and the generic Zip implementation is inside. The only problem is that the generic Zip classes are not public and visible for the programmers. But there is a simple way to get access to this hidden API and I wrote a small wrapper class for this.


A quick check in the Object Browser shows us that WindowsBase.DLL has a namespace MS.Internal.IO.Zip. This sounds good, but there are no public classes visible.

However, the following call:

var types = typeof(System.IO.Packaging.Package).Assembly.GetTypes();

gives us 824 class types, public and non-public and especially one with the name MS.Internal.IO.Zip.ZipArchive. Now it is easy to get this special class type and the methods and properties:

var type = typeof(System.IO.Packaging.Package).Assembly.GetType
var static_methodes = type.GetMethods(BindingFlags.Static | 
		BindingFlags.Public | BindingFlags.NonPublic);
var nostatic_methodes = type.GetMethods(BindingFlags.Instance | 
		BindingFlags.Public | BindingFlags.NonPublic);

and we get the most important methods:

static ZipArchive OpenOnFile(string path, FileMode mode, 
	FileAccess access, FileShare share, bool streaming);
static ZipArchive OpenOnStream(Stream stream, FileMode mode, 
	FileAccess access, bool streaming);
ZipFileInfo AddFile(string path, 
	CompressionMethodEnum compmeth, DeflateOptionEnum option);
ZipFileInfo GetFile(string name);
ZipFileInfo DeleteFile(string name);
ZipFileInfoCollection GetFiles();
void Dispose();

The same procedure for ZipFileInfo and we get:

Stream GetStream(FileMode mode, FileAccess access);

and properties like: Name, LastModFileDateTime, FolderFlag...
This is all what we need to implement a small wrapper class and access over Reflection:

class ZipArchive : IDisposable
  private object external;
  public static ZipArchive OpenOnFile
      (string path, FileMode mode, FileAccess access, FileShare share, bool streaming)    
    var type = typeof(System.IO.Packaging.Package).Assembly.GetType
    var meth = type.GetMethod("OpenOnFile", BindingFlags.Static | 
		BindingFlags.Public | BindingFlags.NonPublic);
    return new ZipArchive { external = meth.Invoke(null, new object[] 
		{ path, mode, access, share, streaming }) };
  public class ZipFileInfo //...

The complete ZipArchive wrapper implementation is in the demo project ZipArchiveTest in Program.cs.
Only 97 lines for this class and we can use it in a code sequence like this:

var str = new MemoryStream();

//create some files:
using (var arc = ZipArchive.OpenOnStream(str))
  var doc1 = new XDocument(new XElement
	("root", new XElement("item"), new XElement("item"), new XElement("item")));
  var doc2 = new XDocument(new XElement("root", Enumerable.Repeat
		("item", 1000).Select(p => new XElement(p))));
  using (var fs = arc.AddFile("test1.xml").GetStream
		(FileMode.Open, FileAccess.ReadWrite)) doc1.Save(fs);
  using (var fs = arc.AddFile("test2.xml").GetStream
		(FileMode.Open, FileAccess.ReadWrite)) doc2.Save(fs);

// read the files:
using (var arc = ZipArchive.OpenOnStream(str))
  var doc1 = XDocument.Load(arc.GetFile("test1.xml").GetStream());
  var doc2 = XDocument.Load(arc.GetFile("test2.xml").GetStream());
  var doc3 = XDocument.Load(arc.GetFile("dir/test3.xml").GetStream());

Using the Demo

The demo program ZipArchiveTest using the ZipArchive class is as small as possible and can show the content of Zip archives without any restrictions. Double click to files in the ListBox opens a new window to show the file content as text.

Image 1


Microsoft should publish its hidden ZipArchive class, but till then, we can use such a simple wrapper to save terabytes of data worldwide.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

Comments and Discussions

QuestionPossibility to read file creation date/time ? Pin
Member 95381332-Jan-18 5:18
MemberMember 95381332-Jan-18 5:18 
AnswerRe: Possibility to read file creation date/time ? Pin
D. Christian Ohle2-Jan-18 23:28
MemberD. Christian Ohle2-Jan-18 23:28 
GeneralRe: Possibility to read file creation date/time ? Pin
Member 95381333-Jan-18 2:13
MemberMember 95381333-Jan-18 2:13 
QuestionTrying to add file to existing zip - File contains corrupted data. Pin
annca29-Nov-16 0:57
Memberannca29-Nov-16 0:57 
AnswerRe: Trying to add file to existing zip - File contains corrupted data. Pin
D. Christian Ohle29-Nov-16 5:40
MemberD. Christian Ohle29-Nov-16 5:40 
GeneralRe: Trying to add file to existing zip - File contains corrupted data. Pin
annca4-Dec-16 22:49
Memberannca4-Dec-16 22:49 
Praisethanks Pin
santosh_1319-Jun-16 9:29
Membersantosh_1319-Jun-16 9:29 
QuestionCompressed folder is invalid. Pin
YuryKo27-Nov-15 4:57
MemberYuryKo27-Nov-15 4:57 
AnswerRe: Compressed folder is invalid. Pin
YuryKo29-Nov-15 20:10
MemberYuryKo29-Nov-15 20:10 
GeneralRe: Compressed folder is invalid. Pin
D. Christian Ohle30-Nov-15 21:43
MemberD. Christian Ohle30-Nov-15 21:43 
GeneralRe: Compressed folder is invalid. Pin
YuryKo1-Dec-15 3:01
MemberYuryKo1-Dec-15 3:01 
SuggestionThis is not a safe way Pin
Jalal Khordadi25-Apr-15 2:21
MemberJalal Khordadi25-Apr-15 2:21 
GeneralRe: This is not a safe way Pin
D. Christian Ohle25-Apr-15 4:58
MemberD. Christian Ohle25-Apr-15 4:58 
QuestionCreating zip with several files Pin
Member 86122293-Dec-14 3:29
MemberMember 86122293-Dec-14 3:29 
AnswerRe: Creating zip with several files Pin
D. Christian Ohle3-Dec-14 5:02
MemberD. Christian Ohle3-Dec-14 5:02 
GeneralRe: Creating zip with several files Pin
Member 1161073816-Apr-15 5:49
MemberMember 1161073816-Apr-15 5:49 
GeneralRe: Creating zip with several files Pin
D. Christian Ohle17-Apr-15 5:05
MemberD. Christian Ohle17-Apr-15 5:05 4.5 Pin
kiquenet.com24-Nov-14 21:00
professionalkiquenet.com24-Nov-14 21:00 
AnswerRe: .net 4.5 Pin
D. Christian Ohle24-Nov-14 21:59
MemberD. Christian Ohle24-Nov-14 21:59 
QuestionCan I use this for creating password protected zip file? Pin
Joy George K7-Oct-14 14:01
professionalJoy George K7-Oct-14 14:01 
AnswerRe: Can I use this for creating password protected zip file? Pin
D. Christian Ohle7-Oct-14 20:06
MemberD. Christian Ohle7-Oct-14 20:06 
GeneralRe: Can I use this for creating password protected zip file? Pin
Joy George K24-Oct-14 5:21
professionalJoy George K24-Oct-14 5:21 
GeneralMy vote of 5 Pin
Jingwood29-Sep-14 16:56
MemberJingwood29-Sep-14 16:56 
GeneralRe: My vote of 5 Pin
D. Christian Ohle30-Sep-14 4:28
MemberD. Christian Ohle30-Sep-14 4:28 
GeneralRe: My vote of 5 Pin
Jingwood30-Sep-14 4:53
MemberJingwood30-Sep-14 4:53 

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.