Beispiel #1
0
        private MsiDataContainer(LessIO.Path msiDatabaseFilePath)
        {
            using var db = new Database(msiDatabaseFilePath.PathString, DatabaseOpenMode.ReadOnly);
            this.files   = MsiFile.CreateMsiFilesFromMSI(db);

            this.tables = db.Tables.OrderBy(tab => tab.Name).Select(table => TableWithData.Create(db, table.Name)).ToArray();
        }
Beispiel #2
0
        private static MsiFile[] GetMsiFileFromFileNames(Path msi, string[] fileNamesToExtract)
        {
            var msiFiles = MsiFile.CreateMsiFilesFromMSI(msi);

            Array.Sort(msiFiles, (f1, f2) => string.Compare(f1.LongFileName, f2.LongFileName, StringComparison.InvariantCulture));

            var fileNamesToExtractAsMsiFiles = new List <MsiFile>();

            foreach (var fileName in fileNamesToExtract)
            {
                var found = Array.BinarySearch(msiFiles, fileName, FileNameComparer.Default);
                if (found >= 0)
                {
                    fileNamesToExtractAsMsiFiles.Add(msiFiles[found]);
                }
                else
                {
                    Console.WriteLine("File {0} was not found in the msi.", fileName);
                }
            }
            return(fileNamesToExtractAsMsiFiles.ToArray());
        }
Beispiel #3
0
        /// <summary>
        /// Extracts the compressed files from the specified MSI file to the specified output directory.
        /// If specified, the list of <paramref name="filesToExtract"/> objects are the only files extracted.
        /// </summary>
        /// <param name="msi">The MSI file to extract files from.</param>
        /// <param name="outputDir">The directory to extract the files into.</param>
        /// <param name="filesToExtract">The files to extract or null or empty to extract all files.</param>
        /// <param name="progressCallback">Will be called during during the operation with progress information, and upon completion. The argument will be of type <see cref="ExtractionProgress"/>.</param>
        public static void ExtractFiles(Path msi, string outputDir, MsiFile[] filesToExtract, AsyncCallback progressCallback)
        {
            if (msi.IsEmpty)
            {
                throw new ArgumentNullException("msi");
            }
            if (string.IsNullOrEmpty(outputDir))
            {
                throw new ArgumentNullException("outputDir");
            }

            int filesExtractedSoFar = 0;
            //Refrence on Embedding files: https://msdn.microsoft.com/en-us/library/aa369279.aspx
            ExtractionProgress progress = null;
            Database           msidb    = new Database(msi.PathString, DatabaseOpenMode.ReadOnly);

            try
            {
                if (filesToExtract == null || filesToExtract.Length < 1)
                {
                    filesToExtract = MsiFile.CreateMsiFilesFromMSI(msidb);
                }

                progress = new ExtractionProgress(progressCallback, filesToExtract.Length);

                if (!FileSystem.Exists(msi))
                {
                    Trace.WriteLine("File \'" + msi + "\' not found.");
                    progress.ReportProgress(ExtractionActivity.Complete, string.Empty, filesExtractedSoFar);
                    return;
                }

                progress.ReportProgress(ExtractionActivity.Initializing, string.Empty, filesExtractedSoFar);
                FileSystem.CreateDirectory(new Path(outputDir));

                //map short file names to the msi file entry
                var fileEntryMap = new Dictionary <string, MsiFile>(filesToExtract.Length, StringComparer.InvariantCulture);
                foreach (MsiFile fileEntry in filesToExtract)
                {
                    MsiFile existingFile = null;
                    if (fileEntryMap.TryGetValue(fileEntry.File, out existingFile))
                    {
                        //NOTE: This used to be triggered when we ignored case of file, but now we don't ignore case so this is unlikely to occur.
                        // Differing only by case is not compliant with the msi specification but some installers do it (e.g. python, see issue 28).
                        Debug.Print("!!Found duplicate file using key {0}. The existing key was {1}", fileEntry.File, existingFile.File);
                    }
                    else
                    {
                        fileEntryMap.Add(fileEntry.File, fileEntry);
                    }
                }

                Debug.Assert(fileEntryMap.Count == filesToExtract.Length, "Duplicate files must have caused some files to not be in the map.");

                progress.ReportProgress(ExtractionActivity.Uncompressing, string.Empty, filesExtractedSoFar);
                List <CabInfo> cabInfos = CabsFromMsiToDisk(msi, msidb, outputDir);

                try
                {
                    foreach (CabInfo cab in cabInfos)
                    {
                        foreach (CabFileInfo cabFile in cab.GetFiles())
                        {
                            var sourcePath = Path.Combine(cabFile.Path, cabFile.Name);
                            if (fileEntryMap.TryGetValue(sourcePath.PathString, out MsiFile msiFile))
                            {
                                Path destPath         = Path.Combine(GetTargetDirectory(outputDir, msiFile.Directory.FullPath), msiFile.LongFileName);
                                Path relativeDestPath = Path.Combine(msiFile.Directory.FullPath, msiFile.LongFileName);
                                progress.ReportProgress(ExtractionActivity.ExtractingFile, relativeDestPath.PathString, ++filesExtractedSoFar);
                                cab.UnpackFile(sourcePath.PathString, destPath.PathString);
                            }
                        }
                    }
                }
                finally
                {
                    foreach (CabInfo cab in cabInfos)
                    {
                        DeleteFileForcefully(new Path(cab.FullName));
                    }
                }
            }
            finally
            {
                if (msidb != null)
                {
                    msidb.Close();
                }
                if (progress != null)
                {
                    progress.ReportProgress(ExtractionActivity.Complete, "", filesExtractedSoFar);
                }
            }
        }