如何压缩文件使用 cmd 行?

我想使用批处理文件命令(WindowsXP 批处理文件)压缩一个目录。

例如,如果我想解压缩一个文件意味着我可以使用 jar -xf file.zip(java) bat 文件命令。

像这样,我需要一个命令行批量压缩目录。

382392 次浏览

You can use the following command:

zip -r nameoffile.zip directory

Hope this helps.

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

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

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

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.

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

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