public static bool Decompress(string filePath, string extractFolder) { try { var cab = new CabInfo(filePath); cab.Unpack(extractFolder); return true; } catch { return false; } }
public static string GetKeyedWsp(string wspFile, string company, string servers, DateTime expiry) { Guid transGuid = new Guid(); //hax: config me string baseDir = @"D:\Hosting\5709750\html\sprockettools\d\"; string source = baseDir + @"original\" + transGuid.ToString() + @"\"; string dest = baseDir + @"modified\"; //load the cab and unpack it to the temporary directory used for this transaction CabInfo cab = new CabInfo(baseDir + wspFile); cab.Unpack(source); //get xml file string keyXmlFile = source + @"Layouts\SprocketValidator\key.xml"; XmlDocument xd = new XmlDocument(); xd.Load(keyXmlFile); //update XML file and write back to disk XmlNode n = xd.SelectSingleNode(@"/key"); n.InnerText = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(company + "|" + servers + "|" + DateTime.Today.AddDays(14).ToShortDateString())); using (XmlWriter xw = XmlWriter.Create(keyXmlFile)) { xd.WriteTo(xw); xw.Flush(); } //create a list with files and add them to a cab file string newFile = (dest + DateTime.Now.ToString("yyyyMMdd") + "_" + company + "_" + wspFile).Replace(" ", ""); CabInfo newCab = new CabInfo(newFile); List<string> paths = new List<string>(); //put files into list to be packed foreach (string compFile in Directory.GetFiles(source, "*", SearchOption.AllDirectories)) { paths.Add(compFile.Replace(source, "")); } newCab.PackFiles(source, paths, paths); //delete the transaction directory Directory.Delete(source,true); return newFile; }
private static void DownloadAndUnpackCab() { var webClient = new WebClient(); try { if (Settings.Proxy != null) webClient.Proxy = Settings.Proxy; webClient.DownloadFile("http://update.microsoft.com/redist/wuredist.cab", Path.Combine(TempDir, CabFileName)); } catch (Exception e) { Logger.Log("Could not download WUA cab file.", LogLevel.Error); Logger.LogException(e); } var cab = new CabInfo(Path.Combine(TempDir, CabFileName)); cab.Unpack(TempDir); }
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); }
public void CabinetExtractUpdate() { int fileCount = 5, fileSize = 2048; string dirA = String.Format("{0}-{1}-A", fileCount, fileSize); if (Directory.Exists(dirA)) Directory.Delete(dirA, true); Directory.CreateDirectory(dirA); 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); } CabInfo cabInfo = new CabInfo("testupdate.cab"); cabInfo.Pack(dirA); cabInfo.Unpack(dirB); DateTime originalTime = File.GetLastWriteTime(Path.Combine(dirA, "€1.txt")); DateTime pastTime = originalTime - new TimeSpan(0, 5, 0); DateTime futureTime = originalTime + new TimeSpan(0, 5, 0); using (CabEngine cabEngine = new CabEngine()) { string cabName = "testupdate.cab"; ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext(cabName, dirB, null); streamContext.ExtractOnlyNewerFiles = true; Assert.AreEqual<bool>(true, streamContext.ExtractOnlyNewerFiles); Assert.IsNotNull(streamContext.ArchiveFiles); Assert.AreEqual<int>(1, streamContext.ArchiveFiles.Count); Assert.AreEqual<string>(cabName, streamContext.ArchiveFiles[0]); Assert.AreEqual<string>(dirB, streamContext.Directory); File.SetLastWriteTime(Path.Combine(dirB, "€1.txt"), futureTime); cabEngine.Unpack(streamContext, null); Assert.IsTrue(File.GetLastWriteTime(Path.Combine(dirB, "€1.txt")) - originalTime > new TimeSpan(0, 4, 55)); File.SetLastWriteTime(Path.Combine(dirB, "€1.txt"), pastTime); File.SetLastWriteTime(Path.Combine(dirB, "€2.txt"), pastTime); File.SetAttributes(Path.Combine(dirB, "€2.txt"), FileAttributes.ReadOnly); File.SetAttributes(Path.Combine(dirB, "€2.txt"), FileAttributes.Hidden); File.SetAttributes(Path.Combine(dirB, "€2.txt"), FileAttributes.System); cabEngine.Unpack(streamContext, null); Assert.IsTrue((File.GetLastWriteTime(Path.Combine(dirB, "€1.txt")) - originalTime).Duration() < new TimeSpan(0, 0, 5)); // Just test the rest of the streamContext properties here. IDictionary<string, string> testMap = new Dictionary<string, string>(); streamContext = new ArchiveFileStreamContext(cabName, dirB, testMap); Assert.AreSame(testMap, streamContext.Files); Assert.IsFalse(streamContext.EnableOffsetOpen); streamContext.EnableOffsetOpen = true; Assert.IsTrue(streamContext.EnableOffsetOpen); streamContext = new ArchiveFileStreamContext(cabName, ".", testMap); Assert.AreEqual<string>(".", streamContext.Directory); string[] testArchiveFiles = new string[] { cabName }; streamContext = new ArchiveFileStreamContext(testArchiveFiles, ".", testMap); Assert.AreSame(testArchiveFiles, streamContext.ArchiveFiles); } }
/// <summary> /// Unpack a cab file /// </summary> public void UnCab(string cabFile, string tempFolderToUse) { EnsureDirectoryExists(tempFolderToUse); CabInfo cabToUnpack = new CabInfo(cabFile); cabToUnpack.Unpack(tempFolderToUse); }
private static void DownloadAndUnpackCab() { WebClient webClient = new WebClient(); try { webClient.DownloadFile("http://update.microsoft.com/redist/wuredist.cab", Path.Combine(tempDir, cabFileName)); } catch (Exception e) { AgentSettings.Log("Exception: {0}", AgentLogLevel.Error, e.Message); if (e.InnerException != null) { AgentSettings.Log("Inner exception: {0}", AgentLogLevel.Error, e.InnerException.Message); } AgentSettings.Log("Could not download WUA cab file.", AgentLogLevel.Error); } CabInfo cab = new CabInfo(Path.Combine(tempDir, cabFileName)); cab.Unpack(tempDir); }