Skip to content

Instantly share code, notes, and snippets.

@CennoxX
Last active April 5, 2023 07:23
Show Gist options
  • Save CennoxX/102dd003a81585623f67dc5dc7319d3c to your computer and use it in GitHub Desktop.
Save CennoxX/102dd003a81585623f67dc5dc7319d3c to your computer and use it in GitHub Desktop.
Extract a zip file to a folder if there are multiple files inside, extract directly if there is only one file. Afterwards the zip file will be deleted. As it should be.
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
partial class ZipExtractor
{
static void Main(string[] args)
{
// get the name of the zip file to extract
string filename = args[0];
string fullPathWithoutExtension = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename));
// run the 7z l -slt command to list the contents of the zip file
Process process = new();
process.StartInfo.FileName = "7z.exe";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = $"l -slt \"{filename}\" -o\"{fullPathWithoutExtension}\"";
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// check if the zip file contains multiple files/folders
string? otherFiles = output.Split(new[] { "----------" }, StringSplitOptions.None)[1];
Regex? regex = PathPattern();
MatchCollection? matches = regex.Matches(otherFiles);
int numberOfFiles = matches.Select(i => i.Groups["filename"].Value).Distinct().Count();
// run the 7z x command to extract the zip file into the new folder
process.StartInfo.Arguments = $"x \"{filename}\"" + (numberOfFiles != 1 ? $" -o\"{fullPathWithoutExtension}\"" : "");
process.Start();
output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// delete the zip file
File.Delete(filename);
}
[GeneratedRegex("Path = (?<filename>[^\\\\\\r\\n]+)")]
private static partial Regex PathPattern();
}
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\CompressedFolder\Shell\Extract]
[HKEY_CLASSES_ROOT\CompressedFolder\Shell\Extract\command]
@="\"C:\\Windows\\system32\\wscript.exe\" \"C:\\Users\\user\\Documents\\ZipExtractor\\ZipExtractor.vbs\" \"%1\""
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFilePath = WScript.Arguments.Item(0)
objShell.CurrentDirectory = objFSO.GetParentFolderName(strFilePath)
objShell.Run "C:\Users\user\Documents\ZipExtractor\ZipExtractor.exe " & Chr(34) & WScript.Arguments.Item(0) & Chr(34), 0, False
Set objShell = Nothing
Set objFSO = Nothing
@CennoxX
Copy link
Author

CennoxX commented Apr 2, 2023

I have always been annoyed that when unzipping, you either find a folder in the folder, or you find multiple files in the folder mixed with other files where the zip file should be unzipped. ZipExtractor puts an end to that. 7zip has to be installed and C:\Program Files\7-Zip added to PATH.

Call from the VB script ZipExtractor.vbs to hide the command line window, add to context menu by running ZipExtractor.reg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment