Saving and Putting back.

DalexL

Active member
Joined
Jul 3, 2010
Messages
34
Programming Experience
3-5
I wanted to make a program in which multiple files could be saved into one and then unpacked later. I tried reading and then rewriting the exact strings but it made the files corrupt. I was able to do it with bytes but I'm not sure how I add mutiple files together. Any help would be appreciated.
 
You can open a FileStream to write binary data to a file. Call File.ReadAllBytes to get a Byte array containing the contents of a file, then call Write on your FileStream to write that data out. Do that for each file you want to add, then close the FileStream.

The thing is, you need to know where each file begins and ends in order to be able to successfully read the data back out again. I would suggest that you precede the data for a file with a 64 bit number that contains the number of Bytes in that file. That way you will read a number, then read that many Bytes to get the file. You might even want to include some other information in the header, like the name of the file. With this in mind, I'd suggest using a BinaryWriter to write the data. You create the FileStream, then create the BinaryWriter and pass the stream to the constructor. Likewise use a FileStream and a BinaryReader to read the data back again.
 
Is this a quick process? If so, could you please provide a quick example of it? This is a completely different area than what I'm used to.

Thanks!
 
As is always the case, I would like to see you take the information already provided and have a go. If an example is what you want then you can find plenty already on the web. Read what you need to and do what you can. If you have issues then post back and show us exactly what you've done and tell us exactly what you're stuck on. In that situation, I am more than happy to help you fix the problem, but I'm not really interested in writing the code for you because, in my experience, doing so provides less overall value.
 
Back
Top