Click here to Skip to main content
Licence CPOL
First Posted 1 Nov 2004
Views 40,649
Downloads 287
Bookmarked 21 times

Working with Archived files

By | 31 Oct 2004 | Article
Some time ago, I faced a different concept of file when I worked on a mainframe: one that holds multiple other files. Since then, I tried to reproduce this behavior in .NET.

Introduction

Some time ago, I was sent to work with an old mainframe system. There I became familiar with a source repository where a single file on the system contains several files from the source code. Although this seems very familiar to many people (through TAR or ZIP files), I wanted the ability to work with the files within without copying them to a temporary location. This article is the result of this quest.

The Archive Class

I would be very unpractical to create such a class just to store files. It would work just like many other compression libraries out there – except for no compression at all. Plus, it would impose many limitations to store just files and no directories to organize them.

I created a simple file system based on what has been described as WinFS (Windows Longhorn FileSystem based on SQL). A single file contains a table with every file or folder inside the Archive. It's a simple record, so there's nothing more than a name, number of parent folder, entry points for files, or indexes for folders. I chose to record no dates or times. I also chose to use UNIX-style path separator to avoid escaped backslashes or verbatim strings.

In the end, it's a very simple class containing many of the functionality provided by File and Directory classes in the System.IO namespace, including methods like OpenRead and OpenText.

Why would I use it?

At first sight, many would consider a waste of time to use an Archive. And maybe you're right. The usage of every piece of technology depends on its need within the project. Maybe it's not your case.

At first, I designed an Archive to contain source codes for all of my projects. When it was near completion, I realized it wasn't such a great idea but found other uses for it such as small databases (specially to store serialized objects) and/or files I don't want users picking at. You can even find others I haven't thought of, let us know.

Limitations

As far as I can see, there are very few limits to expose:

  • An Int32 is used to index file blocks, so the archive size limit is 1,5Kb * 2,147,483,647 (= 3,221,225,470 Kb, theoretically). This is also the biggest a single file can get.
  • Directories differ from files by using negative start indexes, thus limiting an Archive to 2,147,483,648 directories. Currently, a stored index is used to retrieve the index for a new directory and this is never reset. (Should I revise it?)
  • Maybe others that I can't remember right now.

Expanded Universe

This is the goal of open-source, isn't it? So, the Archive class is made easy to understand, maintain and modify. In a few minutes reading the code, you'll be able to expand the class personalizing it to your own needs. Here are some examples that can be easily accomplished:

  • Replace the "Archive:" header for a more complex version detailing the use or meaning of the files within;
  • Simple scramble cryptography – OK, to use a crypto-stream, the file would require a temporary stream, but some simple cryptography (i.e., using XOR) is easy;
  • Additional file information like creation and modification times (which I chose not to include);
  • A new property to define which character is to be used for path separation;
  • etc.

Some of the modifications might even be useful to other users, so I suggest if you modify the Archive class, post a brief of the modifications you made here too.

Points of Interest

The code has been widely tested, except for files (as can be seen in the Main procedure), although the code used was tested in a previous version. So, there should be no problems with it. If there are any problems, let me know.

Also, the Archive class uses widely .NET Generic collections, limiting its use to .NET Framework 2.0. If anyone is interested in porting it to prior versions, I can post the results here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Leonardo Pessoa

Software Developer

Brazil Brazil

Member

Follow on Twitter Follow on Twitter
I'm that strange type, who likes to code (mostly Objective-C and Javascript nowadays) and hates to use a database (I'd rather code one, instead!). I'm mainly interested in programming languages, compilers, interpreters and the like.
 
Although I don't place any form of restriction upon using the codes I provide, I would appreciate to be mentioned as it's author in any projects using them.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAppending files to the archive Pinmemberallanhaugsted10:23 19 Sep '05  
AnswerRe: Appending files to the archive PinmemberHarkos1:59 23 Sep '05  
GeneralRe: Appending files to the archive Pinmemberscottcurrier12:37 10 Dec '05  
AnswerRe: Appending files to the archive PinmemberHarkos0:07 12 Dec '05  
Hi Scott,
 
Thanks for your reply. I'm glad my work is of help to others. I'll try to help as much as I can. First, I have two comments about why you like this project:
  1. You're correct. I don't load any file into memory. The Archive and all other classes are an interface to a single file. If you read the sources, you know that when you read an archived stream, it reads directly from disk as much as you need.
  2. I really posed no GPL or other open source licenses because I really don't believe in this open source madness. At any time the can pull they're licenses and charge you for that. Even Torvalds did it. This is no open source at all; if you want to give code away, give it with no restrictions. I put no licenses, but I'd like to be mentioned as author of the class if it's used in any projects.
  3. I'm specially glad to hear the the code is simple to use, lightweight and fast. I never performed any test on this matter, so thanks again.
Now, about your doubts:
  1. The directory entry is a very simple one. It was meant to record only filenames, start offsets and parent directory ID. No size or date/times. But it can be altered to, if you want. Also, take a look at the FileInfo class, property Length. You will see how I can get sizes and perhaps understand a little better how the pseudo-filesystem works.
  2. There is no CreateNew method, but I can assume your creating a file by opening it. And you're right; there seems to be a bug here. After creating a directory, there is a call to UpdateDirs, where all entries are written to the real file. This doesn't happen when a file is created, only when deleted. You can correct it yourself (I with little time now, but as soon as I can, I will update the sources); search the Archive class for the last Open method. You will see a switch instruction and a FileMode.Create option. Before it ends, on the break instruction, add the line UpdateDirs(); to it. This shall solve the problem.
  3. As for the file size problem, I'll need a little more time to review the code and see if there is really any bug. If there is, I might post it back together with other fixes.
I hope this helps for now, but I'll try to solve these issues as soon as I can.
 
[]'s
Harkos
---
"Money isn't our god, integrity will free our soul."
Cut Throat - Sepultura
GeneralMultiple streams using NTFS PinmemberSteven Campbell8:15 1 Nov '04  
GeneralRe: Multiple streams using NTFS PinmemberHarkos8:36 2 Nov '04  
QuestionMisnamed article and why do this? PinmemberDale Thompson3:34 1 Nov '04  
AnswerRe: Misnamed article and why do this? PinmemberHarkos6:42 1 Nov '04  
AnswerRe: Misnamed article and why do this? Pinmemberjdraper36:37 8 Aug '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 1 Nov 2004
Article Copyright 2004 by Leonardo Pessoa
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid