我正在重新撰寫一個適用于檔案的 PowerShell 腳本。我在這里使用了兩個功能
無需匯入和匯出檔案即可展開存檔
并且可以成功地讀寫檔案到存檔。我已經發布了整個程式,以防萬一有人幫助我更清楚。
但是,存在三個問題(除了我真的不知道自己在做什么的事實之外)。
1.) 大多數檔案在嘗試運行時都會出現此錯誤
Add-ZipEntry -ZipFilePath ($OriginalArchivePath $PartFileDirectoryName) -EntryPath $entry.FullName -Content $fileBytes}
無法將值“507”轉換為型別“System.Byte”。錯誤:“對于無符號位元組,值太大或太小。” (用位元組陣列中的任何數字替換 507)
2.) 當它讀取一個檔案并將其添加到 zip 存檔 (*.imscc) 時,它會在檔案內容的開頭添加一個字符“a”。
3.)唯一不會出錯的檔案是文本檔案,當我真的希望它處理任何檔案時
感謝您的任何幫助!
更新:我嘗試使用 System.IO.BinaryWriter,但錯誤相同。
Add-Type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.IO.Compression'
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
function Folder-SuffixGenerator($SplitFileCounter)
{
return ' (' $usrSuffix ' ' $SplitFileCounter ')'
}
function Get-ZipEntryContent(#returns the bytes of the first matching entry
[string] $ZipFilePath, #optional - specify a ZipStream or path
[IO.Stream] $ZipStream = (New-Object IO.FileStream($ZipFilePath, [IO.FileMode]::Open)),
[string] $EntryPath){
$ZipArchive = New-Object IO.Compression.ZipArchive($ZipStream, [IO.Compression.ZipArchiveMode]::Read)
$buf = New-Object byte[] (0) #return an empty byte array if not found
$ZipArchive.GetEntry($EntryPath) | ?{$_} | %{ #GetEntry returns first matching entry or null if there is no match
$buf = New-Object byte[] ($_.Length)
Write-Verbose " reading: $($_.Name)"
$_.Open().Read($buf,0,$buf.Length)
}
$ZipArchive.Dispose()
$ZipStream.Close()
$ZipStream.Dispose()
return ,$buf
}
function Add-ZipEntry(#Adds an entry to the $ZipStream. Sample call: Add-ZipEntry -ZipFilePath "$PSScriptRoot\temp.zip" -EntryPath Test.xml -Content ([text.encoding]::UTF8.GetBytes("Testing"))
[string] $ZipFilePath, #optional - specify a ZipStream or path
[IO.Stream] $ZipStream = (New-Object IO.FileStream($ZipFilePath, [IO.FileMode]::OpenOrCreate)),
[string] $EntryPath,
[byte[]] $Content,
[switch] $OverWrite, #if specified, will not create a second copy of an existing entry
[switch] $PassThru ){#return a copy of $ZipStream
$ZipArchive = New-Object IO.Compression.ZipArchive($ZipStream, [IO.Compression.ZipArchiveMode]::Update, $true)
$ExistingEntry = $ZipArchive.GetEntry($EntryPath) | ?{$_}
If($OverWrite -and $ExistingEntry){
Write-Verbose " deleting existing $($ExistingEntry.FullName)"
$ExistingEntry.Delete()
}
$Entry = $ZipArchive.CreateEntry($EntryPath)
$WriteStream = New-Object System.IO.StreamWriter($Entry.Open())
$WriteStream.Write($Content,0,$Content.Length)
$WriteStream.Flush()
$WriteStream.Dispose()
$ZipArchive.Dispose()
If($PassThru){
$OutStream = New-Object System.IO.MemoryStream
$ZipStream.Seek(0, 'Begin') | Out-Null
$ZipStream.CopyTo($OutStream)
}
$ZipStream.Close()
$ZipStream.Dispose()
If($PassThru){$OutStream}
}
$NoDeleteFiles = @('files_meta.xml' ,'course_settings.xml', 'assignment_groups.xml', 'canvas_export.txt', 'imsmanifest.xml')
Set-Variable usrSuffix -Option ReadOnly -Value 'part' -Force
$MaxImportFileSize = 1000
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
$SplitFileCounter = 1
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser.filter = "Canvas Export Files (*.imscc)| *.imscc"
[void]$FileBrowser.ShowDialog()
$FileBrowser.FileName
$FilePath = $FileBrowser.FileName
$OriginalArchivePath = $FilePath.Substring(0,$FilePath.Length-6)
$PartFileDirectoryName = $OriginalArchive (Folder-SuffixGenerator($SplitFileCounter)) '.imscc'
$CourseZip = [IO.Compression.ZipFile]::OpenRead($FilePath)
$CourseZipFiles = $CourseZip.Entries | Sort Length -Descending
$CourseZip.Dispose()
<#
$SortingTable = $CourseZip.entries | Select Fullname,
@{Name="Size";Expression={$_.length}},
@{Name="CompressedSize";Expression={$_.Compressedlength}},
@{Name="PctZip";Expression={[math]::Round(($_.compressedlength/$_.length)*100,2)}}|
Sort Size -Descending | format-table –AutoSize
#>
# Add mandatory files
ForEach($entry in $CourseZipFiles)
{
if ($NoDeleteFiles.Contains($entry.Name)){
Write-Output "Adding to Zip" $entry.FullName
# Add to Zip
$fileBytes = Get-ZipEntryContent -ZipFilePath $FilePath -EntryPath $entry.FullName
Add-ZipEntry -ZipFilePath ($OriginalArchivePath $PartFileDirectoryName) -EntryPath $entry.FullName -Content $fileBytes
}
}```
uj5u.com熱心網友回復:
System.IO.StreamWriter是文本撰寫器,因此不適合撰寫原始位元組。表示將文本(由有效代碼點(范圍- )的實體組成的 .NET 字串)轉換為實體(- )Cannot convert value "507" to type "System.Byte"的嘗試不恰當。因此,任何代碼點大于( ) 的 Unicode 字符都會導致此錯誤。[char][uint16]0x00xffff[byte]0x00xff2550xff
解決方案是使用允許寫入原始位元組的 .NET API,即System.IO.BinaryWriter:
$WriteStream = [System.IO.BinaryWriter]::new($Entry.Open())
$WriteStream.Write($Content)
$WriteStream.Flush()
$WriteStream.Dispose()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434567.html
標籤:电源外壳 压缩 文件流 system.io.compression 压缩流
上一篇:了解晦澀的PowerShell陣列語法:為什么@(11..20 1)有效而@(1 11..20)無效?我怎樣才能實作后者?
