How to zip a file using cmd line?

287,506

Solution 1

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

Solution 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

Solution 3

You can use the following command:

zip -r nameoffile.zip directory

Hope this helps.

Solution 4

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

Solution 5

ZIP FILE via Cross-platform Java without manifest and META-INF folder:

jar -cMf {yourfile.zip} {yourfolder}
Share:
287,506
PS Kumar
Author by

PS Kumar

http://beginnersbook.com/2013/05/jsp-tutorial-directives/ http://www.java2novice.com/java-collections-and-util/iterator/iterate/ Java - How to share the session between two or more Web Application? http://www.softteco.com/blog/table-of-contents-in-jasperreports/

Updated on July 26, 2021

Comments

  • PS Kumar
    PS Kumar almost 3 years

    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
    PS Kumar over 10 years
    am using windows XP. The command zip is not recognized as internal or external command. Can you suggest me in windows XP?
  • PS Kumar
    PS Kumar over 10 years
    I am using windows Xp can u suggest?
  • Manish
    Manish over 10 years
    This will not be platform independent. I think OP wants to do it using java.
  • Bigxiang
    Bigxiang over 10 years
    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
    Bigxiang over 10 years
    @user1990589 I imporved my anwser, and an xp way and a java way, hope it helps.
  • Frecklefoot
    Frecklefoot about 6 years
    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.
  • Edwin Pratt
    Edwin Pratt over 3 years
    If you are on Windows, you can also install MinGW, it can allow you you to install zip
  • Dinsdale
    Dinsdale over 3 years
    This syntax doesn't work if you're trying to zip more than one item. The microsoft reference for the cmdlet: docs.microsoft.com/en-us/powershell/module/…
  • Shrikant Verma
    Shrikant Verma about 3 years
    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.
  • bunjeeb
    bunjeeb about 3 years
    Compress-Archive relies upon the Microsoft .NET Framework API only work when files less than 2 GB... Reference --> stackoverflow.com/a/50418594/1371297 :(
  • Spherical Cowboy
    Spherical Cowboy over 2 years
    Without adding the path, by calling the .exe file directly: C:\Program Files\7-Zip\7z.exe a -tzip {yourfile.zip} {yourfolder}
  • Joel
    Joel over 2 years
    great command thx
  • Gibado
    Gibado about 2 years
    It might be helpful to mention Compress-Archive is only available in power shell V5 and up