class TJCRCreate - Tricky1975/JCR6_Sharp GitHub Wiki
TJCRCreate can be used to create JCR6 files yourself. Please bote that JCR6 was never designed to add and replace files like you can with archives such as zip. Although ulities that can do exist, but they were also coded outside the scope of normal JCR6 usage. In basic TJCRCreate creates a completely new JCR6 file, and you can add as much files as you like to it, and once you close it the JCR6 file will be "finalized" to readers can use it.
Before you can use this class you must initiate it and assign it to variable.
Like this
var myJCRfile = new TJCRCreate("MyJCRfile.JCR","lzma");
Now I guess it looks kinda obvious, but a littlebit of explanation is in order. The first parameter will (of course) refer to the file that has to be written. The writing will start immediately, so if there's already a file with that name it will immediately be destroyed, if you do not want that back it up first. I had to go this way as JCR6 files tend to be quite large and there's no need to take up too much RAM for that. The second parameter only refers to the alogorithm used to compress the file table. Yes, in JCR6 the file table too is being compressed. If the compressed file table turns out to be larger than the uncompressed one, then no compression will take place and JCR6 will automatically revert to "Store".
Now a few notes are in order before creating JCR6 files.
- C# is a pointer based language with an automated garbage collector. The TJCRCreate class has been desinged to automatically close and finalize JCR6 files should the classes they are linked to fall through the garbage collector, but I consider it "bad design" to let it come to that, as you can better keep full control over these things. In other words be sure to use the "Close" method when your JCR6 file has all the data it needs before disposing the variable you attached this class to.
- With the AddEntry method you can create files in the JCR6 resource and write data into them. Here goes too that JCR6 has been designed to automatically close all open files that have to live inside the JCR6 file before finalizing them, and JCR6 has even been designed not to let the garbage collector get ahold of those. Yet, it's bad design not to close them first.
- None of the classes have been test in "using" statements, so I really don't know what will happen i fyou try.
- Also note that JCR6 is case insensitive. HELLO.TXT and Hello.txt are therefore the same file. JCR6 is not smart enough to see you using duplicate file names and if you do, only the one you added last (adding will happen of file closure of the file to add) will be seen and all data in the file added before will still live in the JCR6 file, but be completely unaccessible by JCR6.
- The order in which files are displayed by JCR6 listing utilities should be alphabetic by filename (that is a JCR6 design rule), but that only goes for full utilities. Libraries set up for other languages may not be able to automatically order their maps/dictionaries/tables/whatever-they-call-it (which is for example the case in the libraries for the Go language), so programmers should be aware of that. The C# reading classes have been designed to automatically sort on alphabet.
- JCR6 does support resource files that "depend" on each other, and will automatically patch to certain files if they exist, or even refuse to load without them. When overwriting existing JCR6 files these "dependencies" are not touched at all, and if the new JCR6 still needs to refer to those, the links so be re-created.
- Fields Author and Notes have under normal usage no use. They have only been put in to display this data when JCR6 viewing utilities ask for it. If you use a lot with 3rd party files, it may be good manners to use these fields.
- The 'Storage' field, which is by default always set to "Store" (for obvious reasons) can be used to decide the Storage algorithm. The "BRUTE" parameter will in future versoins be used to make JCR6 try all known algorithms and pick the one with the best result. I think it goes without saying, but the class with the driver for that algorithm MUST BE DECLARED first before adding files. If it doesn't exist, the request will result into a failure.
- JCR6 has no "official" directory supprt. In order to make a directory structure just use the slash ("/") as path separator (never use backslash "", not even on Windows systems. JCR6 can act very odd to that, and some JCR6 instalments may even throw an error or cause other security features to be put in motion). Due to this it is also NOT possible to add empty directories to JCR6.
Copies all reference data from one existing entry to another. This will basically mean that there are two files refering to the same data, while the data itself only lives once inside the JCR6 file. I've used this feature a lot in "The Secrets of Dyrt" (which uses JCR5 which already had this feature present) and Star Story and The Fairy REVAMPED would also have been handicapped without it. To cut if very short, it's just two links, same data.
var myJCRfile = new TJCRCreate("MyJCRfile.JCR","lzma");
MyJCRFile.AddString("Hello World!","File1.txt");
MyJCRFile.Alias("File1.txt","File2.txt");
MYJCRFile.Close();
Now File1 and File2 contain the same reference and will thus both "contain" the text "Hello World!", while the text "Hello World!" itself can only be found inside the JCR6 file once.
void AddString(string mystring, string Entry, string Storage="Store", string Author = "", string Notes = "")
Adds a string as a file to a JCR6 file. Well this example demonstrates it, I guess
var myJCRfile = new TJCRCreate("MyJCRfile.JCR","lzma");
MyJCRFile.AddString("Hello World!","File1.txt");
MYJCRFile.Close();
void AddBytes(byte[] mybuffer, string Entry, string Storage = "Store", string Author = "", string Notes = "")
Adds an array of bytes to a JCR6 file. This method has most of all been set up when you don't want to use the features JCR6 provides for file composition itself, but want to use your own, using memory streams and such. If you have some binary data as byte arrays in memory, you can of course use this feature as well. Please note, this feature creates, writes and closes all in once, so only add bytes in their "final form".
void AddFile(string OriginalFile, string Entry, string Storage="Store", string Author = "", string Notes = "")
Copy a file from your file system into a JCR6 resource
TJCRCreateStream NewEntry(string Entry, string Storage="", string Author = "", string Notes = "", byte Endian = QOpen.LittleEndian)
Opens a file for writing, which will be added to the JCR6 file as soon as you close it.
- For the methods you can use to write the data, please take a look in the document about hte TJCRCreateStream class
- When you close the JCR6 file as a whole, all TJCRCreateStream files still attached to it will be closed and added before finalizing the JCR6 file. This is only a security measure as it's deemed bad design to fully rely on this.
- By default all integers are written in LittleEndian, yes, even when you run this program in an BigEndian CPU. This to prevent compatibility issues. You can set it to QOpen.BigEndian if you want to write the data in BigEndian, or set the value to 0 in order not to check it at all and to leave it to the CPU, but I recommend against it unless you KNOW what you are doing.
Adds a comment to a JCR6 file. These comments are solely meant for documentation purposes, and are only meant to be displayed by listing utilities.
Close all entries opened with NewEntry.
Closes and finalizes the JCR6 resource to make it ready to use. All entries you created with New NewEntry, will if they are still open, be closed and added automatically before finalizing.