Introduction
After years of backing up my files on CDs, I found that I cannot remember the CD so I have to choose some suspicious CDs and search for my files on it! This program can save the CD/DVD file properties to the PC so that when I want to search for a file, I need not search every CD. All I need to do is search for the specified file by entering the key and click the Search button. The code does this in the following manner:
- To get and save data:
- Gets all file names with its primary properties and directories
- Saves this data to
DireNode
and FileNode
objects
- Serializes the data to a stream
- Compresses the serialized stream and saves it to a file
- To retrieve data:
- Decompresses the data
- Deserializes data and links it to a treeview control
- To search loop on all files in the specified directory:
- Decompresses the data
- Deserializes data
- Searches using the keyword, if found shows the results in the treeview
Using the Code
To use the code:
BuildSaveAndRetrieve bsr;
bool search;
Use this method to build the file.
- The first param is the selected DVD drive
- The second param is the full file name to save the data
bsr.Build(drivename, fileName);
To load nodes:
bsr.LoadNodes(fileName, out initNode);
- The first parameter is the file name where the needed data is saved
- The second is the treenode where the data is retrieved
To search for a key in a directory, the first parameter is the location
of the data files, the second is for the keyword
:
bsr.SearchAll(location, keyword);
To find a key in a specified node:
bsr.Find(key, currDirNode);
You can also use compression and decompression. For example (De)Compress, (De)Serialization:
public Stream DeflateDecompress(string fileName)
{
try
{
FileStream fs= new FileStream(fileName, FileMode.Open,
FileAccess.Read, FileShare.None);
return (new DeflateStream(fs, CompressionMode.Decompress));
}
catch (Exception e)
{
ErrorMessage(e.Message);
return null;
}
}
public bool DeflateCompress(Stream ms, string fileName)
{
try
{
byte[] buffer = new byte[ms.Length];
ms.Position = 0;
if ((ms.Read(buffer, 0, buffer.Length)) != ms.Length)
{
ErrorMessage("Unable to read data from memory");
return false;
}
ms.Close();
FileStream fs = new FileStream(fileName, FileMode.Create,
FileAccess.Write, FileShare.None);
DeflateStream compressedzipStream = new DeflateStream
(fs, CompressionMode.Compress, true);
compressedzipStream.Write(buffer, 0, buffer.Length);
compressedzipStream.Close();
fs.Close();
fs = new FileStream(fileName, FileMode.Open);
DeflateStream zipStream = new DeflateStream
(fs, CompressionMode.Decompress);
if (!TestData(buffer, zipStream))
{
zipStream.Close();
return false;
}
else
{
zipStream.Close();
return true;
}
}
catch (Exception e)
{
ErrorMessage(e.Message);
return false;
}
}
public static bool TestData(byte[] bufferSrc, Stream decompressed)
{
int bufferSrcLength = bufferSrc.Length;
int decompressedStreamLength = 0;
byte[] buffer = new byte[bufferSrcLength];
decompressedStreamLength = decompressed.Read(buffer, 0, bufferSrcLength);
if (decompressedStreamLength != bufferSrcLength)
return false;
for (int i = 0; i < decompressedStreamLength; i++)
{
if (bufferSrc[i] != buffer[i])
return false;
}
return true;
}
private Stream SerializeNodes(string fileName, DirNode rootCDDVDDir)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, rootCDDVDDir);
return ms;
}
private static DirNode DeserializeNodes(Stream s)
{
BinaryFormatter bf = new BinaryFormatter();
try
{
DirNode retrievedRootDirNode = (DirNode)bf.Deserialize(s);
s.Close();
return retrievedRootDirNode;
}
catch (Exception e)
{
ErrorMessage(e.Message);
s.Close();
return null;
}
}
Points of Interest
The important things are these methods above and the two class definitions. The other important things are the Save
, Retrieve
, Find
and SearchAll
methods.
The code in the project is more descriptive than my words (I think) because it is my first article.
I'm waiting for your feedback.
That is all.
Hope you enjoy the code.
History
- 24th March, 2006: Initial post