-2

I'm tring to create a program to archieve some files from a folder to a single binary file so I can read files from the binary archieve later. So I created a archivation method but I don't really know how can I read the files from the binary without unpacking them...

Some code:

public static void PackFiles()
{
    using (var doFile = File.Create("root.extension"))
    using (var doBinary = new BinaryWriter(doFile))
    {
        foreach (var file in Directory.GetFiles("Data"))
        {
            doBinary.Write(true); 
            doBinary.Write(Path.GetFileName(file));
            var data = File.ReadAllBytes(file);
            doBinary.Write(data.Length);
            doBinary.Write(data);
        }
        doBinary.Write(false); 
    }
}

Also, can I set a kind of "password" to the binary file so the archieve can only be unpacked if the password is known?

P.S: I dont need zip :)

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
VIclean
  • 155
  • 1
  • 2
  • 7
  • To read back the files you'd need to create a way for you to know or it to tell you where the various files start and end. For the password, you'd have to store a hash of the salted password in the binary and then have the application that reads the binary ask for a password and compare it to the salted hash in the binary. – Sam Leach May 12 '14 at 07:24
  • 1
    why don't you just write a zip file? – Jon May 12 '14 at 07:26
  • 2
    You can use [dotnetzip](http://dotnetzip.codeplex.com/) for packing, unpacking and using password. – Menelaos Vergis May 12 '14 at 07:27
  • Why? Why would you write your own archivation software, when there's so many ready for use? Many including encryption (necessary to have any kind of useful password protection) and compression (lower levels often don't limit throughput pretty much at all). You're basically saying "so I made my own archive format, but I have no idea how to make my own archive format". – Luaan May 12 '14 at 07:30
  • I want my own archivation system. Not Zip. – VIclean May 12 '14 at 10:10

1 Answers1

2

I think the best way to go is using ZIP for this.

There is a solid and fast library called dotnetzip

Example using password:

using (ZipFile zip = new ZipFile())
{
    zip.Password= "123456!";
    zip.AddFile("ReadMe.txt");
    zip.AddFile("7440-N49th.png");
    zip.AddFile("2005_Annual_Report.pdf");        
    zip.Save("Backup.zip");
}
Menelaos Vergis
  • 3,715
  • 5
  • 30
  • 46