从本质上讲,ZIP文件格式通过将文件压缩成单个文件来减小文件的大小。此过程可节省磁盘空间、加密数据并轻松与他人共享文件。在这篇文章中,我们将向您展示如何在 Windows 11/10 中使用PowerShell实用程序( PowerShell)压缩和解压缩文件。( zip and unzip files )( zip and unzip files )
如何使用 PowerShell 压缩文件
首先使用Compress-Archive cmdlet将一些文件压缩到ZIP文件存档中。它获取您要压缩的任何文件的路径(多个文件用逗号分隔)并将它们存档在您指定的目标位置。
请执行下列操作:
按Windows key + X打开高级用户菜单(open Power User Menu),然后按键盘上的I启动 PowerShell。
接下来,输入下面的语法,将<PathToFiles>和<PathToDestination>占位符分别替换为您要压缩的文件的路径以及您希望它去的名称和文件夹。
Compress-Archive -LiteralPath <PathToFiles> -DestinationPath <PathToDestination>
注意(Note):当您提供目标路径时,请务必为存档文件命名,否则PowerShell会将其保存为您指定的“.zip”。此外,请记住,仅当文件路径包含空格时,路径周围的引号才需要。
或者,要压缩文件夹及其所有子文件夹的全部内容,您可以使用与上述相同的语法,将<PathToFiles>和<PathToDestination>占位符替换为要压缩的文件的路径以及名称和文件夹希望它去,分别。
它应该如下图所示。
此命令将路径放入其中包含多个文件和文件夹的目录,而不指定单个文件。PowerShell获取根目录中的所有内容并对其、子文件夹和所有内容进行压缩。
阅读(Read):如何打开 .TAR.GZ、.TGZ 或 .GZ。文件(How to open .TAR.GZ, .TGZ or .GZ. Files).
通配符 (*) 函数
Compress-Archive cmdlet 允许您使用通配符 (*) 进一步扩展功能。使用该字符时,可以排除根目录,仅压缩目录中的文件,或选择特定类型的所有文件。要将通配符与Compress-Archive一起使用,您必须改用-Path 参数,因为-LiteralPath不接受它们。
现在,从上面给出的两个示例中,您已经了解了在创建存档文件时如何包含根目录及其所有文件和子目录。但是,如果要从Zip文件中排除根文件夹,可以使用通配符将其从存档中省略。通过在文件路径的末尾添加星号 (*),PowerShell将只获取根目录内的内容。正确的语法如下所示。
Compress-Archive -Path C:\path\to\file\* -DestinationPath C:\path\to\archive.zip
现在,如果您有一个包含一堆不同文件类型(.docx、.txt、.jpg等)但只想压缩所有一种类型的文件夹,您可以使用下面的语法。PowerShell将归档指定的文件,而不会明确触及其他文件。请记住(Bear),使用此方法的存档中不包含子目录和根文件夹的文件。
Compress-Archive -Path C:\path\to\file\*.docx -DestinationPath C:\path\to\archive.zip
最后,如果您想要一个仅压缩根目录及其所有子目录中的文件的存档,您将使用星点星号 (*.*) 通配符使用以下语法压缩文件。使用此方法,子目录和根文件夹的文件也不包含在存档中。
Compress-Archive -Path C:\path\to\file\*.* -DestinationPath C:\path\to\archive.zip
现在,必须指出,即使在存档完成后,您也可以使用-Update参数和下面提供的正确语法来更新现有的压缩文件。这使您可以将存档中的旧文件版本替换为具有相同名称的新文件,并添加已在根目录中创建的文件。
Compress-Archive -Path C:\path\to\files -Update -DestinationPath C:\path\to\archive.zip
您可以在Windows 10中使用(Windows 10)PowerShell压缩文件的各种场景的过程到此结束。继续下面查看如何使用PowerShell解压缩文件。
阅读(Read):如何在 Windows 11/10 上安装 CURL。
如何使用 PowerShell 解压缩文件
如您所见,PowerShell可用于压缩文件。该实用程序还可以解压缩档案。这个过程甚至比压缩它们更容易——您所需要的只是源文件和准备解压缩数据的目的地。
让我们开始吧。
要使用PowerShell(PowerShell)解压缩文件,请执行以下操作:
打开 PowerShell。
接下来,输入下面的语法,将<PathToZipFile>和<PathToDestination>占位符分别替换为您要压缩的文件的路径以及您希望它去的名称和文件夹。
Expand-Archive -LiteralPath <PathToZipFile> -DestinationPath <PathToDestination>
指定将文件提取到的目标文件夹将填充存档的内容。如果解压缩前该文件夹不存在,PowerShell将在解压缩前创建该文件夹并将内容放入其中。
默认情况下,如果省略-DestinationPath 参数,PowerShell会将内容解压缩到当前根目录并使用Zip文件的名称创建一个新文件夹。
在此示例中,在命令中指定了文件夹Docs,因此 PowerShell将在路径C:\Users\Chidum.OsobaluDocs并将文件从存档中提取到该文件夹中。请参阅包含在下面这篇文章开头存档的两个文件的输出文件夹。
请注意,如果目标文件夹中已存在Docs文件夹, (Docs)PowerShell在尝试解压缩文件时将返回错误。但是,您可以使用-Force(-Force) 参数强制PowerShell用新数据覆盖数据。
如果不再需要旧文件,您应该只使用-Force参数,因为这将不可逆地替换您计算机上的文件。(-Force)
And this wraps up our subject on how to zip and unzip files using the PowerShell utility in Windows 11/10!
How to Zip and Unzip files using PowerShell in Windows 11/10
Essentially, the ZIP file format reduceѕ the size of files by compresѕing them intо a single file. This process saves disk space, encrypts data, and makes it easy to share files with others. In this post, we will show you how to zip and unzip files using the PowerShell utility in Windows 11/10.
How to zip files using PowerShell
You begin by compressing some files into a ZIP file archive using the Compress-Archive cmdlet. It takes the path to any files you want to compress—multiple files are separated with a comma—and archives them in the destination you specify.
Do the following:
Press Windows key + X to open Power User Menu and then press I on the keyboard to launch PowerShell.
Next, type in the syntax below, replacing <PathToFiles> and <PathToDestination> placeholder with the path to the files you want to compress and the name and folder you want it to go, respectively.
Compress-Archive -LiteralPath <PathToFiles> -DestinationPath <PathToDestination>
Note: When you provide the destination path, be sure to give the archive file a name or PowerShell will save it as “.zip” where you specify. Also, bear in mind that quotations around the path are only necessary when the file path contains a space.
Alternatively, to zip the entire contents of a folder and all of its subfolders, you can use the same syntax as above, replacing <PathToFiles> and <PathToDestination> placeholder with the path to the files you want to compress and the name and folder you want it to go, respectively.
It should look like as shown in the image below.
This command puts the path to a directory with multiple files and folders in it without specifying individual files. PowerShell takes everything inside of the root directory and compresses it, subfolders, and all.
Read: How to open .TAR.GZ, .TGZ or .GZ. Files.
The wildcard character (*) function
The Compress-Archive cmdlet lets you use a wildcard character (*) to expand the functionality even further. When you use the character, you can exclude the root directory, compress only files in a directory, or choose all files of a specific type. To use a wildcard with Compress-Archive, you must use the -Path parameter instead, as -LiteralPath does not accept them.
Now, from both examples given above, you have seen how to include the root directory and all of its files and subdirectories when creating an archive file. However, if you want to exclude the root folder from the Zip file, you can use a wildcard to omit it from the archive. By adding an asterisk (*) to the end of the file path, PowerShell will only grab what’s inside of the root directory. The correct syntax is presented below.
Compress-Archive -Path C:\path\to\file\* -DestinationPath C:\path\to\archive.zip
Now, in the case whereby you have a folder with a bunch of different file types (.docx, .txt, .jpg, etc.) but only want to compress all of one type, you can use the syntax below. PowerShell will archive the files specified without touching the others explicitly. Bear in mind that subdirectories and the files of the root folder aren’t included in the archive with this method.
Compress-Archive -Path C:\path\to\file\*.docx -DestinationPath C:\path\to\archive.zip
Lastly, if you want an archive that only compresses files in the root directory and all its subdirectories, you will use the star-dot-star (*.*) wildcard to zip the files with the syntax below. With this method too, subdirectories and the files of the root folder aren’t included in the archive.
Compress-Archive -Path C:\path\to\file\*.* -DestinationPath C:\path\to\archive.zip
Now, it’s imperative to point out that even after the archive is complete, you can update an existing zipped file with the use of the -Update parameter with the correct syntax provided below. This lets you replace older file versions in the archive with newer ones that have the same names, and add files that have been created in the root directory.
Compress-Archive -Path C:\path\to\files -Update -DestinationPath C:\path\to\archive.zip
And this concludes the process of the various scenarios that you can zip files using PowerShell in Windows 10. Continue below to see how you can unzip files using PowerShell.
Read: How to install CURL on Windows 11/10.
How to Unzip files using PowerShell
As you have already seen, PowerShell can be used to zip files. The utility also can unzip archives. The process is even easier than compressing them – all you need is the source file and a destination for the data ready to unzip.
Let’s get to it.
To unzip files using PowerShell, do the following:
Open PowerShell.
Next, type in the syntax below, replacing <PathToZipFile> and <PathToDestination> placeholder with the path to the files you want to compress and the name and folder you want it to go, respectively.
Expand-Archive -LiteralPath <PathToZipFile> -DestinationPath <PathToDestination>
The destination folder specified to extract the files into will populate with the contents of the archive. If the folder didn’t exist before unzipping, PowerShell will create the folder and place the contents into it before unzipping.
By default, if you leave out the -DestinationPath parameter, PowerShell will unzip the contents into the current root directory and use the name of the Zip file to create a new folder.
In this example, the folder Docs is specified in the command, so PowerShell will create the folder Docs in the path C:\Users\Chidum.Osobalu and extract the files from the archive into the folder. See the output folder containing the two files archived at the beginning of this post below.
Note that, if the folder Docs already exists in the destination, PowerShell will return an error when it tries to unzip the files. However, you can force PowerShell to overwrite the data with the new ones using the -Force parameter.
You should only use the -Force parameter if the old files are no longer needed, as this will irreversibly replace the files on your computer.
And this wraps up our subject on how to zip and unzip files using the PowerShell utility in Windows 11/10!