129

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.

4
  • 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, 2013 at 7:31
  • On Windows? Linux? OSX?
    – Jo Smo
    Aug 11, 2016 at 18:11
  • 1
    Since OP is talking about "BAT file", then I assume it's Windows. Jun 30, 2020 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 Jan 28, 2021 at 13:54

8 Answers 8

224

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

5
  • 1
    I am using windows Xp can u suggest?
    – PS Kumar
    Aug 12, 2013 at 5: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, 2. add installition path of 7-zip to your PATH. see detail How to set the path and environment variables in Windows, 3. execute it: 7z a -tzip yourfile.zip yourfolder @user1990589, if you want more examples, check this
    – Bigxiang
    Aug 12, 2013 at 6:25
  • 1
    @user1990589 I imporved my anwser, and an xp way and a java way, hope it helps.
    – Bigxiang
    Aug 12, 2013 at 6:42
  • 1
    If you are on Windows, you can also install MinGW, it can allow you you to install zip Sep 5, 2020 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} Nov 20, 2021 at 16:28
51

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

7
  • 3
    This syntax doesn't work if you're trying to zip more than one item. The microsoft reference for the cmdlet: learn.microsoft.com/en-us/powershell/module/…
    – Dinsdale
    Nov 3, 2020 at 17:09
  • 3
    Compress-Archive relies upon the Microsoft .NET Framework API only work when files less than 2 GB... Reference --> stackoverflow.com/a/50418594/1371297 :(
    – bunjeeb
    Apr 11, 2021 at 22:58
  • 1
    great command thx
    – Joel
    Nov 29, 2021 at 7:33
  • 3
    It might be helpful to mention Compress-Archive is only available in power shell V5 and up
    – Gibado
    Feb 17, 2022 at 19:26
  • 1
    For windows this should be the no 1 answer
    – bobt
    May 3, 2023 at 1:23
21

You can use the following command:

zip -r nameoffile.zip directory

Hope this helps.

2
  • 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, 2013 at 5:42
  • This will not be platform independent. I think OP wants to do it using java.
    – Manish
    Aug 12, 2013 at 5:45
12

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.

2
  • 1
    This worked great for me. Note that Windows actually ships bsdtar. An list of all command line options can be found here: freebsd.org/cgi/… 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 Dec 24, 2022 at 6:23
  • Is this really a .ZIP file? Or is it a compressed tar archive? Experimentation could tell the answer eventually...
    – MarkHu
    Nov 16, 2023 at 2:50
5

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
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. Feb 28, 2018 at 21:08
  • This answer is not well-formed. Does "The zip Package should be installed in system" mean you need to install it first? Or that you should expect it to already be installed?
    – MarkHu
    Nov 16, 2023 at 2:52
2

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, and 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. For a practical use you can extract images and thumbnails or text from zips such as MS DocX, PptX, etc. enter image description here

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!

1

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')"
-1

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
  • 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 9, 2023 at 10:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.