Example #1
0
        public void CabFileInfoOpenText()
        {
            CabInfo cabInfo = new CabInfo("test.cab");
            int txtSize = 10240;
            CompressionTestUtil.GenerateRandomFile("test00.txt", 0, txtSize);
            CompressionTestUtil.GenerateRandomFile("test01.txt", 1, txtSize);

            string expectedText = File.ReadAllText("test01.txt");
            
            cabInfo.PackFiles(null, new string[] { "test00.txt", "test01.txt" }, null);

            CabFileInfo cfi = new CabFileInfo(cabInfo, "test01.txt");
            using (StreamReader cabFileReader = cfi.OpenText())
            {
                string text = cabFileReader.ReadToEnd();
                Assert.AreEqual(expectedText, text);

                // Check the assumption that the cab can't be deleted while a stream is open.
                Exception caughtEx = null;
                try
                {
                    File.Delete(cabInfo.FullName);
                }
                catch (Exception ex)
                {
                    caughtEx = ex;
                }

                Assert.IsInstanceOfType(caughtEx, typeof(IOException));
            }

            // Ensure all streams are closed after disposing of the StreamReader returned by OpenText.
            File.Delete(cabInfo.FullName);
        }