Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
        public void CabinetUtfPaths()
        {
            string[] files = new string[]
            {
                "어그리먼트送信ポート1ßà_Agreement.txt",
                "콘토소ßà_MyProfile.txt",
                "파트너1ßà_PartnerProfile.txt",
            };

            string dirA = "utf8-A";
            if (Directory.Exists(dirA)) Directory.Delete(dirA, true);
            Directory.CreateDirectory(dirA);
            string dirB = "utf8-B";
            if (Directory.Exists(dirB)) Directory.Delete(dirB, true);
            Directory.CreateDirectory(dirB);

            int txtSize = 1024;
            CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[0]), 0, txtSize);
            CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[1]), 1, txtSize);
            CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[2]), 2, txtSize);

            ArchiveFileStreamContext streamContextA = new ArchiveFileStreamContext("utf8.cab", dirA, null);
            using (CabEngine cabEngine = new CabEngine())
            {
                cabEngine.Pack(streamContextA, files);
            }

            ArchiveFileStreamContext streamContextB = new ArchiveFileStreamContext("utf8.cab", dirB, null);
            using (CabEngine cex = new CabEngine())
            {
                cex.Unpack(streamContextB, null);
            }

            bool directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB);
            Assert.IsTrue(directoryMatch,
                "Testing whether cabinet output directory matches input directory.");
        }
Ejemplo n.º 3
0
        public void CabinetOffset()
        {
            int txtSize = 10240;
            CompressionTestUtil.GenerateRandomFile("test.txt", 0, txtSize);
            CompressionTestUtil.GenerateRandomFile("base.txt", 1, 2 * txtSize + 4);

            ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext("base.txt", null, null);
            streamContext.EnableOffsetOpen = true;

            using (CabEngine cabEngine = new CabEngine())
            {
                cabEngine.Pack(streamContext, new string[] { "test.txt" });
            }

            Assert.IsTrue(new FileInfo("base.txt").Length > 2 * txtSize + 4);

            string saveText;
            using (Stream txtStream = File.OpenRead("test.txt"))
            {
                saveText = new StreamReader(txtStream).ReadToEnd();
            }
            File.Delete("test.txt");

            using (CabEngine cex = new CabEngine())
            {
                cex.Unpack(streamContext, null);
            }
            string testText;
            using (Stream txtStream = File.OpenRead("test.txt"))
            {
                testText = new StreamReader(txtStream).ReadToEnd();
            }
            Assert.AreEqual<string>(saveText, testText);
        }
Ejemplo n.º 4
0
        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);
            }
        }
Ejemplo n.º 5
0
 public void CabExtractorExtract()
 {
     int txtSize = 40960;
     CabInfo cabInfo = new CabInfo("test.cab");
     CompressionTestUtil.GenerateRandomFile("test0.txt", 0, txtSize);
     CompressionTestUtil.GenerateRandomFile("test1.txt", 1, txtSize);
     cabInfo.PackFiles(null, new string[] { "test0.txt", "test1.txt" }, null);
     using (CabEngine cabEngine = new CabEngine())
     {
         using (Stream cabStream = File.OpenRead("test.cab"))
         {
             using (Stream exStream = cabEngine.Unpack(cabStream, "test0.txt"))
             {
                 string str = new StreamReader(exStream).ReadToEnd();
                 string expected = new StreamReader("test0.txt").ReadToEnd();
                 Assert.AreEqual<string>(expected, str);
             }
             cabStream.Seek(0, SeekOrigin.Begin);
             using (Stream exStream = cabEngine.Unpack(cabStream, "test1.txt"))
             {
                 string str = new StreamReader(exStream).ReadToEnd();
                 string expected = new StreamReader("test1.txt").ReadToEnd();
                 Assert.AreEqual<string>(expected, str);
             }
         }
         using (Stream txtStream = File.OpenRead("test0.txt"))
         {
             Exception caughtEx = null;
             try
             {
                 cabEngine.Unpack(txtStream, "test0.txt");
             }
             catch (Exception ex) { caughtEx = ex; }
             Assert.IsInstanceOfType(caughtEx, typeof(CabException));
             Assert.AreEqual<int>(2, ((CabException) caughtEx).Error);
             Assert.AreEqual<int>(0, ((CabException) caughtEx).ErrorCode);
             Assert.AreEqual<string>("Cabinet file does not have the correct format.", caughtEx.Message);
         }
     }
 }
Ejemplo n.º 6
0
        private IList<ArchiveFileInfo> RunCabinetPackUnpack(int fileCount, long fileSize,
            long maxFolderSize, long maxArchiveSize, CompressionLevel compLevel)
        {
            Console.WriteLine("Creating cabinet with {0} files of size {1}",
                fileCount, fileSize);
            Console.WriteLine("MaxFolderSize={0}, MaxArchiveSize={1}, CompressionLevel={2}",
                maxFolderSize, maxArchiveSize, compLevel);

            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] = TEST_FILENAME_PREFIX + iFile + ".txt";
                CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[iFile]), iFile, fileSize);
            }

            string[] archiveNames = new string[100];
            for (int i = 0; i < archiveNames.Length; i++)
            {
                archiveNames[i] = String.Format("{0}-{1}{2}{3}.cab", fileCount, fileSize,
                    (i == 0 ? "" : "-"), (i == 0 ? "" : i.ToString()));
            }

            string progressTextFile = String.Format("progress_{0}-{1}.txt", fileCount, fileSize);
            CompressionTestUtil testUtil = new CompressionTestUtil(progressTextFile);

            IList<ArchiveFileInfo> fileInfo;
            using (CabEngine cabEngine = new CabEngine())
            {
                cabEngine.CompressionLevel = compLevel;

                File.AppendAllText(progressTextFile,
                    "\r\n\r\n====================================================\r\nCREATE\r\n\r\n");
                cabEngine.Progress += testUtil.PrintArchiveProgress;

                OptionStreamContext streamContext = new OptionStreamContext(archiveNames, dirA, null);
                if (maxFolderSize == 1)
                {
                    streamContext.OptionHandler =
                        delegate(string optionName, object[] parameters)
                        {
                            if (optionName == "nextFolder") return true;
                            return null;
                        };
                }
                else if (maxFolderSize > 1)
                {
                    streamContext.OptionHandler =
                        delegate(string optionName, object[] parameters)
                        {
                            if (optionName == "maxFolderSize") return maxFolderSize;
                            return null;
                        };
                }
                cabEngine.Pack(streamContext, files, maxArchiveSize);

                IList<string> createdArchiveNames = new List<string>(archiveNames.Length);
                for (int i = 0; i < archiveNames.Length; i++)
                {
                    if (File.Exists(archiveNames[i]))
                    {
                        createdArchiveNames.Add(archiveNames[i]);
                    }
                    else
                    {
                        break;
                    }
                }

                Console.WriteLine("Listing cabinet with {0} files of size {1}",
                    fileCount, fileSize);
                File.AppendAllText(progressTextFile, "\r\n\r\nLIST\r\n\r\n");
                fileInfo = cabEngine.GetFileInfo(
                    new ArchiveFileStreamContext(createdArchiveNames, null, null), null);

                Assert.AreEqual<int>(fileCount, fileInfo.Count);
                if (fileCount > 0)
                {
                    int folders = ((CabFileInfo) fileInfo[fileInfo.Count - 1]).CabinetFolderNumber + 1;
                    if (maxFolderSize == 1)
                    {
                        Assert.AreEqual<int>(fileCount, folders);
                    }
                }

                Console.WriteLine("Extracting cabinet with {0} files of size {1}",
                    fileCount, fileSize);
                File.AppendAllText(progressTextFile, "\r\n\r\nEXTRACT\r\n\r\n");
                cabEngine.Unpack(new ArchiveFileStreamContext(createdArchiveNames, dirB, null), null);
            }

            bool directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB);
            Assert.IsTrue(directoryMatch,
                "Testing whether cabinet output directory matches input directory.");

            return fileInfo;
        }