4

for a small project, I would like to be able to store additional information about a file and keep that information with the file even when it is moved.

The additional information will be stored in a XML-file. To keep the file and its description together, I thought about using ZIP-archives without any compression, but I would like these ZIP-archives to behave just like the original files (i.e. if the original file was a video file, a double-click on the archive should open the file in the media player). This requires me to write a small program that handles this 'new' file format.

However, I have not found a solution that would allow me to open the file without first extracting the file from the archive (even without compression), which does take some time and is not what I want.

My questions are: Is there a library (for C# or C/C++) that allows me to open a zip file and directly play/open a file inside it wihout extracting the archive? Or is there an easier way to implement what I need (maybe I am thinking in the wrong direction)?

user1136324
  • 203
  • 3
  • 10
  • Yes, if you're "limited" to NTFS file system you can use [ADS](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364404(v=vs.85).aspx). – Adriano Repetti Nov 12 '13 at 13:33
  • NTFS already supports adding metadata to files, which is how it displays size info about images etc. You can also store metadata to NTFS alternate file streams. – Panagiotis Kanavos Nov 12 '13 at 13:34

1 Answers1

4

Windows already allows you to store additional metadata about a shell item (including files) through the Windows Property System.

The Windows API Code Pack includes samples and documentation on how to work with many of the native OS capabilities, including the Property System.

The following excerpts come from the PropertyEdit sample.

To get a file's property by name:

var myObject= ShellObject.FromParsingName(fileName);
IShellProperty prop = myObject.Properties.GetProperty(propertyName);

To set a string property:

if (prop.ValueType == typeof(string))
{
    (prop as ShellProperty<string>).Value = value;
}

If you don't want to use the Property System, you can use NTFS alternate data streams to store additional info about a file. There is no direct support for ADS in .NET but a simple search returns multiple wrappers, libraries and SO questions about them, eg NTFS - Alternate Data Streams

Community
  • 1
  • 1
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 1
    Although I am currently focusing on Windows, I would like to be able to use copy the files to a different OS and have them keep their metadata. – user1136324 Nov 12 '13 at 14:04
  • @user1136324 Well, given what you ask for, no matter how you implement it, portability is going to be limited by either file format, file system, operating system or some combination of those. – user2802841 Nov 12 '13 at 14:22
  • In that case, you could implement your additional .XML code into a second file, "FileName.ext.MyXMLData" etc, save it alongside the original file, and mark it as hidden. Most users won't see them. Hidden files in Linux start with a "." symbol. "Copying them" from Windows Explorer or Linux File Managers could be problematic, unless users have hidden files shown. But programatically, code could copy these hidden files easily. – rdtsc May 21 '15 at 12:14
  • The only other thing I can think of, is a complex file handler which recognizes the various formats of the files it encounters (such as Portable Executable, MPEG, WMV, etc., and actively modifies them and adds additional data sections. That's quite a task, and many file types won't support this. – rdtsc May 21 '15 at 12:17