Last active
April 5, 2023 07:23
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 runningZipExtractor.reg
.