Пример #1
0
        public void CabInfoCompressExtract()
        {
            int fileCount = 10, fileSize = 1024;
            string dirA = String.Format("{0}-{1}-A", fileCount, fileSize);
            if (Directory.Exists(dirA)) Directory.Delete(dirA, true);
            Directory.CreateDirectory(dirA);
            Directory.CreateDirectory(Path.Combine(dirA, "sub"));
            string dirB = String.Format("{0}-{1}-B", fileCount, fileSize);
            if (Directory.Exists(dirB)) Directory.Delete(dirB, true);
            Directory.CreateDirectory(dirB);

            string[] files = new string[fileCount];
            for (int iFile = 0; iFile < fileCount; iFile++)
            {
                files[iFile] = "€" + iFile + ".txt";
                CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[iFile]), iFile, fileSize);
            }
            CompressionTestUtil.GenerateRandomFile(Path.Combine(Path.Combine(dirA, "sub"), "€-.txt"), fileCount + 1, fileSize);

            CabInfo cabInfo = new CabInfo("test.cab");
            cabInfo.Pack(dirA);
            cabInfo.Unpack(dirB);
            bool directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB);
            Assert.IsFalse(directoryMatch,
                "Testing whether cabinet output directory matches input directory.");
            Directory.Delete(dirB, true);
            Directory.CreateDirectory(dirB);
            cabInfo.Pack(dirA, true, CompressionLevel.Normal, null);
            cabInfo.Unpack(dirB);
            directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB);
            Assert.IsTrue(directoryMatch,
                "Testing whether cabinet output directory matches input directory.");
            Directory.Delete(dirB, true);
            Directory.Delete(Path.Combine(dirA, "sub"), true);
            Directory.CreateDirectory(dirB);
            cabInfo.Delete();

            cabInfo.PackFiles(dirA, files, null);
            cabInfo.UnpackFiles(files, dirB, null);
            directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB);
            Assert.IsTrue(directoryMatch,
                "Testing whether cabinet output directory matches input directory.");
            Directory.Delete(dirB, true);
            Directory.CreateDirectory(dirB);
            cabInfo.Delete();

            IDictionary<string, string> testMap = new Dictionary<string, string>(files.Length);
            for (int iFile = 0; iFile < fileCount; iFile++)
            {
                testMap[files[iFile] + ".key"] = files[iFile];
            }
            cabInfo.PackFileSet(dirA, testMap);
            cabInfo.UnpackFileSet(testMap, dirB);
            directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB);
            Assert.IsTrue(directoryMatch,
                "Testing whether cabinet output directory matches input directory.");
            Directory.Delete(dirB, true);
            Directory.CreateDirectory(dirB);

            testMap.Remove(files[1] + ".key");
            cabInfo.UnpackFileSet(testMap, dirB);
            directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB);
            Assert.IsFalse(directoryMatch,
                "Testing whether cabinet output directory matches input directory.");
            Directory.Delete(dirB, true);
            Directory.CreateDirectory(dirB);
            cabInfo.Delete();

            cabInfo.PackFiles(dirA, files, null);
            cabInfo.UnpackFile("€2.txt", Path.Combine(dirB, "test.txt"));
            Assert.IsTrue(File.Exists(Path.Combine(dirB, "test.txt")));
            Assert.AreEqual<int>(1, Directory.GetFiles(dirB).Length);
        }
Пример #2
0
    private void UpdateFilesOnOneMediaDisk(string mediaCab,
        InstallPathMap compressedFileMap, InstallPathMap uncompressedFileMap)
    {
        if(compressedFileMap.Count > 0)
        {
            string cabFile = null;
            bool cabFileIsTemp = false;
            if(mediaCab.StartsWith("#", StringComparison.Ordinal))
            {
                cabFileIsTemp = true;
                mediaCab = mediaCab.Substring(1);

                using(View streamView = this.OpenView("SELECT `Name`, `Data` FROM `_Streams` " +
                      "WHERE `Name` = '{0}'", mediaCab))
                {
                    streamView.Execute();
                    Record streamRec = streamView.Fetch();
                    if(streamRec == null)
                    {
                        this.LogMessage("Stream not found: {0}", mediaCab);
                        throw new InstallerException("Stream not found: " + mediaCab);
                    }
                    using(streamRec)
                    {
                        this.LogMessage("extract cab {0}", mediaCab);
                        Directory.CreateDirectory(this.TempDirectory);
                        cabFile = Path.Combine(this.TempDirectory,
                            Path.GetFileNameWithoutExtension(mediaCab) + ".cab");
                        streamRec.GetStream("Data", cabFile);
                    }
                }
            }
            else
            {
                cabFile = Path.Combine(this.SourceDirectory, mediaCab);
            }

            CabInfo cab = new CabInfo(cabFile);
            ArrayList fileKeyList = new ArrayList();
            foreach (CabFileInfo fileInCab in cab.GetFiles())
            {
                string fileKey = fileInCab.Name;
                if(this.Files[fileKey] != null)
                {
                    fileKeyList.Add(fileKey);
                }
            }
            string[] fileKeys = (string[]) fileKeyList.ToArray(typeof(string));

            Directory.CreateDirectory(this.TempDirectory);

            ArrayList remainingFileKeys = new ArrayList(fileKeys);
            foreach(string fileKey in fileKeys)
            {
                InstallPath fileInstallPath = compressedFileMap[fileKey];
                if(fileInstallPath != null)
                {
                    UpdateFileStats(fileKey, fileInstallPath);

                    string filePath = Path.Combine(this.WorkingDirectory, fileInstallPath.SourcePath);
                    this.LogMessage("copy {0} {1}", filePath, fileKey);
                    File.Copy(filePath, Path.Combine(this.TempDirectory, fileKey), true);
                    remainingFileKeys.Remove(fileKey);
                }
            }

            if(remainingFileKeys.Count > 0)
            {
                this.cabName = mediaCab;
                this.cabMsg = "extract {0}\\{1}";
                string[] remainingFileKeysArray = (string[]) remainingFileKeys.ToArray(typeof(string));
                cab.UnpackFiles(remainingFileKeysArray, this.TempDirectory, remainingFileKeysArray,
                    this.CabinetProgress);
            }

            ClearReadOnlyAttribute(this.TempDirectory, fileKeys);

            if(!cabFileIsTemp)
            {
                cab = new CabInfo(Path.Combine(this.WorkingDirectory, mediaCab));
            }
            this.cabName = mediaCab;
            this.cabMsg = "compress {0}\\{1}";
            cab.PackFiles(this.TempDirectory, fileKeys, fileKeys,
                this.CompressionLevel, this.CabinetProgress);

            if(cabFileIsTemp)
            {
                Record streamRec = new Record(1);
                streamRec.SetStream(1, cabFile);
                this.Execute(String.Format(
                    "UPDATE `_Streams` SET `Data` = ? WHERE `Name` = '{0}'", mediaCab),
                    streamRec);
            }
        }

        foreach (KeyValuePair<string, InstallPath> entry in uncompressedFileMap)
        {
            UpdateFileStats((string) entry.Key, (InstallPath) entry.Value);
        }
    }