Ejemplo n.º 1
0
        public static List <LogInfo> Decompress(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_Decompress info = cmd.Info.Cast <CodeInfo_Decompress>();

            #region Event Handlers
            void ReportDecompressProgress(object sender, ProgressEventArgs e)
            {
                s.MainViewModel.BuildCommandProgressValue = e.PercentDone;
                s.MainViewModel.BuildCommandProgressText  = $"Decompressing... ({e.PercentDone}%)";
            }

            #endregion

            string srcArchive = StringEscaper.Preprocess(s, info.SrcArchive);
            string destDir    = StringEscaper.Preprocess(s, info.DestDir);

            // Path Security Check
            if (!StringEscaper.PathSecurityCheck(destDir, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            // Does SrcArchive exist?
            if (!File.Exists(srcArchive))
            {
                return(LogInfo.LogErrorMessage(logs, $"Cannot find [{srcArchive}]"));
            }

            // Check if file or directory exist under name of destDir
            if (!Directory.Exists(destDir))
            {
                if (File.Exists(destDir))
                {
                    return(LogInfo.LogErrorMessage(logs, $"[{destDir}] should be a directory, not a file"));
                }

                Directory.CreateDirectory(destDir);
            }

            using (SevenZipExtractor extractor = new SevenZipExtractor(srcArchive))
            {
                extractor.Extracting += ReportDecompressProgress;
                s.MainViewModel.SetBuildCommandProgress("Decompress Progress");
                try
                {
                    extractor.ExtractArchive(destDir);
                }
                finally
                {
                    extractor.Extracting -= ReportDecompressProgress;
                    s.MainViewModel.ResetBuildCommandProgress();
                }
            }

            logs.Add(new LogInfo(LogState.Success, $"[{srcArchive}] decompressed to [{destDir}]"));
            return(logs);
        }
Ejemplo n.º 2
0
        public static List <LogInfo> Decompress(EngineState s, CodeCommand cmd)
        { // Decompress,<SrcArchive>,<DestDir>,[UTF8|UTF16|UTF16BE|ANSI]
            List <LogInfo> logs = new List <LogInfo>();

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Decompress));
            CodeInfo_Decompress info = cmd.Info as CodeInfo_Decompress;

            string srcArchive = StringEscaper.Preprocess(s, info.SrcArchive);
            string destDir    = StringEscaper.Preprocess(s, info.DestDir);

            // Path Security Check
            if (StringEscaper.PathSecurityCheck(destDir, out string errorMsg) == false)
            {
                logs.Add(new LogInfo(LogState.Error, errorMsg));
                return(logs);
            }

            if (!File.Exists(srcArchive))
            {
                logs.Add(new LogInfo(LogState.Error, $"Cannot find [{srcArchive}]"));
                return(logs);
            }

            if (!Directory.Exists(destDir))
            {
                if (File.Exists(destDir))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{destDir}] should be a directory, not a file"));
                    return(logs);
                }
                Directory.CreateDirectory(destDir);
            }

            if (info.Encoding == null)
            {
                ArchiveHelper.DecompressNative(srcArchive, destDir, true);
            }
            else
            {
                ArchiveHelper.DecompressManaged(srcArchive, destDir, true, info.Encoding); // Can handle null value of Encoding
            }
            logs.Add(new LogInfo(LogState.Success, $"[{srcArchive}] compressed to [{destDir}]"));

            return(logs);
        }