📅  最后修改于: 2023-12-03 15:05:57.249000             🧑  作者: Mango
As a programmer, you might often need to compress and archive files for distribution or storage. For this purpose, Windows provides a built-in command-line tool called zip
that allows you to create, modify, and extract ZIP archives.
In this guide, we'll explore the various features and options of the zip
command and demonstrate how to use it effectively.
To create a new ZIP archive, use the following command:
zip archive.zip file1 file2 folder1
This will create a new archive called archive.zip
and add file1
, file2
, and folder1
to it. You can add as many files and folders as you want, separated by spaces.
If you want to add all files and subfolders within a folder to the archive, use the -r
(recursive) option:
zip -r archive.zip folder2
This will add all files and subfolders within folder2
to the archive.
You can customize various aspects of the archive using command-line options:
-0
, -1
, -6
: Set the compression level. -0
is no compression, -6
is maximum compression (default is -6
).-q
: Quiet mode. Suppress verbose output.-j
: Junk the path. Store only the filename, not the directory structure.-x
: Exclude files that match a pattern. For example, -x "*.txt"
will exclude all files with the ".txt" extension.-z
: Put the archive in the background. Useful for large archives that take a while to create.To add files to an existing ZIP archive, use the following command:
zip archive.zip newfile
This will add newfile
to the existing archive archive.zip
.
If you want to replace a file within the archive, use the -f
(freshen) option:
zip -f archive.zip file1
This will replace file1
within the archive with the newer version on disk.
To extract all files from a ZIP archive, use the following command:
unzip archive.zip
This will extract all files and subfolders within archive.zip
to the current folder.
If you only want to extract specific files, use the command like this:
unzip archive.zip file1 file2
This will extract only file1
and file2
from the archive.
You can customize the extraction process using various options:
-l
: List files instead of extracting them.-n
: Never overwrite files that already exist.-j
: Junk paths. Extract files without creating directories.-d
: Extract files to a specific directory. For example, -d extracted
will extract files to a folder called "extracted".-q
: Quiet mode. Suppress verbose output.The zip
command is a versatile tool for creating, modifying, and extracting ZIP archives in Windows. By knowing how to use its options effectively, you can save time and streamline your workflow. With this guide, you should be well-equipped to start using zip
on your next project.
Markdown code:
# Windows Cmd Zip
## Introduction
As a programmer, you might often need to compress and archive files for distribution or storage. For this purpose, Windows provides a built-in command-line tool called `zip` that allows you to create, modify, and extract ZIP archives.
In this guide, we'll explore the various features and options of the `zip` command and demonstrate how to use it effectively.
## Creating a ZIP Archive
To create a new ZIP archive, use the following command:
zip archive.zip file1 file2 folder1
This will create a new archive called `archive.zip` and add `file1`, `file2`, and `folder1` to it. You can add as many files and folders as you want, separated by spaces.
If you want to add all files and subfolders within a folder to the archive, use the `-r` (recursive) option:
zip -r archive.zip folder2
This will add all files and subfolders within `folder2` to the archive.
### Customizing the Archive
You can customize various aspects of the archive using command-line options:
- `-0`, `-1`, `-6`: Set the compression level. `-0` is no compression, `-6` is maximum compression (default is `-6`).
- `-q`: Quiet mode. Suppress verbose output.
- `-j`: Junk the path. Store only the filename, not the directory structure.
- `-x`: Exclude files that match a pattern. For example, `-x "*.txt"` will exclude all files with the ".txt" extension.
- `-z`: Put the archive in the background. Useful for large archives that take a while to create.
## Modifying a ZIP Archive
To add files to an existing ZIP archive, use the following command:
zip archive.zip newfile
This will add `newfile` to the existing archive `archive.zip`.
If you want to replace a file within the archive, use the `-f` (freshen) option:
zip -f archive.zip file1
This will replace `file1` within the archive with the newer version on disk.
## Extracting Files from a ZIP Archive
To extract all files from a ZIP archive, use the following command:
unzip archive.zip
This will extract all files and subfolders within `archive.zip` to the current folder.
If you only want to extract specific files, use the command like this:
unzip archive.zip file1 file2
This will extract only `file1` and `file2` from the archive.
### Customizing the Extraction
You can customize the extraction process using various options:
- `-l`: List files instead of extracting them.
- `-n`: Never overwrite files that already exist.
- `-j`: Junk paths. Extract files without creating directories.
- `-d`: Extract files to a specific directory. For example, `-d extracted` will extract files to a folder called "extracted".
- `-q`: Quiet mode. Suppress verbose output.
## Conclusion
The `zip` command is a versatile tool for creating, modifying, and extracting ZIP archives in Windows. By knowing how to use its options effectively, you can save time and streamline your workflow. With this guide, you should be well-equipped to start using `zip` on your next project.