/// <summary> /// Loads the directories into the storage file. /// </summary> /// <param name="reader">Reader.</param> /// <param name="numberOfDirectories">Number of directories in the storage file.</param> private void LoadDirectories(BinaryReader reader, int numberOfDirectories) { String name, path; JDirectory aux, directory; //Create the root directory _newStorage.Root = new JDirectory(""); //Auxiliary directory aux = new JDirectory("Root"); _newStorage.Root.AddDirectory(aux); for (int i = 0; i < numberOfDirectories; i++) { //Create the new directory name = reader.ReadString(); directory = new JDirectory(name); //Get the path path = reader.ReadString(); //Get the path and add the directory _newStorage.Root.AddDirectory(path, directory); } }
/// <summary> /// Adds a directory to a directory inside a path that starts in this /// directory /// </summary> /// <param name="path">Path to add the directory</param> /// <param name="directory">Directory to add</param> public void AddDirectory(String path, JDirectory directory) { JDirectory targetDirectory; targetDirectory = GetDirectory(path); if (targetDirectory == null) { throw new IOException("The path " + path + " is not correct in the JDirectory " + _name + "."); } targetDirectory.AddDirectory(directory); }
/// <summary> /// Gets the qualified names of all the files of the directory and its subdirectories. /// </summary> /// <param name="directory">Directory where to perfom the search.</param> /// <param name="searchPattern">Search pattern to match in the filenames.</param> /// <param name="fileNames">Collection where the qualified names will be added.</param> private void GetPrivateFileNames(JDirectory directory, string searchPattern, Collection <string> fileNames) { foreach (JFile file in directory.GetFiles()) { if (Match(searchPattern, file.Name, false)) { fileNames.Add(file.GetQualifiedName()); } } foreach (JDirectory childDirectory in directory.GetDirectories()) { directory.GetPrivateFileNames(childDirectory, searchPattern, fileNames); } }
/// <summary> /// Writes a <see cref="JDirectory"/> and all its subdirectories into the storage file. /// </summary> /// <param name="directory">Directory to strat writing.</param> /// <param name="path">Path of the directory.</param> private void WriteDirectories(JDirectory directory, string path) { String newPath; List <JDirectory> directories; writer.Write(directory.Name); writer.Write(path); newPath = path + Path.DirectorySeparatorChar + directory.Name; directories = directory.GetDirectories(); foreach (JDirectory childDirectory in directories) { WriteDirectories(childDirectory, newPath); } }
/// <summary> /// Writes all <see cref="JFile"/> data into the storage file. /// </summary> /// <param name="directory">Root directory to start writing.</param> private void WriteFiles(JDirectory directory) { List <JFile> files; List <JDirectory> directories; files = directory.GetFiles(); foreach (JFile file in files) { WriteFile(file); } directories = directory.GetDirectories(); foreach (JDirectory childDirectory in directories) { WriteFiles(childDirectory); } }
/// <summary> /// Writes all <see cref="JFile"/> headers in a directory and its subdirectories into the storage file. /// </summary> /// <param name="directory">Directory to start writing.</param> /// <param name="path">Path of the directory.</param> private void WriteFilesHeader(JDirectory directory, string path) { String newPath; List <JFile> files; List <JDirectory> directories; newPath = path + Path.DirectorySeparatorChar + directory.Name; files = directory.GetFiles(); foreach (JFile file in files) { WriteFileHeader(file, newPath); } directories = directory.GetDirectories(); foreach (JDirectory childDirectory in directories) { WriteFilesHeader(childDirectory, newPath); } }
/// <summary> /// Adds a directory to this directory /// </summary> /// <param name="directory">Directory to add</param> public void AddDirectory(JDirectory directory) { _directories.Add(new JFileName(directory.Name), directory); directory.Parent = this; }
/// <summary> /// Writes a <see cref="JDirectory"/> and all its subdirectories into the storage file. /// </summary> /// <param name="directory">Directory to strat writing.</param> /// <param name="path">Path of the directory.</param> private void WriteDirectories(JDirectory directory, string path) { String newPath; List<JDirectory> directories; writer.Write(directory.Name); writer.Write(path); newPath = path + Path.DirectorySeparatorChar + directory.Name; directories = directory.GetDirectories(); foreach (JDirectory childDirectory in directories) WriteDirectories(childDirectory, newPath); }
/// <summary> /// Overloaded constructor /// </summary> /// <param name="name">Name of the storage</param> /// <param name="version">Version of the storage</param> /// <param name="filters">Filters known by the storage</param> /// <param name="root">Root directory of the storage</param> public JStorageSource(String name, int version, List<IJFilter> filters, JDirectory root) : this(name) { _version = version; _root = root; if (filters != null) _filters = filters; }
/// <summary> /// Constructor that assigns the directory name and parent /// </summary> /// <param name="name">Name of the directory</param> /// <param name="parent">Parent of the directory</param> public JDirectory(String name, JDirectory parent) : this(name) { _parent = parent; }
/// <summary> /// Gets the qualified names of all the files of the directory and its subdirectories. /// </summary> /// <param name="directory">Directory where to perfom the search.</param> /// <param name="searchPattern">Search pattern to match in the filenames.</param> /// <param name="fileNames">Collection where the qualified names will be added.</param> private void GetPrivateFileNames(JDirectory directory, string searchPattern, Collection<string> fileNames) { foreach (JFile file in directory.GetFiles()) if (Match(searchPattern, file.Name, false)) fileNames.Add(file.GetQualifiedName()); foreach (JDirectory childDirectory in directory.GetDirectories()) directory.GetPrivateFileNames(childDirectory, searchPattern, fileNames); }
/// <summary> /// Adds a directory to a directory inside a path that starts in this /// directory /// </summary> /// <param name="path">Path to add the directory</param> /// <param name="directory">Directory to add</param> public void AddDirectory(String path, JDirectory directory) { JDirectory targetDirectory; targetDirectory = GetDirectory(path); if (targetDirectory == null) throw new IOException("The path " + path + " is not correct in the JDirectory " + _name + "."); targetDirectory.AddDirectory(directory); }
/// <summary> /// Writes all <see cref="JFile"/> data into the storage file. /// </summary> /// <param name="directory">Root directory to start writing.</param> private void WriteFiles(JDirectory directory) { List<JFile> files; List<JDirectory> directories; files = directory.GetFiles(); foreach (JFile file in files) WriteFile(file); directories = directory.GetDirectories(); foreach (JDirectory childDirectory in directories) WriteFiles(childDirectory); }
/// <summary> /// Writes all <see cref="JFile"/> headers in a directory and its subdirectories into the storage file. /// </summary> /// <param name="directory">Directory to start writing.</param> /// <param name="path">Path of the directory.</param> private void WriteFilesHeader(JDirectory directory, string path) { String newPath; List<JFile> files; List<JDirectory> directories; newPath = path + Path.DirectorySeparatorChar + directory.Name; files = directory.GetFiles(); foreach (JFile file in files) WriteFileHeader(file, newPath); directories = directory.GetDirectories(); foreach (JDirectory childDirectory in directories) WriteFilesHeader(childDirectory, newPath); }
/// <summary> /// Extracts the contents of one directory and all the subdirectories inside it /// </summary> /// <param name="path">Path to extract the contents</param> /// <param name="rootDirectory">Root directory where the extraction begins</param> private void ExtractDirectoryFiles(String path, JDirectory rootDirectory) { Stream inFile, outFile; BinaryWriter bw = null; byte[] bytes; int bytesRead; // If the path doesn't exist, create it if (!Directory.Exists(path)) Directory.CreateDirectory(path); // For each file in the root node foreach (JFile file in rootDirectory.GetFiles()) { try { // Create the new hard disk file outFile = File.Open(System.IO.Path.Combine(path, file.Name), FileMode.Create, FileAccess.Write); bw = new BinaryWriter(outFile); inFile = GetStream(file); // Write the storage data to it bytes = new byte[4096]; while ((bytesRead = inFile.Read(bytes, 0, bytes.Length)) != 0) bw.Write(bytes, 0, bytesRead); bw.Flush(); } catch (Exception) { } finally { if (bw != null) bw.Close(); } } foreach (JDirectory directory in rootDirectory.GetDirectories()) ExtractDirectoryFiles(System.IO.Path.Combine(path, directory.Name), directory); }