PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/FileArchive/ArchiveFile.vb

#
Visual Basic | 119 lines | 63 code | 26 blank | 30 comment | 1 complexity | ddad055ede50eb50e99d3e1c94fbd637 MD5 | raw file
  1. Imports System.IO
  2. ''' <summary>
  3. ''' Represents a file in an archive.
  4. ''' </summary>
  5. Public Class ArchiveFile
  6. Private FFileName As String = String.Empty
  7. Private FFile As BinaryReader = Nothing
  8. Private FFileTable As FileTable = Nothing
  9. ''' <summary>
  10. ''' Do not allow construction of the <see cref="ArchiveFile" /> class.
  11. ''' </summary>
  12. ''' <param name="fileName">Name of the file.</param>
  13. Private Sub New(ByVal fileName As String)
  14. FFileName = fileName
  15. FFile = New BinaryReader(New FileStream(fileName, FileMode.Open))
  16. FFileTable = GetFileTable()
  17. End Sub
  18. ''' <summary>
  19. ''' Closes the open archive file.
  20. ''' </summary>
  21. Public Sub Close()
  22. FFile.Close()
  23. End Sub
  24. ''' <summary>
  25. ''' Creates a new archive file based on the specified file name and file table.
  26. ''' </summary>
  27. ''' <param name="fileName">Name of the file.</param>
  28. ''' <param name="fileTable">The file table that specifies what goes into the archive.</param>
  29. Public Shared Sub Create(ByVal fileName As String, ByVal fileTable As FileTable)
  30. Dim File As New BinaryWriter(New FileStream(fileName, FileMode.Create))
  31. ' Write file table
  32. WriteFileTable(File, fileTable)
  33. ' Write the files
  34. For FileIndex As Integer = 0 To fileTable.FileCount - 1
  35. Dim TableEntry As EntryData = fileTable.Files(FileIndex)
  36. Dim InputFile As New BinaryReader(New FileStream(TableEntry.FileName, FileMode.Open))
  37. Dim FileLength As Integer = CInt(InputFile.BaseStream.Length)
  38. Dim Contents As Byte() = InputFile.ReadBytes(FileLength)
  39. fileTable.SetFilePosition(TableEntry.VirtualFileName, CInt(File.BaseStream.Position))
  40. File.Write(FileLength)
  41. File.Write(Contents)
  42. InputFile.Close()
  43. Next
  44. ' Write the amended file table and close the file
  45. WriteFileTable(File, fileTable)
  46. File.Close()
  47. End Sub
  48. ''' <summary>
  49. ''' Loads the specified file.
  50. ''' </summary>
  51. ''' <param name="fileName">Name of the file to load.</param>
  52. ''' <returns>The archive file at the given location.</returns>
  53. Public Shared Function Load(ByVal fileName As String) As ArchiveFile
  54. Return New ArchiveFile(fileName)
  55. End Function
  56. Public Function GetFile(ByVal virtualFileName As String) As Byte()
  57. FFile.BaseStream.Position = FFileTable.GetFilePosition(virtualFileName)
  58. Dim FileLength As Integer = FFile.ReadInt32
  59. Return FFile.ReadBytes(FileLength)
  60. End Function
  61. ''' <summary>
  62. ''' Gets all files in the archive.
  63. ''' </summary>
  64. ''' <returns>A list of all of the files in the archive.</returns>
  65. Public Function GetAllFiles() As String()
  66. Return FFileTable.VirtualFileNames
  67. End Function
  68. Private Function GetFileTable() As FileTable
  69. FFile.BaseStream.Position = 0
  70. Dim FileCount As Integer = FFile.ReadInt32
  71. Dim Result As New FileTable
  72. For FileIndex As Integer = 0 To FileCount - 1
  73. ' Get data about the file
  74. Dim VirtualFileName As String = FFile.ReadString
  75. Dim Position As Integer = FFile.ReadInt32
  76. ' Do some sanity checking
  77. If VirtualFileName.Trim.Length = 0 Then Throw New ApplicationException("Virtual file name has not been specified.")
  78. If Position = 0 Then Throw New ApplicationException("Invalid position for file '" + VirtualFileName + "' in archive.")
  79. ' Add the file to the list
  80. Result.AddFile(VirtualFileName, VirtualFileName, Position)
  81. Next
  82. Return Result
  83. End Function
  84. Private Shared Sub WriteFileTable(ByVal file As BinaryWriter, ByVal fileTable As FileTable)
  85. file.BaseStream.Position = 0
  86. file.Write(fileTable.FileCount)
  87. For FileIndex As Integer = 0 To fileTable.FileCount - 1
  88. Dim TableEntry As EntryData = fileTable.Files(FileIndex)
  89. file.Write(TableEntry.VirtualFileName)
  90. file.Write(TableEntry.Position)
  91. Next
  92. End Sub
  93. End Class