Recently, in my .NET / C # project, the functionality for creating and unpacking zip archives was claimed for me. It would seem very simple operations, given that Windows Explorer has long been able to work with zip files and I suggested that Microsoft provides an API for working with them.
The absence of classes for working with zip in the .NET framework did not become a big surprise for me, but it wasn’t good news either. The lack of functionality for working with zip in various unmanaged Windows APIs (WinAPI, COM interfaces, etc.) annoyed me. At the moment, the entire source code of the project was developed by me and I did not want to include an additional third-party .dll file in the distribution package due to the simplest functionality.
After searching the Internet and in msdn, I chose from the following solutions:
- In the .NET Framework there is a class System.IO.Packaging.ZipPackage, but as I understand it, you cannot fully work with zip archives using it and besides it appeared only in .NET 3.5, I didn't really want to attach my project to it . This class uses the MS.Internal.IO.Zip.ZipArchive class, but, as the name implies, access to it is denied. I wonder why.
- In the .NET Framework for J #, there is a java.util.zip.ZipFile class and there is an article on MSDN how to use it from C #. But Visual J # Framework is not included in the .NET Framework, respectively, you need to include the Visual J # Redistributable Package in your distribution, which is 3.6 megabytes in general, which is useless code for me. Not too elegant.
- minizip is a zlib add-on, written in C. For use in .NET you need a dll assembly + interop.
- Third-party libraries SharpZipLib and DotNetZip . Both are completely written and provide a fairly wide functionality.
There is another
option for working with zip files with the help of the Shell API, but personally it seems to me too perverted, and for example, it will not work to unpack the zip archive in memory.
')
None of the above options did not suit me completely, and I decided to develop my own. In the .NET Framework, there is a System.IO.Compression.DeflateStream class that can package and unpack data based on the deflate algorithm. You only need to be able to read / write the internal structure of the zip archive, which is described in the
specification .
As a result, in a couple of days I developed a small and simple library that can package and create zip files, both in the file system and in memory:
- It only supports the deflate compression algorithm and, of course, the store.
- Able to work with archives containing files in utf8.
- Normally unpacks zip archives created by WinRAR, and, probably, by many other programs under Windows. Archives unsupported by my zip library I also met
Of course, there is no full support for the entire ZIP format (encryption, various compression algorithms, extra fields, etc.), this is an enormous amount of work and all of the above solutions do not provide it either. My solution is acceptable only for situations when it is known in advance that the archive format is supported by the library and the archives will not be directly received from the application user. In my project, these requirements are met, so the solution I developed was completely fine.
I did not set the task to develop open source library, with documentation and convenient API. I just wanted to tell and give an example of working with ZIP from .NET without using third-party libraries.
Download the source code and an example
here .
I would be glad if someone come in handy :)