Esempio n. 1
0
        /// <param name="tempPath">Path.Combine(
        /// Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
        ///         "Coptic Chanter", "Doc Creator", "temp")</param>
        /// <returns></returns>
        public ReadResults Read(string tempPath)
        {
            string folderPath = Path.Combine(tempPath, Name);

            if (Directory.Exists(folderPath))
            {
                foreach (string file in Directory.EnumerateFiles(folderPath))
                {
                    File.Delete(file);
                }
                Directory.Delete(folderPath);
            }
            Directory.CreateDirectory(folderPath);

            bool isSuccess = ZipStream == null?UnzipFile(ZipPath, folderPath) : UnzipFile(ZipStream, folderPath);

            if (isSuccess)
            {
                var           results = new ReadResults();
                List <string> files   = Directory.EnumerateFiles(folderPath).ToList();
                if (files.Contains(Path.Combine(folderPath, "index.xml")))
                {
                    // Create an instance of the XmlSerializer class;
                    // specify the type of object to be deserialized.
                    XmlSerializer serializer = new XmlSerializer(typeof(Index));

                    // A FileStream is needed to read the XML document.
                    string text = File.ReadAllText(Path.Combine(folderPath, "index.xml"));

                    //Use the Deserialize method to restore the object's state with
                    //data from the XML document.
                    results.Index = (Index)serializer.Deserialize(XDocument.Parse(text).CreateReader());

                    foreach (string filename in files)
                    {
                        if (filename != Path.Combine(folderPath, "index.xml") && !filename.EndsWith(".zip"))
                        {
                            try
                            {
                                var doc = CopticInterpreter.ReadDocXml(filename);
                                results.IncludedDocs.Add(doc);

                                /*// Create an instance of the XmlSerializer class;
                                 * // specify the type of object to be deserialized.
                                 * XmlSerializer serializerDoc = new XmlSerializer(typeof(DocXML));
                                 *
                                 * // A FileStream is needed to read the XML document.
                                 * string textDoc = File.ReadAllText(filename);
                                 *
                                 * //Use the Deserialize method to restore the object's state with
                                 * //data from the XML document.
                                 * var readerDoc = XDocument.Parse(textDoc).CreateReader();
                                 * var serialDoc = (DocXML)serializerDoc.Deserialize(readerDoc);
                                 * results.IncludedDocs.Add(serialDoc);*/
                            }
                            catch (Exception ex)
                            {
                                Output.WriteLine("Error: ", ex.Message);
                                Output.WriteLine("Unexpected file in set");
                            }
                        }
                    }

                    return(results);
                }
                else
                {
                    Output.WriteLine("Set file not valid: No index found");
                }
            }
            return(null);
        }
        public ReadResults Read()
        {
            string FolderPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                "Coptic Chanter", "Doc Creator", "temp",
                Path.GetFileNameWithoutExtension(ZipPath)
                );

            if (Directory.Exists(FolderPath))
            {
                foreach (string file in Directory.EnumerateFiles(FolderPath))
                {
                    File.Delete(file);
                }
                Directory.Delete(FolderPath);
            }
            Directory.CreateDirectory(FolderPath);

            if (UnzipFile(ZipPath, FolderPath))
            {
                var           results = new ReadResults();
                List <string> files   = Directory.EnumerateFiles(FolderPath).ToList();
                if (files.Contains(Path.Combine(FolderPath, "index.xml")))
                {
                    // Create an instance of the XmlSerializer class;
                    // specify the type of object to be deserialized.
                    XmlSerializer serializer = new XmlSerializer(typeof(IndexXML));

                    // A FileStream is needed to read the XML document.
                    string text = File.ReadAllText(Path.Combine(FolderPath, "index.xml"));

                    //Use the Deserialize method to restore the object's state with
                    //data from the XML document.
                    results.Index = (IndexXML)serializer.Deserialize(XDocument.Parse(text).CreateReader());

                    foreach (string filename in files)
                    {
                        if (filename != Path.Combine(FolderPath, "index.xml") && !filename.EndsWith(".zip"))
                        {
                            try
                            {
                                var doc = CopticInterpreter.ReadDocXML(filename);
                                results.IncludedDocs.Add(doc);

                                /*// Create an instance of the XmlSerializer class;
                                 * // specify the type of object to be deserialized.
                                 * XmlSerializer serializerDoc = new XmlSerializer(typeof(DocXML));
                                 *
                                 * // A FileStream is needed to read the XML document.
                                 * string textDoc = File.ReadAllText(filename);
                                 *
                                 * //Use the Deserialize method to restore the object's state with
                                 * //data from the XML document.
                                 * var readerDoc = XDocument.Parse(textDoc).CreateReader();
                                 * var serialDoc = (DocXML)serializerDoc.Deserialize(readerDoc);
                                 * results.IncludedDocs.Add(serialDoc);*/
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Error: ", ex.Message);
                                Console.WriteLine("Unexpected file in set");
                            }
                        }
                    }

                    return(results);
                }
                else
                {
                    Console.WriteLine("Set file not valid: No index found");
                }
            }
            return(null);
        }