Hallo,
hier mal ein kleiner Include, mit dem es möglich ist, unkomprimierte Dateiarchive mit BlitzMax zu erstellen und auszulesen: BlitzMax: [AUSKLAPPEN] [EINKLAPPEN]
Private Global SAVEFUNCVERSION:String = "1.1" Global READFUNCVERSION:String = "1.1"
Public Function SavePacFile:Int(Path:String, Files:TList) Local PAC:TStream = WriteFile(Path) If(PAC <> Null) If(Files = Null) Return 0 EndIf WriteLine(PAC, "%PAC" + Replace(SAVEFUNCVERSION, ".", "") + "%") WriteLine(PAC, "[REG]") For Local x:String = EachIn Files WriteLine(PAC, StripDir(x)) WriteInt(PAC, FileSize(x)) Next WriteLine(PAC, "[DAT]") Local TempSrcFile:TStream For x = EachIn Files TempSrcFile = ReadFile(x) If(TempSrcFile = Null) CloseFile(PAC) DeleteFile(Path) Return 0 EndIf Repeat WriteByte(PAC, ReadByte(TempSrcFile)) Until Eof(TempSrcFile) CloseFile(TempSrcFile) Next CloseFile(PAC) Return 1 Else Return 0 EndIf End Function
Function LoadFileFromPac:TBank(PAC:TStream, FileName:String) If(PAC <> Null) SeekStream(PAC, 0) If(ReadLine(PAC) <> "%PAC" + Replace(READFUNCVERSION, ".", "") + "%") Print("PACERR: Wrong PAC-Version found!") Return Null EndIf If(ReadLine(PAC) <> "[REG]") Print("PACERR: Wrong Datastructure found!") Return Null EndIf Local ErrCode:Int = 0 Local SeekSize:Int = 0 Local SearchFileName:String = "" Local SearchFileSize:Int = 0 Repeat SearchFileName = ReadLine(PAC) SearchFileSize = ReadInt(PAC) SeekSize = SeekSize + SearchFileSize If(Eof(PAC)) ErrCode = 1 Exit EndIf If(SearchFileName = "[DAT]") ErrCode = 2 Exit EndIf Until SearchFileName = FileName SeekSize = SeekSize - SearchFileSize If(ErrCode = 0) Local TempString:String = "" Repeat TempString = ReadLine(PAC) If(TempString = "[DAT]") Exit EndIf ReadInt(PAC) Until Eof(PAC) If(Eof(PAC)) Return Null EndIf SeekStream(PAC, (StreamPos(PAC) + SeekSize)) Local ReturnMe:TBank = CreateBank(SearchFileSize) ReadBank(ReturnMe, PAC, 0, SearchFileSize) Return ReturnMe Else Print("PACERR: Unexpected End-of-File") Return Null EndIf Else Return Null EndIf End Function
Function ListPacContent:Int(Path:String) Local PAC:TStream = ReadFile(Path) If(PAC <> Null) SeekStream(PAC, 0) If(ReadLine(PAC) <> "%PAC" + Replace(READFUNCVERSION, ".", "") + "%") Print("PACERR: Wrong PAC-Version found!") Return Null EndIf If(ReadLine(PAC) <> "[REG]") Print("PACERR: Wrong Datastructure found!") Return Null EndIf Local ListFileName:String = "" Local ListFileSize:Int = 0 Local FileCounter:Int = 1 Print("Data-Package '" + Path + "' contains:") Print("No.~tName~t~t~t~t~tSize in Bytes") Repeat ListFileName = ReadLine(PAC) ListFileSize = ReadInt(PAC) If(ListFileName <> "[DAT]") Print(" " + FileCounter + "~t" + ListFileName + "~t~t~t~t" + ListFileSize + "") EndIf FileCounter = FileCounter + 1 Until ListFileName = "[DAT]" Or Eof(PAC) CloseFile(PAC) Print("" + (FileCounter - 2) + " Files") Else Print("PACERR: Package not found!") Return 0 EndIf End Function
Wie funktionierts?
Wenn man ein Datenpaket erstellen will, ruft man einfach die Funktion SavePacFile() mit den beiden Parametern auf:
1.) Path:String - resultierende Dateipfad zum Dateiarchiv
2.) Files:TList - Eine Liste von Strings mit allen Dateien, die in das Paket sollen, also in etwa so angelegt (Pseudocode) BlitzMax: [AUSKLAPPEN] [EINKLAPPEN] Global FileList:TList = CreateList() ListAddLast(FileList, "Bild.bmp") ListAddLast(FileList, "Text.txt") ListAddLast(FileList, "NochEinBild.jpg")
Wenn man eine Datei aus einem Datenpaket "extrahieren" möchte, ruft man die Funktion LoadFileFromPac:TBank() mit den beiden Parametern auf:
1.) PAC:TStream - Das als TStream geöffnete Datenpaket (Nicht der Pfad dahin)
2.) FileName:String - Der Name der zu extrahierenden Datei (Nur der tatsächliche Dateiname, nicht der grundsätzliche Pfad (Beispiel: Statt "C:\Test\WichtigeDatei.txt" muss "WichtigeDatei.txt" angegeben werden)
3.) Die Funktion returned eine TBank, diese kann dann weiterverwendet werden.
Ich hoffe es hilft dem einen oder anderen...
|
mfG, CO²
Sprachen: BlitzMax, C, C++, C#, Java
Hardware: Windows 7 Ultimate 64-Bit, AMX FX-6350 (6x3,9 GHz), 32 GB RAM, Nvidia GeForce GTX 750 Ti
|