Project: Unzip.File.Shell32



Unzip a ZIP file using Shell32 COM wrapper and dynamic (thanks to Marco Minerva for help):
void Main()
{
	var zipPath = ReadLine("Insert the zip file path:");
	
	new ZipUtility().UnzipFile(zipPath);
}

public static string ReadLine(string message)
{
	Console.WriteLine(message);
	return Console.ReadLine();
}

public class ZipUtility
{
	public void UnzipFile(string zipPath)
	{
		var fileInfo = new FileInfo(zipPath);
		var extractPath = 
			Path
				.Combine(
					fileInfo.DirectoryName, 
					Path.GetFileNameWithoutExtension(zipPath));
		
		if (Directory.Exists(extractPath))
		{
			Directory.Delete(extractPath, true);
		}
		
		Directory.CreateDirectory(extractPath);
		
		UnzipFile(zipPath, extractPath);
	}
	
	public void UnzipFile(string zipPath, string extractPath)
	{
		var shell = (dynamic)GetShell();
		dynamic scrFolder = shell.Namespace(zipPath);       
		dynamic destFolder = shell.NameSpace(extractPath);
		dynamic items = scrFolder.Items();
		destFolder.CopyHere(items, 20);
	}
	
	public void ZipFile(string zipPath, string folderPathToCompress)
	{
		var shell = (dynamic)GetShell();
			
		dynamic scrFolder = shell.Namespace(folderPathToCompress);       
		dynamic destFolder = shell.NameSpace(zipPath);
		dynamic items = scrFolder.Items();
		destFolder.CopyHere(items, 4);
	}
	
	private object GetShell()
	{
		var shellType = Type.GetTypeFromProgID("Shell.Application");
		
		return Activator.CreateInstance(shellType);	
	}
}

Last edited Jun 2, 2012 at 7:17 PM by teoarch, version 2

Comments

No comments yet.