You can put your files in a list of custom class:
[Serializable]
public class FileEntry
{
public string FileName {get;set;}
public string FileRelativePath {get;set;}
public byte[] FileContents {get;set;}
}
Add the files to a list:
List<FileEntry> files = new List<FileEntry> ();
for .......
{
files.Add(new FileEntry()
{
FileName = .....,
FileRelativePath = .....,
FileContents = File.ReadAllBytes(......),
};
}
And then, use BinarryFormatter to convert this structure to byte-array:
byte[] filesBytes;
BinaryFormatter ser = new BinaryFormatter();
using(MemoryStream ms = new MemoryStream())
{
ser.Serialize(ms, files);
filesBytes = ms.ToArray();
}
Now, you have your structure as byte[], you can easily encrypt them with some easy way as this:
filesBytes = Encrypt(filesBytes , ......);
Then save the encrypted bytes to some location with some custom extension:
File.WriteAllBytes(".........\.....encr",filesBytes);
Then, when you want to re-open the file and read the clear data:
byte[] encryptedData = File.ReadAllBytes(".......\.....encr");
Decrypt the contents with the same algorithm:
byte[] clearContent = Decrypt(encryptedData, ......);
And deserialize the contents into the primary structure:
BinaryFormatter ser = new BinaryFormatter();
using(MemoryStream ms = new MemoryStream(clearContent))
{
List<FileEntry> files = ser.Deserialize(ms) as List<FileEntry>;
}
And then, write the content of the files to some location if you want:
foreach(var file in files)
{
File.WriteAllBytes(string.Format("........{0}...{1}",file.FileRelativePath , file.FileName), file.FileContents)
}
You can use this question about encryption:
Easy way to encrypt/obfuscate a byte array using a secret in .NET?
And this is an example about binary formatter:
I have posted this answer to my blog :)