0

i would like to know how could I possibly use the programming language C to create a file archiver such as tar. Im stuck on the first bit on how to copy a bunch of files into one file, and then extrating them back out of that one file. Any help would be appreciated thanks.

analk
  • 1
  • 1
  • 2

2 Answers2

2

It's a good idea to read up on the tar format for some inspiration.

http://en.wikipedia.org/wiki/Tar_%28file_format%29

http://www.gnu.org/software/automake/manual/tar/Standard.html

It's quite simple and shouldn't be too hard to implement yourself, if you got a good grasp of basic C I/O.

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
0

Assuming you don't want compression, which is pretty hard, and just want's something REALLY simple, you are gonna need to do the following:

Create a file to hold all the files you want.

Fetch one of the files you want to archive, get it's name, name_size and it's size.

Write the name_size of the name, name, size of the file, and the size * bytes of the file into the archive one. Repeat to all of the files you want to archive.

To get the files back from the one archive, you are gonna need to read the name's size, create that file with the next name_size next bytes, then read the size of the file bytes, and write them to the single file you created.

You would have this:

File1:


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


FileN:


yyyyyyyyyyyyyyyyyyyy


After the archiving you would have:


5File1size of File1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5FileNsizeof FileNyyyyyyyyyyyyyyyyyyyy


hfingler
  • 1,931
  • 4
  • 29
  • 36
  • Don't forget file type - good to know if it's a file, link or directory. Also permissions and times can be useful. – Johan Kotlinski Nov 26 '10 at 19:09
  • @kotlinksi, yeah, i was assuming he only had normal files and had the permissions, but it's always good to check it. Also, if you copy the raw bytes of the file, and move to another file, i will have the exact same attributes of the original one, because it has the same file header. As for OS attributes, it can get hard, because it is OS dependent, and copying those is messing with the OS's API. – hfingler Nov 26 '10 at 19:12
  • thanks im going to try and apply the logic mentioned with the information learnt from the above links. – analk Nov 28 '10 at 17:35
  • could you help me with some pseduo code perhaps with the second part you mentioned: get the name its name_size and its size...(The rest I think I may understand once I get this concept) say i use a struct and then put those 3 intoit.then create an inputfile. then do... or is there another way? – analk Nov 28 '10 at 18:07
  • @analk, well, if the files are passed as a parameter, then you know their size using `strlen`, and you have the chars of the file, because.. well.. you have 'em. To get the file size you can do different stuff, but using `stat` (first parameter is the filename) is an easy way to do it, and you can get some other properties of the file. Check this link: `http://www.cplusplus.com/forum/windows/10853/`. If you don't have the filenames, and want to get all of the files inside a folder, check this link to get all filenames `http://stackoverflow.com/questions/612097/`. – hfingler Nov 29 '10 at 00:36
  • continuing: If you want to archive folders too, like a folder with some other folders and files, and when you extract the file you want to keep the organization, that method won't work, you are gonna have to write some other stuff to the file, like the folder(s) the file is in. – hfingler Nov 29 '10 at 00:41