如何将单个txt文件移动到powershell 3中的zip [英] How to move a single txt file to the zip in powershell 3

查看:61
本文介绍了如何将单个txt文件移动到powershell 3中的zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个文本文件复制/移动到 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

相关文档:

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:

这篇关于如何将单个txt文件移动到powershell 3中的zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆