119

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.

PS Kumar
  • 2,096
  • 6
  • 30
  • 45
  • 1
    Your question is unclear. Why do you need to use the command line? In what context do you want to use the command - because you tagged this question with Java AND Flex? – splash Aug 13 '13 at 07:31
  • On Windows? Linux? OSX? – Jo Smo Aug 11 '16 at 18:11
  • 1
    Since OP is talking about "BAT file", then I assume it's Windows. – Sreenikethan I Jun 30 '20 at 10:48
  • 2
    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` – Shrikant Verma Jan 28 '21 at 13:54

8 Answers8

215

If you are using Ubuntu Linux:

  1. Install zip

    sudo apt-get install zip
    
  2. Zip your folder:

    zip -r {filename.zip} {foldername}
    

If you are using Microsoft Windows:

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:

  1. Download 7-Zip from the 7-Zip home page

  2. Add the path to 7z.exe to your PATH environment variable. See this QA: How to set the path and environment variables in Windows

  3. Open a new command-prompt window and use this command to create a PKZIP *.zip file:

    7z a -tzip {yourfile.zip} {yourfolder}
    

Cross-platform Java:

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

bernard paulus
  • 1,644
  • 1
  • 21
  • 33
Bigxiang
  • 6,252
  • 3
  • 22
  • 20
  • 1
    I am using windows Xp can u suggest? – PS Kumar Aug 12 '13 at 05:43
  • 2
    when using windows xp: 1. install a zip tool, I suggest 7zip, it's free and support many types, you can download it from [7zip home page](http://www.7-zip.org/), 2. add installition path of 7-zip to your PATH. see detail [How to set the path and environment variables in Windows](http://www.computerhope.com/issues/ch000549.htm#1), 3. execute it: 7z a -tzip yourfile.zip yourfolder @user1990589, if you want more examples, [check this](http://www.dotnetperls.com/7-zip-examples) – Bigxiang Aug 12 '13 at 06:25
  • 1
    @user1990589 I imporved my anwser, and an xp way and a java way, hope it helps. – Bigxiang Aug 12 '13 at 06:42
  • 1
    If you are on Windows, you can also install MinGW, it can allow you you to install `zip` – Ed The ''Pro'' Sep 05 '20 at 15:02
  • Without adding the path, by calling the .exe file directly: C:\Program Files\7-Zip\7z.exe a -tzip {yourfile.zip} {yourfolder} – Spherical Cowboy Nov 20 '21 at 16:28
46

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

Shrikant Verma
  • 571
  • 4
  • 4
  • 3
    This syntax doesn't work if you're trying to zip more than one item. The microsoft reference for the cmdlet: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive – Dinsdale Nov 03 '20 at 17:09
  • Still it will work, that all you have to do is, just go to parent folder and try to zip entire folder so automatically all the items inside the folder will automatically zip, Hope it help... just try once. – Shrikant Verma Jan 28 '21 at 13:52
  • 2
    Compress-Archive relies upon the Microsoft .NET Framework API only work when files less than 2 GB... Reference --> https://stackoverflow.com/a/50418594/1371297 :( – bunjeeb Apr 11 '21 at 22:58
  • 1
    great command thx – Joel Nov 29 '21 at 07:33
  • 2
    It might be helpful to mention `Compress-Archive` is only available in power shell V5 and up – Gibado Feb 17 '22 at 19:26
  • For windows this should be the no 1 answer – bobt May 03 '23 at 01:23
  • Does not seem to work if the pathname contains spaces. Try enclosing with both double and single quotes but neither made a difference. – dcp May 30 '23 at 19:13
22

You can use the following command:

zip -r nameoffile.zip directory

Hope this helps.

MitulP91
  • 1,279
  • 2
  • 12
  • 24
  • am using windows XP. The command zip is not recognized as internal or external command. Can you suggest me in windows XP? – PS Kumar Aug 12 '13 at 05:42
  • This will not be platform independent. I think OP wants to do it using java. – Manish Aug 12 '13 at 05:45
10

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.

  • 1
    This worked great for me. Note that Windows actually ships bsdtar. An list of all command line options can be found here: https://www.freebsd.org/cgi/man.cgi?query=bsdtar&sektion=1&format=html The options acvf in the answer mean: -a - autodetect archive type from extension .zip -c - create a new archive -v - verbose -f - next command line parameter is the name of the archive that should be created – Beat Nideröst Dec 24 '22 at 06:23
6

The zip Package should be installed in system.

To Zip a File

zip <filename.zip> <file>

Example:

zip doc.zip doc.txt 

To Unzip a File

unzip <filename.zip>

Example:

unzip mydata.zip
bummi
  • 27,123
  • 14
  • 62
  • 101
ELinuxbook
  • 157
  • 2
  • 2
  • 2
    I assume that works on Linux, but it doesn't work on Windows. Of course, the OP didn't mention which operating system he was using. – Frecklefoot Feb 28 '18 at 21:08
0

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')"
Geograph
  • 2,274
  • 23
  • 23
0

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);

enter image description here

for a modernised copy which still works well in Windows 10 see https://github.com/npocmaka/batch.scripts/blob/master/hybrids/jscript/zipjs.bat

The CMD tool for zip handling on newer Windows 10, thus I presume carried over to Win 11 is TAR, 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.

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!

K J
  • 8,045
  • 3
  • 14
  • 36
-2

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.

enter image description here

  • 1
    You don't say what problems you had with other commands, but several suggest pretty much what you've shown there. The only differences I can see are that you've omitted `-r`, and not put the optional `.zip` extension on your filename. – mwfearnley Mar 09 '23 at 10:16