예제 #1
0
        public static JsonData Decompress
            (string preDefinedRootRelativePath,
            ViewModels.PathAndDirectoriesAndFilesViewModel viewModel)
        {
            JsonData oJsonData = null;

            viewModel.Path =
                FixPath(viewModel.Path);

            string strPreDefinedRootRelativePath =
                FixPreDefinedRootRelativePath(preDefinedRootRelativePath);

            string strRootRelativePath =
                string.Format("{0}{1}",
                              strPreDefinedRootRelativePath, viewModel.Path);

            string strPath =
                System.Web.HttpContext.Current.Server.MapPath(strRootRelativePath);

            Ionic.Zip.ZipFile oZipFile = null;

            try
            {
                foreach (ViewModels.FileViewModel currentFileViewModel in viewModel.Files)
                {
                    string strCurrentPathName =
                        string.Format("{0}\\{1}",
                                      strPath, currentFileViewModel.Name);

                    if (System.IO.File.Exists(strCurrentPathName) == false)
                    {
                        continue;
                    }

                    oZipFile =
                        new Ionic.Zip.ZipFile(strCurrentPathName);

                    InitializeZipFile(oZipFile);

                    try
                    {
                        oZipFile.ExtractAll
                            (strPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
                    }
                    catch
                    {
                    }
                }

                oJsonData =
                    GetDirectoriesAndFiles(preDefinedRootRelativePath, viewModel.Path);

                return(oJsonData);
            }
            catch
            {
                string strErrorMessage =
                    "Unexpected Error on Decompressing File(s)!";

                oJsonData =
                    new JsonData
                {
                    MessageText = strErrorMessage,
                    State       = ViewModels.JsonResultStates.Error,
                };

                return(oJsonData);
            }
            finally
            {
                if (oZipFile != null)
                {
                    oZipFile.Dispose();
                    oZipFile = null;
                }
            }
        }
예제 #2
0
        public static JsonData DeleteDirectoriesAndFiles
            (string preDefinedRootRelativePath, ViewModels.PathAndDirectoriesAndFilesViewModel viewModel)
        {
            JsonData oJsonData = null;

            viewModel.Path = FixPath(viewModel.Path);

            preDefinedRootRelativePath =
                FixPreDefinedRootRelativePath(preDefinedRootRelativePath);

            string strRootRelativePath =
                string.Format("{0}{1}",
                              preDefinedRootRelativePath, viewModel.Path);

            string strPath =
                System.Web.HttpContext.Current.Server.MapPath(strRootRelativePath);

            if (System.IO.Directory.Exists(strPath) == false)
            {
                oJsonData =
                    new JsonData
                {
                    State       = ViewModels.JsonResultStates.Error,
                    MessageText = "The current path is not valid!",
                };

                return(oJsonData);
            }

            ViewModels.DirectoriesAndFilesViewModel
                oReturnViewModel = new ViewModels.DirectoriesAndFilesViewModel();

            bool blnHasError = false;

            foreach (ViewModels.FileViewModel oFile in viewModel.Files)
            {
                string strCurrentPathName =
                    string.Format("{0}{1}", strPath, oFile.Name);

                if (System.IO.File.Exists(strCurrentPathName))
                {
                    try
                    {
                        System.IO.File.Delete(strCurrentPathName);
                    }
                    catch (System.Exception ex)
                    {
                        blnHasError = true;

                        System.IO.FileInfo oFileInfoThatHasError =
                            new System.IO.FileInfo(strCurrentPathName);

                        ViewModels.FileViewModel oFileThatHasError =
                            new ViewModels.FileViewModel(oFileInfoThatHasError);

                        if (ex.Message.Contains("it is being used by another process"))
                        {
                            oFileThatHasError.Message =
                                "It is being used by another process.";
                        }
                        else
                        {
                            oFileThatHasError.Message = ex.Message;
                        }

                        oReturnViewModel.Files.Add(oFileThatHasError);
                    }
                }
            }

            foreach (ViewModels.DirectoryViewModel oDirectory in viewModel.Directories)
            {
                string strCurrentPath =
                    string.Format("{0}{1}", strPath, oDirectory.Name);

                if (System.IO.Directory.Exists(strCurrentPath))
                {
                    try
                    {
                        System.IO.Directory.Delete(strCurrentPath, recursive: true);
                    }
                    catch (System.Exception ex)
                    {
                        blnHasError = true;

                        System.IO.DirectoryInfo oDirectoryInfoThatHasError =
                            new System.IO.DirectoryInfo(strCurrentPath);

                        ViewModels.DirectoryViewModel oDirectoryThatHasError =
                            new ViewModels.DirectoryViewModel(oDirectoryInfoThatHasError);

                        if (ex.Message.Contains("it is being used by another process"))
                        {
                            oDirectoryThatHasError.Message =
                                "It is being used by another process.";
                        }
                        else
                        {
                            oDirectoryThatHasError.Message = ex.Message;
                        }

                        oReturnViewModel.Directories.Add(oDirectoryThatHasError);
                    }
                }
            }

            if (blnHasError)
            {
                string strErrorMessage =
                    "Some selected directories and/or files does not deleted!";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    Data           = oReturnViewModel,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };
            }
            else
            {
                string strInformationMessage =
                    "All selected directories and/or files deleted successfully.";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    Data           = oReturnViewModel,
                    MessageText    = strInformationMessage,
                    State          = ViewModels.JsonResultStates.Success,
                };
            }

            return(oJsonData);
        }