Пример #1
0
 public static string Compress(string folderPath, string fileNamePath)
 {
     try
     {
         var cab = new CabInfo(fileNamePath);
         cab.Pack(folderPath, true, Microsoft.Deployment.Compression.CompressionLevel.Max, null);
         return cab.FullName;
     }
     catch
     {
         return String.Empty;
     }
 }
Пример #2
0
        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);
        }
Пример #3
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);
            }
        }
Пример #4
0
        /// <summary>
        /// Repack the cab file
        /// </summary>
        public void ReCab(string cabFile, string unCabFolder)
        {
            CabInfo cabToPack = new CabInfo(cabFile);
            cabToPack.Pack(unCabFolder, true, Microsoft.Deployment.Compression.CompressionLevel.Min, null);

            try
            {
                // Cleanup the temp folder with unpacked data
                System.IO.Directory.Delete(unCabFolder, true);
            }
            catch { }
        }