Example #1
0
        public void CabLib_CabExtract_4()
        {
            string compPath  = Path.Combine(TestHelper.BaseDir, "ex4.cab");
            string decompDir = Path.Combine(TestHelper.DestDir, "ex4");

            using (FileStream fs = new FileStream(compPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (CabExtract cab = new CabExtract(fs))
                {
                    Assert.IsTrue(cab.ExtractAll(decompDir, out List <string> fileList));
                    Assert.IsTrue(fileList.Count == 3);
                    Assert.IsTrue(fileList.Contains("ex1.jpg"));
                    Assert.IsTrue(fileList.Contains("ex2.jpg"));
                    Assert.IsTrue(fileList.Contains("ex3.jpg"));
                }

            // Compare SHA256 Digest
            byte[] originDigest1 = TestHelper.SHA256Digest(Path.Combine(TestHelper.BaseDir, "ex1.jpg"));
            byte[] decompDigest1 = TestHelper.SHA256Digest(Path.Combine(decompDir, "ex1.jpg"));
            Assert.IsTrue(decompDigest1.SequenceEqual(originDigest1));

            byte[] originDigest2 = TestHelper.SHA256Digest(Path.Combine(TestHelper.BaseDir, "ex2.jpg"));
            byte[] decompDigest2 = TestHelper.SHA256Digest(Path.Combine(decompDir, "ex2.jpg"));
            Assert.IsTrue(decompDigest2.SequenceEqual(originDigest2));

            byte[] originDigest3 = TestHelper.SHA256Digest(Path.Combine(TestHelper.BaseDir, "ex3.jpg"));
            byte[] decompDigest3 = TestHelper.SHA256Digest(Path.Combine(decompDir, "ex3.jpg"));
            Assert.IsTrue(decompDigest3.SequenceEqual(originDigest3));
        }
Example #2
0
        public void CabLib_CabExtract_3()
        {
            string originPath = Path.Combine(TestHelper.BaseDir, "ex3.jpg");
            string compPath   = Path.Combine(TestHelper.BaseDir, "ex3.cab");
            string decompDir  = Path.Combine(TestHelper.DestDir, "ex3");

            using (FileStream fs = new FileStream(compPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (CabExtract cab = new CabExtract(fs))
                {
                    Assert.IsFalse(cab.ExtractSingleFile("ex2.jpg", decompDir)); // ex2.jpg does not exist in ex3.cab
                    Assert.IsTrue(cab.ExtractAll(decompDir, out List <string> fileList));
                    Assert.IsTrue(fileList.Count == 1);
                    Assert.IsTrue(fileList[0].Equals("ex3.jpg"));
                }

            // Compare SHA256 Digest
            byte[] originDigest = TestHelper.SHA256Digest(originPath);
            byte[] decompDigest = TestHelper.SHA256Digest(Path.Combine(decompDir, "ex3.jpg"));
            Assert.IsTrue(decompDigest.SequenceEqual(originDigest));
        }
Example #3
0
        private static void InternalCopyOrExpand(EngineState s, List <LogInfo> logs, CodeInfo_CopyOrExpand info, string srcFile, string destPath)
        {
            string srcFileName = Path.GetFileName(srcFile);
            bool   destIsDir   = Directory.Exists(destPath);
            bool   destIsFile  = File.Exists(destPath);

            if (!destIsDir)
            {
                if (destIsFile)
                {
                    if (info.Preserve)
                    {
                        logs.Add(new LogInfo(info.NoWarn ? LogState.Ignore : LogState.Warning, $"Cannot overwrite [{destPath}]"));
                        return;
                    }
                    else
                    {
                        logs.Add(new LogInfo(info.NoWarn ? LogState.Ignore : LogState.Warning, $"[{destPath}] will be overwritten"));
                    }
                }
            }

            if (File.Exists(srcFile))
            { // SrcFile is uncompressed, just copy!
                string destFullPath = destPath;
                if (destIsDir)
                {
                    destFullPath = Path.Combine(destPath, srcFileName);
                }
                else if (!destIsFile)
                {
                    Directory.CreateDirectory(FileHelper.GetDirNameEx(destPath));
                }

                File.Copy(srcFile, destFullPath, !info.Preserve);
                logs.Add(new LogInfo(LogState.Success, $"[{srcFile}] copied to [{destPath}]"));
            }
            else
            { // Extract Cabinet from _ (Ex) EXPLORER.EX_ -> EXPLORER.EXE
                string destDir;
                if (destIsDir)
                {
                    destDir = destPath;
                }
                else
                {
                    destDir = Path.GetDirectoryName(destPath);
                }

                string srcCab = srcFile.Substring(0, srcFile.Length - 1) + "_";
                if (File.Exists(srcCab))
                {
                    // Get Temp Dir
                    string tempDir;
                    {
                        string tempDirName = Path.GetTempFileName();
                        File.Delete(tempDirName);
                        tempDir = Path.Combine(Path.GetTempPath(), tempDirName);
                    }
                    Directory.CreateDirectory(tempDir);

                    try
                    {
                        bool result;
                        using (FileStream fs = new FileStream(srcCab, FileMode.Open, FileAccess.Read, FileShare.Read))
                            using (CabExtract cab = new CabExtract(fs))
                            {
                                result = cab.ExtractAll(tempDir, out List <string> fileList);
                                if (2 < fileList.Count)
                                { // WB082 behavior : Expand/CopyOrExpand only supports single-file cabinet
                                    logs.Add(new LogInfo(LogState.Error, $"Cabinet [{srcFileName}] should contain single file"));
                                    return;
                                }
                            }

                        if (result)
                        { // Extract Success
                            string tempFullPath = Path.Combine(tempDir, srcFileName);
                            if (File.Exists(tempFullPath))
                            {
                                string destFullPath;
                                if (destIsDir)
                                {
                                    destFullPath = Path.Combine(destDir, srcFileName);
                                }
                                else // Move to new name
                                {
                                    destFullPath = Path.Combine(destDir, Path.GetFileName(destPath));
                                }

                                if (File.Exists(destFullPath))
                                {
                                    logs.Add(new LogInfo(LogState.Warning, $"File [{destFullPath}] already exists, will be overwritten"));
                                }

                                try
                                {
                                    if (!Directory.Exists(Path.GetDirectoryName(destFullPath)))
                                    {
                                        Directory.CreateDirectory(Path.GetDirectoryName(destFullPath));
                                    }
                                    File.Copy(tempFullPath, destFullPath, true);
                                    logs.Add(new LogInfo(LogState.Success, $"[{srcFileName}] from [{srcCab}] extracted to [{destFullPath}]"));
                                }
                                finally
                                {
                                    File.Delete(tempFullPath);
                                }
                            }
                            else
                            { // Unable to find srcFile
                                logs.Add(new LogInfo(LogState.Error, $"Cabinet [{srcFileName}] does not contains [{Path.GetFileName(destPath)}]"));
                            }
                        }
                        else
                        { // Extract Fail
                            logs.Add(new LogInfo(LogState.Error, $"Failed to extract [{srcCab}]"));
                        }
                    }
                    finally
                    {
                        Directory.Delete(tempDir, true);
                    }
                }
                else
                { // Error
                    logs.Add(new LogInfo(LogState.Error, $"[{srcFile}] nor [{srcCab}] not found"));
                }
            }
        }