Reputation: 2446
I want to zip a directory using the batch file command (Windows XP batch file).
For example, if I want to unzip a file means I can use the jar -xf file.zip
(java) bat file command.
Like that I want a command line batch to zip a directory.
Upvotes: 149
Views: 639759
Reputation: 11939
At the time of the Original Question the correct answer should have been use Windows JScript or VBS via command line which at the time such methods could build upto 2 GB zip using ADODB.Stream.writeFile(destination,ZIP_DATA);
for a modernised copy which still works well in Windows 10 see https://github.com/npocmaka/batch.scripts/blob/master/hybrids/jscript/zipjs.bat
however it was not "native" command line in Windows 8 or earlier, since that system inbuilt dll feature was there as part of explorer, since I think, at least XP"
see Tar --help
To list use Tar -tf file.zip
To extract use Tar -m -xf file.zip
So the OP wanted save to zip
To compress use Tar -a -cf new.zip files
Other options are available, but I think to work with "nested" archive files requires peeling the levels off.
For a practical use you can extract images and thumbnails or text from zips such as MS DocX, PptX, etc.
Overall it is better to use a universal zip tool such as the prime suggestion 7-Zip utility, but others are popular on windows especially the Grandfather of all Windows variants WinZip (c) 1991 https://www.winzip.com/en/download/winzip/ which has specialist versions such as secured WinZip Courier and WinZip PDF!
Upvotes: 5
Reputation: 2604
Zip the folder from cmd by running PowerShell:
powershell "Add-Type -A System.IO.Compression.FileSystem; [IO.Compression.ZipFile]::CreateFromDirectory('folder','archive.zip')"
Unzip:
powershell "Add-Type -A System.IO.Compression.FileSystem; [IO.Compression.ZipFile]::ExtractToDirectory('archive.zip','folder')"
Upvotes: 2
Reputation: 169
Nothing listed here worked with me. This should be a very simple thing. I post my answer here, if anything because each time I search for "how to zip on the cmd window" I end up on this page, with no solution that works for me.
So here is the one that works with me: zip output_file input_files
, as in the screenshot below.
Upvotes: -1
Reputation: 599
Windows 10 has tar command since 2018. It supports zip archive in default. You do not need to install any additional packages nor software.
tar.exe acvf yourfile.zip yourfolder
Compress-Archive in PowerShell does not support 2GB+ files.
Upvotes: 14
Reputation: 839
Yes, we can zip and unzip the file/folder using cmd. See the below command and simply you can copy past in cmd and change the directory and file name
To Zip/Compress File
powershell Compress-Archive D:\Build\FolderName D:\Build\FolderName.zip
To Unzip/Expand File
powershell expand-archive D:\Build\FileName.zip D:\deployments\FileName
Upvotes: 64
Reputation: 6712
Install zip
sudo apt-get install zip
Zip your folder:
zip -r {filename.zip} {foldername}
Windows does not come with a command-line zip program, despite Windows Explorer natively supporting Zip files since the Plus! pack for Windows 98.
I recommend the open-source 7-Zip utility which includes a command-line executable and supports many different archive file types, especially its own *.7z
format which offers superior compression ratios to traditional (PKZIP) *.zip
files:
Download 7-Zip from the 7-Zip home page
Add the path to 7z.exe
to your PATH
environment variable. See this QA:
How to set the path and environment variables in Windows
Open a new command-prompt window and use this command to create a PKZIP *.zip
file:
7z a -tzip {yourfile.zip} {yourfolder}
If you have the Java JDK installed then you can use the jar
utility to create Zip files, as *.jar
files are essentially just renamed *.zip
(PKZIP) files:
jar -cfM {yourfile.zip} {yourfolder}
Explanation: * -c compress * -f specify filename * -M do not include a MANIFEST file
Upvotes: 255
Reputation: 165
The zip Package should be installed in system.
zip <filename.zip> <file>
Example:
zip doc.zip doc.txt
unzip <filename.zip>
Example:
unzip mydata.zip
Upvotes: 6
Reputation: 1335
You can use the following command:
zip -r nameoffile.zip directory
Hope this helps.
Upvotes: 23