I wanted to use .net’s System.IO.Compression.GZipStream to compress and decompress a directory. But I find it only able to compress a single file, how can I write a zip class that can compress a specific directory to a zip file?
ps: we’re not allowed to use any third party zip library
There is plenty of documentation on the .zip file format. Wikipedia is a good place to start:
http://en.wikipedia.org/wiki/ZIP_file_format.
You can use the
System.IO.Compression.DeflateStreamfor the actual compression, but you’ll need a CRC32 implementation, and you’ll need to layout the file with the correct headers, etc.It is a non-trivial amount of work.
You might consider purchasing a commercial .zip library if you are unable to use an open-source library. You should not have any legal issues with using a commercial library**, and the cost to purchase a license is almost certainly cheaper than the cost of developing and testing the library yourself. Also, check around. Your company might have a license for a commercial library already.
**I’m not a lawyer, so check with your legal contact.