如何将单个txt文件移动到powershell 3中的zip [英] How to move a single txt file to the zip in powershell 3
问题描述
我正在尝试将一个文本文件复制/移动到 zip.我不想解压缩它,复制文件并将其压缩回来.有什么办法可以直接将文本文件复制或移动到powershell中的zip文件中.当我在 powershell 中执行此操作时,它会在此之后执行,当我尝试查看 zip 内部时,它说路径无效.
Powershell 命令:
$A = "20160914.4"新建项目 "C:\test\VersionLabel.txt" -ItemType 文件$A |设置内容C:\test\VersionLabel.txt"复制项目 "C:\test\VersionLabel.txt" "C:\test\Abc.zip" -Force
<块引用>
错误:压缩文件夹无效
>= PowerShell 5.0
根据上面@SonnyPuijk 的回答,使用 Compress-Archive
.
clear-host[字符串]$zipFN = 'c:\temp\myZipFile.zip'[字符串]$fileToZip = 'c:\temp\myTestFile.dat'压缩归档 -Path $fileToZip -Update -DestinationPath $zipFN
要将单个文件添加到现有 zip:
clear-host添加类型 -assembly 'System.IO.Compression'添加类型 -assembly 'System.IO.Compression.FileSystem'[字符串]$zipFN = 'c:\temp\myZipFile.zip'[字符串]$fileToZip = 'c:\temp\myTestFile.dat'[System.IO.Compression.ZipArchive]$ZipFile = [System.IO.Compression.ZipFile]::Open($zipFN, ([System.IO.Compression.ZipArchiveMode]::Update))[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipFile, $fileToZip, (Split-Path $fileToZip -Leaf))$ZipFile.Dispose()
要从头开始创建单个文件 zip 文件:
同上,只替换:[System.IO.Compression.ZipArchiveMode]::Update
使用:[System.IO.Compression.ZipArchiveMode]::Create
相关文档:
压缩存档
:https://technet.microsoft.com/en-us/library/dn841358.aspxCreateEntryFromFile
:https://msdn.microsoft.com/en-us/library/hh485720(v=vs.110).aspx
I am trying to copy/move one text file to the zip. I don't want to unzip it, copy the file and zip it back. Is there any way we can directly copy or move text file to the zip in powershell. When i am doing it in powershell, it's doing after that when i try to look inside the zip it's saying invalid path.
Powershell commands:
$A = "20160914.4"
New-Item "C:\test\VersionLabel.txt" -ItemType file
$A | Set-Content "C:\test\VersionLabel.txt"
Copy-Item "C:\test\VersionLabel.txt" "C:\test\Abc.zip" -Force
Error: The compressed folder is invalid
>= PowerShell 5.0
Per @SonnyPuijk's answer above, use Compress-Archive
.
clear-host
[string]$zipFN = 'c:\temp\myZipFile.zip'
[string]$fileToZip = 'c:\temp\myTestFile.dat'
Compress-Archive -Path $fileToZip -Update -DestinationPath $zipFN
< PowerShell 5.0
To add a single file to an existing zip:
clear-host
Add-Type -assembly 'System.IO.Compression'
Add-Type -assembly 'System.IO.Compression.FileSystem'
[string]$zipFN = 'c:\temp\myZipFile.zip'
[string]$fileToZip = 'c:\temp\myTestFile.dat'
[System.IO.Compression.ZipArchive]$ZipFile = [System.IO.Compression.ZipFile]::Open($zipFN, ([System.IO.Compression.ZipArchiveMode]::Update))
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipFile, $fileToZip, (Split-Path $fileToZip -Leaf))
$ZipFile.Dispose()
To create a single file zip file from scratch:
Same as above, only replace: [System.IO.Compression.ZipArchiveMode]::Update
With: [System.IO.Compression.ZipArchiveMode]::Create
Related Documentation:
Compress-Archive
: https://technet.microsoft.com/en-us/library/dn841358.aspxCreateEntryFromFile
: https://msdn.microsoft.com/en-us/library/hh485720(v=vs.110).aspx
这篇关于如何将单个txt文件移动到powershell 3中的zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!