DotNetZip Library - Home
DotNetZip は Microsoft Public License の ZIP ライブラリ プロジェクト。
試しに C# からコマンドラインでパスワード付きZIPを作成するサンプルプログラムを書いてみた。
using System; using System.IO; using Ionic.Utils.Zip; namespace AppTest { class Program { static void Main(string[] args) { string filePath = null; string zipfile = null; string password = null; for (int i = 0; i < args.Length; i++) { if (args[i] == "-p") { password = args[++i]; } else if (filePath == null) { filePath = args[i]; } else { zipfile = args[i]; } } if (filePath == null || zipfile == null || !File.Exists(filePath)) { if (args.Length != 2) { Console.WriteLine("Usage WhiteMole.exe [-p password] file zipfile"); } return; } try { zipAtEncrypt(filePath, zipfile, password); } catch (ZipException) { Console.WriteLine("Failed to compress " + filePath); } } private static void zipAtEncrypt(string filePath, string zipfile, string password) { using (ZipFile zip = new ZipFile(zipfile)) { if (password != null) zip.Password = password; zip.AddFile(filePath); zip.Save(); } } } }
試したところ何故か圧縮率が Windows XP の標準機能で圧縮したときなどに比べて悪い。
119MB のVMDKファイルを圧縮すると標準機能だと 51MB になるのに、このライブラリだと 79MB にしかならない。もちろん解凍はできるしファイルチェックサムも一致する。
ちなみ圧縮解凍ソフトの Lhaz では XP 標準機能と同様のファイルサイズになる。ううむ、アルゴリズムをすべて実装しているわけではないのだろうか。
ZIP処理の自動化をしたかったのだが、この圧縮サイズ差は大きいので別の方法を考えたい。ただ、アーカイブ生成として使う分には良いかもしれない。機会があったら今度は解凍処理を試してみよう。