Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

C# Use Zip Archives without External Libraries

, 12 Jun 2011
Rate this:
Please Sign up or sign in to vote.
C# use Zip archives without external libraries

Introduction

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.

Background

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
		("MS.Internal.IO.Zip.ZipArchive");
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
		("MS.Internal.IO.Zip.ZipArchive");
    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.

Conclusion

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

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberJing-Lu29-Sep-14 16:56 
GeneralRe: My vote of 5 PinmemberD. Christian Ohle30-Sep-14 4:28 
GeneralRe: My vote of 5 PinmemberJing-Lu30-Sep-14 4:53 
QuestionHow to update an existing zip file? Pinmembersam1954-Sep-14 11:00 
AnswerRe: How to update an existing zip file? PinmemberD. Christian Ohle4-Sep-14 23:08 
GeneralRe: How to update an existing zip file? Pinmembersam1955-Sep-14 2:18 
QuestionCompression worse than normal zip Pinmembertoto23223220-Aug-13 4:08 
AnswerRe: Compression worse than normal zip PinmemberD. Christian Ohle20-Aug-13 5:56 
QuestionHow to set LastModFileDateTime? [modified] PinmemberYuanming11-Aug-13 17:39 
AnswerRe: How to set LastModFileDateTime? PinmemberD. Christian Ohle11-Aug-13 23:38 
GeneralRe: How to set LastModFileDateTime? PinmemberYuanming12-Aug-13 13:50 
GeneralRe: How to set LastModFileDateTime? PinmemberD. Christian Ohle12-Aug-13 23:06 
GeneralRe: How to set LastModFileDateTime? PinmemberYuanming13-Aug-13 14:09 
GeneralRe: How to set LastModFileDateTime? PinmemberD. Christian Ohle13-Aug-13 21:54 
GeneralRe: How to set LastModFileDateTime? PinmemberYuanming14-Aug-13 2:43 
QuestionError when running on hosted website [modified] PinmemberMagnamus10-Jul-13 10:06 
AnswerRe: Error when running on hosted website PinmemberD. Christian Ohle10-Jul-13 20:39 
GeneralMy vote of 5 PinmemberMagnamus30-Jun-13 5:00 
QuestionFolders PinmemberMagnamus29-Jun-13 6:12 
AnswerRe: Folders PinmemberD. Christian Ohle29-Jun-13 23:43 
GeneralRe: Folders Pinmembertyukodi30-Jun-13 22:43 
GeneralRe: Folders PinmemberD. Christian Ohle1-Jul-13 5:57 
QuestionLooks like someone has stolen your article... PinmemberFlorian Storck22-May-13 2:59 
AnswerRe: Looks like someone has stolen your article... PinmemberD. Christian Ohle22-May-13 6:01 
QuestionGreat! But... PinmemberCainKellye22-Apr-13 1:36 
AnswerRe: Great! But... PinmemberD. Christian Ohle22-Apr-13 2:00 
GeneralMy vote of 5 PinmemberRalf_13-Mar-13 9:07 
GeneralRe: My vote of 5 PinmemberD. Christian Ohle13-Mar-13 20:52 
GeneralRe: My vote of 5 PinmemberRalf_14-Mar-13 0:30 
GeneralRe: My vote of 5 PinmemberD. Christian Ohle14-Mar-13 6:13 
GeneralRe: My vote of 5 PinmemberRalf_14-Mar-13 23:58 
GeneralMy vote of 5 PinmemberDanielSheets11-Mar-13 4:58 
GeneralMy vote of 5 PinmemberJaap Lamfers12-Feb-13 8:32 
Suggestion.NET 3.5 version PinmemberStehtimSchilf11-Feb-13 8:49 
GeneralThanks! Pinmemberrbignu5-Jan-13 1:44 
QuestionAwesome! PinmemberWildbird3-Jan-13 10:04 
GeneralMy vote of 5 PinmemberAwchie13-Nov-12 15:28 
GeneralThanks! High 5 Dude PinmemberAwchie13-Nov-12 15:21 
GeneralMy vote of 5 Pinmembergelo_one12-Nov-12 23:07 
GeneralThanks PinmemberTheGrandBazaar12-Nov-12 4:21 
GeneralMy vote of 5 PinmemberElron022-Nov-12 13:28 
GeneralMy vote of 5 PinmemberCollin Heine21-Sep-12 7:57 
I've been looking for some kind of guidance like this forever! Thanks!
GeneralMy vote of 5 PinmemberMark Lemke12-Sep-12 0:57 
QuestionNo file compression ?? PinmemberBillJam119-Aug-12 8:25 
AnswerRe: No file compression ?? PinmemberBillJam119-Aug-12 8:49 
QuestionProblem with non ascii characters in file names [modified] PinmemberNyarlatotep18-Jun-12 7:45 
AnswerRe: Problem with non ascii characters in file names PinmemberD. Christian Ohle19-Jun-12 6:54 
GeneralRe: Problem with non ascii characters in file names PinmemberMark Lemke11-Sep-12 8:34 
GeneralRe: Problem with non ascii characters in file names PinmemberD. Christian Ohle11-Sep-12 23:56 
Questionfs.GetStream gives a zero byte length [modified] PinmemberJames Garfield10-Feb-12 4:17 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 12 Jun 2011
Article Copyright 2011 by D. Christian Ohle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid