Пример #1
0
        public void CabInfoSerialization()
        {
            CabInfo cabInfo = new CabInfo("testser.cab");
            int txtSize = 10240;
            CompressionTestUtil.GenerateRandomFile("testser00.txt", 0, txtSize);
            CompressionTestUtil.GenerateRandomFile("testser01.txt", 1, txtSize);
            cabInfo.PackFiles(null, new string[] { "testser00.txt", "testser01.txt" }, null);
            ArchiveFileInfo cfi = cabInfo.GetFiles()[1];

            MemoryStream memStream = new MemoryStream();

            BinaryFormatter formatter = new BinaryFormatter();

            memStream.Seek(0, SeekOrigin.Begin);
            formatter.Serialize(memStream, cabInfo);
            memStream.Seek(0, SeekOrigin.Begin);
            CabInfo cabInfo2 = (CabInfo) formatter.Deserialize(memStream);
            Assert.AreEqual<string>(cabInfo.FullName, cabInfo2.FullName);

            memStream.Seek(0, SeekOrigin.Begin);
            formatter.Serialize(memStream, cfi);
            memStream.Seek(0, SeekOrigin.Begin);
            CabFileInfo cfi2 = (CabFileInfo) formatter.Deserialize(memStream);
            Assert.AreEqual<string>(cfi.FullName, cfi2.FullName);
            Assert.AreEqual<long>(cfi.Length, cfi2.Length);

            CabException cabEx = new CabException();
            memStream.Seek(0, SeekOrigin.Begin);
            formatter.Serialize(memStream, cabEx);
            memStream.Seek(0, SeekOrigin.Begin);
            formatter.Deserialize(memStream);

            cabEx = new CabException("Test exception.", null);
            Assert.AreEqual<string>("Test exception.", cabEx.Message);
        }
Пример #2
0
		public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory)
		{
			bool difference = false;
			IComparer caseInsComp = CaseInsensitiveComparer.Default;

			// TODO: Make this faster by extracting the whole cab at once.
			// TODO: Optimize for the match case by first comparing the whole cab files.

            CabInfo cab1 = new CabInfo(diffInput1);
            CabInfo cab2 = new CabInfo(diffInput2);
			IList<CabFileInfo> cabFilesList1 = cab1.GetFiles();
            IList<CabFileInfo> cabFilesList2 = cab2.GetFiles();
            CabFileInfo[] cabFiles1 = new CabFileInfo[cabFilesList1.Count];
            CabFileInfo[] cabFiles2 = new CabFileInfo[cabFilesList2.Count];
            cabFilesList1.CopyTo(cabFiles1, 0);
            cabFilesList2.CopyTo(cabFiles2, 0);
			string[] files1 = new string[cabFiles1.Length];
			string[] files2 = new string[cabFiles2.Length];
			for(int i1 = 0; i1 < cabFiles1.Length; i1++) files1[i1] = cabFiles1[i1].Name;
			for(int i2 = 0; i2 < cabFiles2.Length; i2++) files2[i2] = cabFiles2[i2].Name;
			Array.Sort(files1, cabFiles1, caseInsComp);
			Array.Sort(files2, cabFiles2, caseInsComp);


			for(int i1 = 0, i2 = 0; i1 < files1.Length || i2 < files2.Length; )
			{
				int comp;
				if(i1 == files1.Length)
				{
					comp = 1;
				}
				else if(i2 == files2.Length)
				{
					comp = -1;
				}
				else
				{
					comp = caseInsComp.Compare(files1[i1], files2[i2]);
				}
				if(comp < 0)
				{
					diffOutput.WriteLine("{0}< {1}", linePrefix, files1[i1]);
					i1++;
					difference = true;
				}
				else if(comp > 0)
				{
					diffOutput.WriteLine("{0}> {1}", linePrefix, files2[i2]);
					i2++;
					difference = true;
				}
				else
				{
					string tempFile1 = Path.GetTempFileName();
					string tempFile2 = Path.GetTempFileName();
					cabFiles1[i1].CopyTo(tempFile1, true);
					cabFiles2[i2].CopyTo(tempFile2, true);
					IDiffEngine diffEngine = diffFactory.GetDiffEngine(tempFile1, tempFile2, options);
					StringWriter sw = new StringWriter();
					if(diffEngine.GetDiff(tempFile1, tempFile2, options, sw, linePrefix + "    ", diffFactory))
					{
						diffOutput.WriteLine("{0}{1}", linePrefix, files1[i1]);
						diffOutput.Write(sw.ToString());
						difference = true;
					}

					File.SetAttributes(tempFile1, File.GetAttributes(tempFile1) & ~FileAttributes.ReadOnly);
					File.SetAttributes(tempFile2, File.GetAttributes(tempFile2) & ~FileAttributes.ReadOnly);
					try
					{
						File.Delete(tempFile1);
						File.Delete(tempFile2);
					}
					catch(IOException)
					{
#if DEBUG
						Console.WriteLine("Could not delete temporary files {0} and {1}", tempFile1, tempFile2);
#endif
					}
					i1++;
					i2++;
				}
			}

			return difference;
		}
Пример #3
0
        public void CabInfoGetFiles()
        {
            IList<CabFileInfo> fileInfo;
            CabInfo cabInfo = new CabInfo("test.cab");
            int txtSize = 10240;
            CompressionTestUtil.GenerateRandomFile("testinfo0.txt", 0, txtSize);
            CompressionTestUtil.GenerateRandomFile("testinfo1.txt", 1, txtSize);
            cabInfo.PackFiles(null, new string[] { "testinfo0.txt", "testinfo1.txt" }, null);

            fileInfo = cabInfo.GetFiles();
            Assert.IsNotNull(fileInfo);
            Assert.AreEqual<int>(2, fileInfo.Count);
            Assert.AreEqual<string>("testinfo0.txt", fileInfo[0].Name);
            Assert.AreEqual<string>("testinfo1.txt", fileInfo[1].Name);

            fileInfo = cabInfo.GetFiles("*.txt");
            Assert.IsNotNull(fileInfo);
            Assert.AreEqual<int>(2, fileInfo.Count);
            Assert.AreEqual<string>("testinfo0.txt", fileInfo[0].Name);
            Assert.AreEqual<string>("testinfo1.txt", fileInfo[1].Name);

            fileInfo = cabInfo.GetFiles("testinfo1.txt");
            Assert.IsNotNull(fileInfo);
            Assert.AreEqual<int>(1, fileInfo.Count);
            Assert.AreEqual<string>("testinfo1.txt", fileInfo[0].Name);
        }
Пример #4
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);
        }
    }