Tuesday, December 13, 2016

Windows in-built compression command without utilizing any external tools


I want a Windows (7 onward) command line compression and archiving command/script, which doesn't require any external tools or utilities, including PowerShell, as I am not much of Windows scripter. It should be totally independent, like Unix/Linux zip/tar commands.


I have searched a lot for this type of a command or script, but I haven't found any yet. Has anyone found something like this?


Answer



You don't need to be an expert script writer to utilize PowerShell and you do not need to utilize an explicit PowerShell script (or its IDE interface) to use PowerShell commands—you can utilize PowerShell commands and make them part of the batch script logic to execute and use.


I think you are looking for a Windows native way to complete the task, and you just want to ensure it is indeed native to Windows and will work on from Windows 7 and above (newer).




Compressing and Archiving


I've created a batch script below that works from Windows 10 and Windows 7 and uses logic that works on older versions of PowerShell as well as newer versions just in case I used older logic.



Drag and Drop


Just drag the folder you wish to compress to the batch script and
it'll be archived as a zip file and it will create the new zip file in
that same location but with the .zip so if
you drag C:\User\User\Desktop\New Folder into the script, it will create
C:\User\User\Desktop\New Folder.zip


Drag folder to script


enter image description here


Created afterwards


enter image description here



The Script


Note: I've left two variables in the script commented out up top so you can use those and set the source folder and the destination file name explicitly if you wish rather than using the drag and drop method.


Source link to the PowerShell logic idea


@ECHO ON
::SET src_folder=C:\Users\User\Desktop\Test
::SET destfile=C:\Users\User\Desktop\Test.zip
SET src_folder=%~1
SET destfile=%~FN1.zip
:DynamicPSScriptBuild
SET PSScript=%temp%\%~n0.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
ECHO [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")>>"%PSScript%"
ECHO $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal >>"%PSScript%"
ECHO $includebasedir = $true >>"%PSScript%"
ECHO [System.IO.Compression.ZipFile]::CreateFromDirectory("%src_folder%", "%destfile%", $compressionLevel, $includebasedir)>>"%PSScript%"
:PowerShell
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
EXIT



Decompressing and Extracting


I've also created a batch script below that works from Windows 10 and Windows 7 and uses logic that works on older versions of PowerShell as well as newer versions just in case I used older logic.



Drag and Drop


Just drag the zip file want to decompress into the script and it will
extract the contents of the file to the same folder as the zip file
you dragged into the script resides. So if you have
C:\User\User\Desktop\ZipMe.zip and it contains a folder named ZipMe, once you drag and drop it into the script, it will extract the folder ZipMe to C:\User\User\Desktop.


Drag the zip file to the script


enter image description here


Extracted afterwards


enter image description here



The Script


Important: The variable in the Compressing and Archiving script above (the first script up top) shown as $includebasedir = $true tells it to zip the actual folder and the contents within. If this value was set to $false instead or if you extract from a zip that does not contain the folder, the below script will extract all the files and other contents to the same folder the zip file resides—so 100 files and folders will be extracted if that's what the zip file contained with no containing parent folder within the file.


Note: I've left two variables in the script commented out up top so you can use those and set the destination folder and the zip file name explicitly if you wish rather than using the drag and drop method.


@ECHO ON
::SET dest_folder=C:\Users\User\Desktop\New folder
::SET zip_file=C:\Users\User\Desktop\Test\test.zip
SET dest_folder=%~DP1
SET zip_file=%~1
:DynamicPSScriptBuild
SET PSScript=%temp%\%~n0.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
ECHO [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.Filesystem")>>"%PSScript%"
ECHO [io.compression.zipfile]::ExtractToDirectory("%zip_file%", "%dest_folder%") >>"%PSScript%"
:PowerShell
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
GOTO :EOF



Further Resources


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...