Explorerの機能を使用してZipの解凍
以前圧縮について書いたけど、Explorerのzip機能を使うのがwindowsでは一番なのかもしれない。
ただし、Explorerの機能なんで上書きとかで問い合わせは発生する。
解凍
Shell.Applicationを利用する。
static class ObjectExtentions { public static object InvokeMethod( this object target, string methodName, params object[] args) { return target.GetType().InvokeMember( methodName, BindingFlags.InvokeMethod, null, target, args ); } } static public void Decompression( string zipFilePath, string dest ) { var shellApp = Activator.CreateInstance( Type.GetTypeFromProgID( "Shell.Application" ) ); var zipFile = shellApp.InvokeMethod( "NameSpace", zipFilePath ); var targetfolder = shellApp.InvokeMethod( "NameSpace", dest ); var zipItems = zipFile.InvokeMethod( "Items" ); targetfolder.InvokeMethod( "CopyHere", zipItems ); } static void Main( string[] args ) { Decompression( "E:\\myzip.zip", "E:\\" ); }
Shell.Applicationを利用する。
.net 4.0以降ならこう書ける。こういう時はdynamicいいよね。
static void Main( string[] args ) { Decompression( "E:\\myzip.zip", "E:\\" ); } static void Decompression( string zipFilePath, string dest ) { dynamic shellApp = Activator.CreateInstance( Type.GetTypeFromProgID( "Shell.Application" ) ); dynamic zipFile = shellApp.NameSpace( zipFilePath ); dynamic targetfolder = shellApp.NameSpace( dest ); targetfolder.CopyHere( zipFile.Items() ); }
shell32.dllを利用する。
参照設定で、COM>Microsoft Shell Controls And Automation を選択
static void Main( string[] args ) { Decompression( "E:\\myzip.zip", "E:\\" ); } static void Decompression( string zipFilePath, string dest ) { var shell = new Shell32.Shell(); var zipFile = shell.NameSpace( zipFilePath ); var targetfolder = shell.NameSpace( dest ); targetfolder.CopyHere( zipFile.Items() ); }