Ejemplo n.º 1
0
        public void Unpack(string filePath, string directoryPath)
        {
            InfoService.InitializeProgressMessageThread();
            if (String.IsNullOrEmpty(directoryPath))
            {
                directoryPath = Directory.GetCurrentDirectory();
            }
            Folder     folder     = null;
            FileStream fileStream = new FileStream(filePath, FileMode.Open);

            try
            {
                InfoService.ShowStatusMessage("Reading...");
                BinaryFormatter formatter = new BinaryFormatter();
                folder = (Folder)formatter.Deserialize(fileStream);
            }
            catch (SerializationException e)
            {
                InfoService.ShowErrorMessage("Failed to unpack. Reason: " + e.Message);
            }
            catch (FileNotFoundException e)
            {
                InfoService.ShowErrorMessage(e.Message);
            }
            finally
            {
                fileStream.Close();
            }
            InfoService.ShowStatusMessage("Unpacking...");
            CreateFoldersAndFilesFromInstance(folder, directoryPath);
            InfoService.ProcessFinished      = true;
            EstimatingService.ProcessedBytes = EstimatingService.NumberOfBytesToProcess;
            InfoService.ShowStatusMessage("Done.");
        }
Ejemplo n.º 2
0
        private List <File> GetFiles(string folderPath)
        {
            string[] filesInDirectory = Directory.GetFiles(folderPath);
            if (filesInDirectory.Length == 0)
            {
                return(null);
            }
            List <File> files = new List <File>();

            try
            {
                var exceptions = new ConcurrentQueue <Exception>();
                Parallel.ForEach(
                    filesInDirectory,
                    new ParallelOptions {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                },
                    (filePath) =>
                {
                    try
                    {
                        InfoService.FileInProcess = filePath;
                        lock (files)
                        {
                            files.Add(new File()
                            {
                                Name    = filePath.Split(Path.DirectorySeparatorChar).Last(),
                                Content = System.IO.File.ReadAllBytes(filePath)
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        exceptions.Enqueue(e);
                    }
                    finally
                    {
                        EstimatingService.ProcessedBytes += new FileInfo(filePath).Length;
                    }
                });
                if (exceptions.Count != 0)
                {
                    throw new AggregateException(exceptions);
                }
            }
            catch (AggregateException e)
            {
                foreach (var exception in e.Flatten().InnerExceptions)
                {
                    InfoService.ShowErrorMessage(exception.Message);
                }
            }
            return(files);
        }
Ejemplo n.º 3
0
        public void Save(string folderPath, string fileName)
        {
            InfoService.InitializeProgressMessageThread();
            folderPath = Path.GetFullPath(folderPath);
            string rootFolderName = folderPath.Split(Path.DirectorySeparatorChar).Last();

            if (String.IsNullOrEmpty(fileName))
            {
                fileName = rootFolderName;
            }
            Folder          folder;
            FileStream      fileStream = new FileStream(fileName + "." + ConfigurationManager.AppSettings["SavedFileFormat"], FileMode.Create);
            BinaryFormatter formatter  = new BinaryFormatter();

            try
            {
                InfoService.ShowStatusMessage("Reading...");
                folder = new Folder
                {
                    Name       = rootFolderName,
                    SubFolders = GetSubFolders(folderPath),
                    Files      = GetFiles(folderPath)
                };
                InfoService.ProcessFinished = true;
                InfoService.ShowStatusMessage("Saving...");
                formatter.Serialize(fileStream, folder);
            }
            catch (DirectoryNotFoundException e)
            {
                InfoService.ShowErrorMessage(e.Message);
            }
            catch (SerializationException e)
            {
                InfoService.ShowErrorMessage("Failed to save. Reason: " + e.Message);
            }
            finally
            {
                fileStream.Close();
                InfoService.ShowStatusMessage("Done.");
            }
        }