public static byte[] Unpack(byte[] fileBytes, string unpackedFileName) { MemoryStream packedStream = new MemoryStream(fileBytes); BasicUnpackStreamContext streamContext = new BasicUnpackStreamContext(packedStream); Predicate <string> isFileMatch = delegate(string match) { return(String.Compare(match, unpackedFileName, true) == 0); }; using (CabEngine engine = new CabEngine()) { engine.Unpack(streamContext, isFileMatch); } Stream unpackedStream = streamContext.FileStream; if (unpackedStream != null) { unpackedStream.Position = 0; byte[] unpackedBytes = new byte[unpackedStream.Length]; unpackedStream.Read(unpackedBytes, 0, unpackedBytes.Length); return(unpackedBytes); } else { string message = String.Format("Error: File does not contain the expected file ('{1}')", unpackedFileName); Console.WriteLine(message); Program.Exit(); return(new byte[0]); } }
/// <inheritdoc/> public override void Extract(IBuilder builder, Stream stream, string?subDir = null) { EnsureFile(stream, msiPath => { try { using var engine = new CabEngine(); using var package = new MsiPackage(msiPath); package.ForEachCabinet(cabStream => { Handler.CancellationToken.ThrowIfCancellationRequested(); EnsureSeekable(cabStream, seekableStream => { // ReSharper disable once AccessToDisposedClosure engine.Unpack( new CabExtractorContext(builder, seekableStream, x => NormalizePath(package.Files[x], subDir), Handler.CancellationToken), fileFilter: package.Files.ContainsKey); }); }); } #region Error handling catch (Exception ex) when(ex is InstallerException or CabException) { // Wrap exception since only certain exception types are allowed throw new IOException(Resources.ArchiveInvalid, ex); } #endregion });
public static byte[] Unpack(byte[] fileBytes, string unpackedFileName) { var packedStream = new MemoryStream(fileBytes); var streamContext = new BasicUnpackStreamContext(packedStream); Predicate <string> isFileMatch = match => String.Compare(match, unpackedFileName, StringComparison.OrdinalIgnoreCase) == 0; using (var engine = new CabEngine()) { engine.Unpack(streamContext, isFileMatch); } var unpackedStream = streamContext.FileStream; if (unpackedStream != null) { unpackedStream.Position = 0; var unpackedBytes = new byte[unpackedStream.Length]; unpackedStream.Read(unpackedBytes, 0, unpackedBytes.Length); return(unpackedBytes); } var message = string.Format("Error: File does not contain the expected file ('{1}')", unpackedFileName); Console.WriteLine(message); Program.Exit(); return(new byte[0]); }
private bool ProcessCatalog() { string tempFile = Path.Combine(Path.GetTempPath(), "HPClientDriverPackCatalog.cab"); using (FileStream innerCab = new FileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { CabEngine engine = new CabEngine(); foreach (ArchiveFileInfo archiveFileInfo in engine.GetFileInfo(innerCab)) { using (Stream stream = engine.Unpack(innerCab, archiveFileInfo.Name)) { XElement catalog = XElement.Load(stream); comboBoxOS.Items.Clear(); List <XElement> nodeList = catalog.Element("HPClientDriverPackCatalog").Element("OSList").Elements("OS").ToList(); foreach (XElement node in nodeList) { string os = node.Element("Name").Value; comboBoxOS.Items.Add(os); comboBoxOS.SelectedIndex = 0; } } } } return(true); }
private void ExtractCab(Stream stream) { using var tempFile = new TemporaryFile("0install"); // Extract embedded CAB from MSI using (var tempStream = File.Create(tempFile)) stream.CopyToEx(tempStream, cancellationToken: CancellationToken); // Extract individual files from CAB using (CabStream = File.OpenRead(tempFile)) CabEngine.Unpack(this, _ => true); }
public static Stream Unpack(Stream compressedStream) { CabEngine cabEngine = new CabEngine(); InMemoryUnpackContext unpackContext = new InMemoryUnpackContext(compressedStream); cabEngine.Unpack(unpackContext, suffix => true); Stream s = unpackContext.UncompressedStream; s.Seek(0, SeekOrigin.Begin); return(s); }
/// <inheritdoc/> protected override void ExtractArchive() { try { CabEngine.Unpack(this, _ => true); } #region Error handling catch (CabException ex) { // Wrap exception since only certain exception types are allowed throw new IOException(Resources.ArchiveInvalid, ex); } #endregion }
/// <inheritdoc/> public override void Extract(IBuilder builder, Stream stream, string?subDir = null) { using var engine = new CabEngine(); EnsureSeekable(stream, seekableStream => { try { engine.Unpack( new CabExtractorContext(builder, seekableStream, path => NormalizePath(path, subDir), Handler.CancellationToken), fileFilter: path => NormalizePath(path, subDir) != null); } #region Error handling catch (CabException ex) { // Wrap exception since only certain exception types are allowed throw new IOException(Resources.ArchiveInvalid, ex); } #endregion }); }
/// <inheritdoc/> protected override void Execute() { State = TaskState.Data; try { if (!Directory.Exists(EffectiveTargetDir)) { Directory.CreateDirectory(EffectiveTargetDir); } CabEngine.Unpack(this, _ => true); } #region Error handling catch (CabException ex) { // Wrap exception since only certain exception types are allowed throw new IOException(Resources.ArchiveInvalid, ex); } #endregion State = TaskState.Complete; }
private void ProcessCatalog() { dataGridViewDriverPackages.Rows.Clear(); bool known = false; string prefix = string.IsNullOrEmpty(registry.ReadString("HPFolderPrefix")) ? "HP" : registry.ReadString("HPFolderPrefix"); string tempFile = Path.Combine(Path.GetTempPath(), "HPClientDriverPackCatalog.cab"); using (FileStream innerCab = new FileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { CabEngine engine = new CabEngine(); foreach (ArchiveFileInfo archiveFileInfo in engine.GetFileInfo(innerCab)) { using (Stream stream = engine.Unpack(innerCab, archiveFileInfo.Name)) { catalog = XElement.Load(stream); IEnumerable <XElement> nodeList = catalog.Element("HPClientDriverPackCatalog").Element("ProductOSDriverPackList").Elements("ProductOSDriverPack").Where( x => x.Element("OSName").Value == UserData["OS"].ToString() ); foreach (XElement node in nodeList) { HPDriverPackage package = new HPDriverPackage(node) { SoftPaq = catalog.Element("HPClientDriverPackCatalog").Element("SoftPaqList").Elements("SoftPaq").Where( x => x.Element("Id").Value == node.Element("SoftPaqId").Value ).FirstOrDefault(), Vendor = prefix }; package.ProcessSoftPaq(); package.GenerateModelFolderName(os, structure); DataGridViewRow dataGridViewRow = new DataGridViewRow(); dataGridViewRow.CreateCells(dataGridViewDriverPackages); dataGridViewRow.Cells[0].Value = false; dataGridViewRow.Cells[1].Value = package.Model; dataGridViewRow.Cells[2].Value = package.VersionShort; dataGridViewRow.Cells[3].Value = package.Size.ToString(); dataGridViewRow.Tag = package; dataGridViewDriverPackages.Rows.Add(dataGridViewRow); } } } } // TODO: will implement when I have an HP device to test with //string query = "SELECT DISTINCT Model FROM SMS_G_System_COMPUTER_SYSTEM WHERE Manufacturer = 'HP' OR Manufacturer = 'Hewlett-Packard'"; //List<IResultObject> models = Utility.SearchWMIToList(ConnectionManager, query); //foreach (IResultObject model in models) //{ // string testModel = model["Model"].StringValue; // testModel = Regex.Replace(testModel, "HP", "", RegexOptions.IgnoreCase); // testModel = Regex.Replace(testModel, "COMPAQ", "", RegexOptions.IgnoreCase); // testModel = Regex.Replace(testModel, "SFF", "Small Form Factor"); // testModel = Regex.Replace(testModel, "USDT", "Desktop"); // testModel = Regex.Replace(testModel, " TWR", " Tower"); // testModel = testModel.TrimEnd("PC").Trim(); // DataGridViewRow row = dataGridViewDriverPackages.Rows // .Cast<DataGridViewRow>() // .Where(r => r.Cells[1].Value.ToString().Equals(testModel, StringComparison.CurrentCultureIgnoreCase)) // .FirstOrDefault(); // if (row != null) // { // row.Cells[0].Value = true; // row.Cells[4].Value = "Model detected"; // known = true; // } //} foreach (DataGridViewRow dataGridViewRow in dataGridViewDriverPackages.Rows) { HPDriverPackage package = (HPDriverPackage)dataGridViewRow.Tag; string path = Path.Combine(sourceFolderPath, package.FolderName); if (Directory.Exists(path)) { string[] fileList = Directory.GetFiles(path, "*.version"); if (File.Exists(Path.Combine(path, package.VersionFile))) { dataGridViewRow.Cells[0].Value = false; dataGridViewRow.Cells[4].Value = "Downloaded"; } else if (fileList.Length > 0) { dataGridViewRow.Cells[0].Value = false; dataGridViewRow.Cells[4].Value = "New version"; } } } dataGridViewDriverPackages.Sort(known ? columnStatus : columnPack, known ? ListSortDirection.Descending : ListSortDirection.Ascending); Initialized = true; }
private void ProcessCatalog() { dataGridViewDriverPackages.Rows.Clear(); bool known = false; string prefix = string.IsNullOrEmpty(registry.ReadString("DellFolderPrefix")) ? "Dell" : registry.ReadString("DellFolderPrefix"); string tempFile = Path.Combine(Path.GetTempPath(), "DriverPackCatalog.cab"); using (FileStream innerCab = new FileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { CabEngine engine = new CabEngine(); foreach (ArchiveFileInfo archiveFileInfo in engine.GetFileInfo(innerCab)) { using (Stream stream = engine.Unpack(innerCab, archiveFileInfo.Name)) { catalog = XElement.Load(stream); XNamespace ns = catalog.GetDefaultNamespace(); // Get download base location from DriverPackCatalog.xml UserData["baseLocation"] = catalog.Attribute("baseLocation").Value; IEnumerable <XElement> nodeList = catalog.Elements(ns + "DriverPackage").Where( x => x.Element(ns + "SupportedOperatingSystems").Elements(ns + "OperatingSystem").Any( y => y.Attribute("osCode").Value == UserData["OS"].ToString().Replace(" ", string.Empty) && y.Attribute("osArch").Value == UserData["Architecture"].ToString() ) ); foreach (XElement node in nodeList) { DellDriverPackage package = new DellDriverPackage(node) { Vendor = prefix }; package.GenerateModelFolderName(os, structure); DataGridViewRow dataGridViewRow = new DataGridViewRow(); dataGridViewRow.CreateCells(dataGridViewDriverPackages); dataGridViewRow.Cells[0].Value = false; dataGridViewRow.Cells[1].Value = package.Model; dataGridViewRow.Cells[2].Value = package.Version; dataGridViewRow.Cells[3].Value = package.Size.ToString(); dataGridViewRow.Tag = package; dataGridViewDriverPackages.Rows.Add(dataGridViewRow); } } } } string query = "SELECT DISTINCT Model FROM SMS_G_System_COMPUTER_SYSTEM WHERE Manufacturer = 'Dell Inc.'"; List <IResultObject> models = Utility.SearchWMIToList(ConnectionManager, query); foreach (IResultObject model in models) { DataGridViewRow row = dataGridViewDriverPackages.Rows .Cast <DataGridViewRow>() .Where(r => r.Cells[1].Value.ToString().Equals(model["Model"].StringValue, StringComparison.CurrentCultureIgnoreCase)) .FirstOrDefault(); if (row != null) { row.Cells[0].Value = true; row.Cells[4].Value = "Model detected"; known = true; } } foreach (DataGridViewRow dataGridViewRow in dataGridViewDriverPackages.Rows) { DellDriverPackage package = (DellDriverPackage)dataGridViewRow.Tag; string path = Path.Combine(sourceFolderPath, package.FolderName); if (Directory.Exists(path)) { string[] fileList = Directory.GetFiles(path, "*.version"); if (File.Exists(Path.Combine(path, package.VersionFile))) { dataGridViewRow.Cells[0].Value = false; dataGridViewRow.Cells[4].Value = "Downloaded"; } else if (fileList.Length > 0) { dataGridViewRow.Cells[0].Value = false; dataGridViewRow.Cells[4].Value = "New version"; } } } dataGridViewDriverPackages.Sort(known ? columnStatus : columnPack, known ? ListSortDirection.Descending : ListSortDirection.Ascending); Initialized = true; }
public Stream OpenFile(string File) => engine.Unpack(CabinetStream, File);