This article presents a versatile way to zip up Visual Studio (VS) solutions, projects, and/or any selected items in the Visual Studio Solution Explorer window, directly to a zip archive. The approach used is, in general, different from that provided by other solutions to this problem. This addin uses the Visual Studio automation object (EnvDTE namespace) to enable it to decide what project items are to be included in the resulting Zip file. This approach has the following advantages:
EnvDTE
The downside to this approach is:
Being in the contracting game requires us to program on client sites, and often to work with the same code off-site. One way to backup the code efficiently is to provide Zip file functionality directly in the VS IDE, and the obvious way to do this is to implement the addin which works directly with the VS automation object and the EnvDTE.Solution, EnvDTE.Project, and EnvDTE.ProjectItem objects for the currently-loaded solution.
EnvDTE.Project
EnvDTE.ProjectItem
There are essentially two components to this addin:
To use #ziplib in your application, simply add a project reference to the .NET module ICSharpCode.SharpZipLib.dll in your VS.NET project and start hooking up the #ziplib objects.
The Windows Installer provides a standard Windows MSI module to install the ZipStudio addin for Visual Studio. Simply extract the files from ZipStudio_inst.zip to a folder and run setup.exe. When you run VS, a menu for the addin will appear in the Visual Studio 'Tools' menu. The addin menu will contain an icon and the menu text ‘Add to Zip file..’. Initially, the menu is disabled until a solution is loaded (or created) in VS. Select some items in the VS Solution Explorer and click the menu item. The Zip Studio window shown in Figure 1 below will appear.
Please note that if the addin is repeatedly installed/un-installed, it may be not appear to be installed and the addin menu may not appear even though the installation executed successfully. In this case, quit all running instances of Visual Studio and execute the file RecreateCommands.reg which is included in the ZipStudio project. The file consists of the following text:
REGEDIT4
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\7.1\PreloadAddinState]
"ZipStudio.Connect"=dword:1
If you now run Visual Studio the menu should appear. The registry setting in the file ensures that the addin is preloaded when Visual Studio starts up.
Figure 1 below shows the ZipStudio window as it appears in the Visual Studio IDE.
Figure 1: The ZipStudio Addin window as it appears in Visual Studio
The following user-interface controls can be seen:
This text box provides the file path of the Zip file that will be created (or updated). The default is the same path as the current solution file (obviously with a .zip instead of a .sln extension). This name can be set to whatever valid file path you like. The '..' button to the immediate right of the file path text box can be clicked to browse for the Zip file.
The list view shows all the files that will be included in the Zip file. The list can be sorted by any of the ‘Filename’, ‘Filepath’, or ‘Date Modified’ columns by clicking on the respective column headers.
Setting this option works similar to the WinZip option with the same name – The full paths of the files are saved but does not include the actual drive letters. The full path ‘D:\ZipStudio\ZipStudio.cs’ is stored as ‘\ZipStudio\ZipStudio.cs’.
If this option is not set, the addin will attempt to save the file paths relative to a common root directory for all the files in the list. In the following set of files with file paths:
the addin will attempt to find a root path for the files and because the files are on different drives, the full paths for the files will in fact be saved. If only the second and third file are part of the list, the common root path will be ‘D:\ZipStudio’, so the path for the solution file will be ‘ZipStudio.sln’ and ‘ZipStudio\ZipStudio.cs’ for the C# file.
If this option is set, the Zip filepath, and the Zip file group options (described below) are saved as part of the VS solution file. This may be a problem if the solution is shared by multiple users or is controlled by a source-control system such as Visual Source-Safe. To ensure that this does not occur, keep this option un-selected.
When the addin is started up Visual Studio, it flags the current date/time. During the session, as files are edited, their modified date/time stamps are more recent than the addin startup time. Setting this option allows only these recently modified files to appear in the included file list.
Visual Studio maintains a list of files open in the editors in an internal project with the UniqueName ’<MiscFiles>’. This project does not have a file path and is not saved. By setting this option, all files that are open in VS that are not part of any project or solution files can also be included in the Zip file. Setting this option unchecked will ensure that only files which are required for the VS Solution to load and build successfully are included.
Any files manually removed in the ‘Remove Selected Items’ function below, are added back into the list of files included in the Zip file by clicking this push-button.
The user has the opportunity to remove any files from the included list, by selecting them and then clicking this push-button. Any files so removed can be re-added by clicking the ‘Reset File List’ push-button.
Setting this option will open the containing folder for the Zip file in the File Explorer and set the file selection to the Zip file after the file is created.
Setting this option will open the Zip file with the application associated with the .zip extension.
Clicking this push-button creates the actual Zip file.
The source-code for this addin is provided in the file ZipStudio_src.zip. Extract the files to a folder of your choice, ensuring that the 'Use folder names' option is set (assuming that you are using WinZip). To build the , open ZipStudio.sln in VS. Please note that the addin was originally built in VS.NET 2003 and that the Setup project ZipStudioSetup.vdproj has registry settings which will only work for VS.NET 2003.
The following is provided to assist your understanding when working through the source code.
In the DTE object, the Solution object consists of a Projects object, which is a collection of Project objects. In turn, a Project object consists of a ProjectItems object, which is a collection of ProjectItem objects. A ProjectItem object provides a set of file paths associated with the project item. As far as can be ascertained, a single file path is used for every project item.
Solution
Projects
Project
ProjectItems
ProjectItem
The basic code for starting the file selection for inclusion in the Zip file is as follows:
if(this.DTE.SelectedItems.Count > 0)
{
for(int i = 1; i <= this.DTE.SelectedItems.Count; i++)
{
SelectedItem item = this.DTE.SelectedItems.Item(i);
if(item.Project == null)
{
if(item.ProjectItem == null)
{
Solution solution = this.DTE.Solution;
// Get the solution files...
}
else
{
// Get the project item files...
}
}
else
{
if(item.ProjectItem == null)
{
// Get the project files...
}
}
}
}
A file path for every possible item can be found as follows:
string filePath = null;
if(Item is Solution)
{
filePath = ((Solution)Item).FullName;
}
else
{
if(Item is Project)
{
filePath = this.GetProjectFullName((Project)Item);
}
else
{
if(Item is ProjectItem)
{
filePath = ((ProjectItem)Item).get_FileNames(1);
}
}
}
In the code above, special handling was required to get the correct file path for the Project object: a combination of the Project.FullName and Project.UniqueName had to be used. The reason is that every project type except the Deployment project (.vdproj) correctly reports the file path in the FullName member. The deployment project does not specify the file extension. However, it does so in the UniqueName member, but this value seems to be either a URI or a relative path. The following method fixes that bug by combining the two members to provide the correct value:
Project.FullName
Project.UniqueName
FullName
UniqueName
private string GetProjectFullName(Project Project)
{
string filePath = Project.FullName;
// Find the file extension of the project FullName.
int extIndex = filePath.LastIndexOf('.');
string filePathExt =
(extIndex > 0) ? filePath.Substring(extIndex + 1) : "";
// Find the file extension of the project UniqueName.
extIndex = Project.UniqueName.LastIndexOf('.');
string uniqueExt =
(extIndex > 0) ? Project.UniqueName.Substring(extIndex + 1) : "";
// If different use the UniqueName extension.
if(filePathExt != uniqueExt)
{
// If the FullName does not have an extension,
// add the one from UniqueName.
if(filePathExt == "") filePath += "." + uniqueExt;
// Else replace it.
else filePath = filePath.Replace(filePathExt, uniqueExt);
}
return filePath;
}
The following code snippet shows the method used to open File Explorer, navigate to the folder containing Zip file, and select the Zip file:
..
string cmdLine = "/select,\"" + ZipFilePath + "\"";
Process.Start("explorer.exe", cmdLine);
..
The following code snippet shows the method used to open the Zip file using the application associated with the Zip file extension:
..
Process.Start(ZipFilePath);
..
If you follow the source-code, you will realize that it is relatively simple. The most difficult part was to code for the exceptions (bugs?) that the different projects exhibited. The InfoPath project (.ipproj) does not appear to implement the ProjectItem.SubProject or the Project.UniqueName members, and any attempt to address these member results in an exception being thrown. The way around this is to place a try..catch block around the code that uses those constructs.
ProjectItem.SubProject
A Hashtable object was used to collect the include files and is keyed by the file paths. This allows 'no-brainer' adding of items while ensuring that all items are added once only. If you recall the rule-set that is applied in adding files to the Zip file, it is obvious that implicitly many items can be selected more than once.
Hashtable
The performance of the addin is very good, allowing the addin to actually run the method that collects the files every time the menu has to be updated. In the case where no files are effectively included, the menu item is disabled in the QueryStatus method of the addin.
QueryStatus
This article does not discuss the infra-structure code required to get the addin to behave correctly or appear presentable in the VS environment. The addin implements the following features which are not discussed in this article:
Solution.Globals
The following are regarded as useful modifications to this addin:
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
REGEDIT4<br />
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\7.1\PreloadAddinState]<br />
"ZipStudio.Connect"=dword:1
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.