我有几个包含多个文件类型的zip文件。我只对.txt文件感兴趣。我只需要提取.txt文件并将其放置在自己的文件夹中,而忽略zip文件中的所有其他文件类型。
所有zip文件都在同一文件夹中。
例子
-foo.zip
--1.aaa
--2.bbb
--3.ccc
--4.txt
-foo2.zip
--5.aaa
--6.bbb
--7.ccc
--8.txt
我想将
4.txt
和8.txt
提取到另一个文件夹。我一辈子都想不起来,花了很长时间寻找,谷歌搜索和尝试。甚至设法偶尔删除zip :-)提前致谢
最佳答案
在提取之前,使用 ZipArchive
类型以编程方式检查文件:
Add-Type -AssemblyName System.IO.Compression
$destination = "C:\destination\folder"
# Locate zip file
$zipFile = Get-Item C:\path\to\file.zip
# Open a read-only file stream
$zipFileStream = $zipFile.OpenRead()
# Instantiate ZipArchive
$zipArchive = [System.IO.Compression.ZipArchive]::new($zipFileStream)
# Iterate over all entries and pick the ones you like
foreach($entry in $zipArchive.Entries){
if($entry.Name -like '*.txt'){
# Create new file on disk, open writable stream
$targetFileStream = $(
New-Item -Path $destination -Name $entry.Name -ItemType File
).OpenWrite()
# Open stream to compressed file, copy to new file stream
$entryStream = $entry.Open()
$entryStream.BaseStream.CopyTo($targetFileStream)
# Clean up
$targetFileStream,$entryStream |ForEach-Object Dispose
}
}
# Clean up
$zipArchive,$zipFileStream |ForEach-Object Dispose
对每个zip文件重复上述步骤。
请注意,上面的代码具有很少的错误处理,将作为示例阅读。
关于windows - 从多个ZIP提取特定文件类型到Powershell中的一个文件夹,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60871547/