1

最后更新:原来我不需要二进制作家。我可以将内存流从一个存档复制到另一个存档。

我正在重新编写一个适用于档案的 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
    }

    
}```
4

1 回答 1

2

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()
于 2022-02-27T13:55:28.757 回答