You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取并编辑gzip压缩文件内的内容?

问题解决:读取并编辑gzip压缩的.dat文件(含LevelName修改)

你用ZipArchive操作gzip格式的.dat文件时报错,核心原因是gzip是单文件压缩格式,并非多文件归档(如Zip),ZipArchive依赖的中央目录记录在gzip文件中不存在,因此抛出System.IO.InvalidDataException: 'End of Central Directory record could not be found.'错误。

以下是完整的VB.NET实现步骤,包含读取解压、修改LevelName、重新压缩保存:


1. 读取并解压gzip格式的.dat文件

gzip仅支持单文件压缩,直接用GZipStream读取解压即可,无需查找归档条目:

Dim inputPath As String = Path.Combine(SelectedWorld, "level.dat")
Dim decompressedContent As String = ""

' 读取并解压文件
Using inputStream As New FileStream(inputPath, FileMode.Open, FileAccess.Read)
    Using gzipStream As New GZipStream(inputStream, CompressionMode.Decompress)
        Using reader As New StreamReader(gzipStream)
            decompressedContent = reader.ReadToEnd()
        End Using
    End Using
End Using

2. 修改LevelName后的内容

根据实际格式用正则表达式匹配替换,以下是两种常见格式的处理示例:

Dim newLevelName As String = "你的新存档名称"

' 情况1:格式为 LevelName:OldName 或 LevelName OldName
Dim pattern1 As String = "(LevelName\s*[:=]?\s*)\S+"
decompressedContent = Regex.Replace(decompressedContent, pattern1, "$1" & newLevelName)

' 情况2:格式为 LevelName="OldName"
' Dim pattern2 As String = "(LevelName\s*=\s*")([^"]+)(")
' decompressedContent = Regex.Replace(decompressedContent, pattern2, "$1" & newLevelName & "$3")

3. 重新压缩并保存修改后的内容

覆盖原文件或保存到新路径,示例为覆盖原文件:

Using outputStream As New FileStream(inputPath, FileMode.Create, FileAccess.Write)
    Using gzipStream As New GZipStream(outputStream, CompressionMode.Compress)
        Using writer As New StreamWriter(gzipStream)
            writer.Write(decompressedContent)
        End Using
    End Using
End Using

完整整合代码

Imports System.IO
Imports System.IO.Compression
Imports System.Text.RegularExpressions

' 替换为你的实际路径
Dim SelectedWorld As String = "你的存档目录路径"
Dim inputPath As String = Path.Combine(SelectedWorld, "level.dat")
Dim decompressedContent As String = ""

' 读取解压
Using inputStream As New FileStream(inputPath, FileMode.Open, FileAccess.Read)
    Using gzipStream As New GZipStream(inputStream, CompressionMode.Decompress)
        Using reader As New StreamReader(gzipStream)
            decompressedContent = reader.ReadToEnd()
        End Using
    End Using
End Using

' 修改LevelName
Dim newLevelName As String = "MyNewWorld"
Dim pattern As String = "(LevelName\s*[:=]?\s*)\S+"
decompressedContent = Regex.Replace(decompressedContent, pattern, "$1" & newLevelName)

' 重新压缩保存
Using outputStream As New FileStream(inputPath, FileMode.Create, FileAccess.Write)
    Using gzipStream As New GZipStream(outputStream, CompressionMode.Compress)
        Using writer As New StreamWriter(gzipStream)
            writer.Write(decompressedContent)
        End Using
    End Using
End Using

MsgBox("LevelName修改完成")

关键注意事项

  • 不要用ZipArchive处理gzip文件:gzip无归档目录结构,ZipArchive仅适用于多文件Zip格式。
  • 正则表达式需适配实际格式:如果你的LevelName格式与示例不同,可调整正则匹配规则。

内容的提问来源于stack exchange,提问作者Amerigo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 06:35:50