I am trying to read and edit a file within a gzip file. I can do this with .zip files, but I get an error when trying to read the .gzip file. Search results so far only talk about compressing or decompressing .gzip files. If relative, the .gzip file (as per file properties) actually has a ".dat" extension. The file I need to edit within it doesn't have any extension.
Using archive As ZipArchive = Compression.ZipFile.Open(Path.Combine(SelectedWorld, "level.dat"), ZipArchiveMode.Update)
Dim entry As ZipArchiveEntry = archive.GetEntry("level")
Dim s As String = ""
Using sr As New StreamReader(entry.Open())
s = sr.ReadToEnd()
End Using
Dim M As Match = Regex.Match(s, "LevelName")
If M.Success Then
MsgBox(M.Value)
'edit word after "LevelName" <<I'm going to need help with this too.
End If
End Using
The above code throws the following error:
System.IO.InvalidDataException: 'End of Central Directory record could not be found.'
Trying the following doesn't seem to have the right stuff to read/modify a file.
Using archive As IO.Compression.GZipStream = IO.Compression.GZipStream
I have found little else for dealing with these types of files. Any help would be greatly appreciated.
CodePudding user response:
A gzip file is not a zip file. Two entirely different things. The error about not finding an end-of-central-directory record is something only found in a zip file.
A zip file is an archive, with files stored within that are independently compressed. That permits removing and adding some files without having to recompress the other files. Though the whole thing will need to be copied to move things around.
A gzip file stores only one compressed file. If you want to edit that one file, then simply gunzip it, edit it, and then re-gzip it.
Often the one compressed file in a gzip file is itself an uncompressed tar archive of many files. The extension of the file will be .tar.gz. If you want to edit the tar archive, again you would gunzip it to get a .tar file, edit it with the tar command, which can delete and append files, and then re-gzip it.
CodePudding user response:
I got it working using:
Imports Cyotek.Data.Nbt
NewName = InputBox("Enter New Name")
Dim MyFile As String = Path.Combine(SelectedWorld, "level.dat")
Dim document As NbtDocument = NbtDocument.LoadDocument(MyFile)
Dim root As TagCompound = document.DocumentRoot
root.Name = "Data"
Dim TC As TagCompound = root.GetCompound("Data")
Dim MyTag As Tag = TC.GetTag("LevelName")
MyTag.SetValue(NewName)
document.Save(MyFile)