Ejemplo n.º 1
0
        private IList<ArchiveFileInfo> RunZipPackUnpack(int fileCount, long fileSize,
            long maxArchiveSize, CompressionLevel compLevel)
        {
            Console.WriteLine("Creating zip archive with {0} files of size {1}",
                fileCount, fileSize);
            Console.WriteLine("MaxArchiveSize={0}, CompressionLevel={1}", 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[1000];
            for (int i = 0; i < archiveNames.Length; i++)
            {
                if (i < 100)
                {
                    archiveNames[i] = String.Format(
                        (i == 0 ? "{0}-{1}.zip" : "{0}-{1}.z{2:d02}"),
                        fileCount, fileSize, i);
                }
                else
                {
                    archiveNames[i] = String.Format(
                         "{0}-{1}.{2:d03}", fileCount, fileSize, i);
                }
            }

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

            IList<ArchiveFileInfo> fileInfo;
            using (ZipEngine zipEngine = new ZipEngine())
            {
                zipEngine.CompressionLevel = compLevel;

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

                OptionStreamContext streamContext = new OptionStreamContext(archiveNames, dirA, null);
                streamContext.OptionHandler =
                    delegate(string optionName, object[] parameters)
                    {
                        // For testing purposes, force zip64 for only moderately large files.
                        switch (optionName)
                        {
                            case  "forceZip64":
                                return fileSize > UInt16.MaxValue;
                            default:
                                return null;
                        }
                    };

                zipEngine.Pack(streamContext, files, maxArchiveSize);

                string checkArchiveName = archiveNames[0];
                if (File.Exists(archiveNames[1])) checkArchiveName = archiveNames[1];
                using (Stream archiveStream = File.OpenRead(checkArchiveName))
                {
                    bool isArchive = zipEngine.IsArchive(archiveStream);
                    Assert.IsTrue(isArchive, "Checking that created archive appears valid.");
                }

                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;
                    }
                }

                Assert.AreNotEqual<int>(0, createdArchiveNames.Count);

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

                Assert.AreEqual<int>(fileCount, fileInfo.Count);

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

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

            return fileInfo;
        }
Ejemplo n.º 2
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;
        }
Ejemplo n.º 3
0
        private IList <ArchiveFileInfo> RunZipPackUnpack(int fileCount, long fileSize,
                                                         long maxArchiveSize, CompressionLevel compLevel)
        {
            Console.WriteLine("Creating zip archive with {0} files of size {1}",
                              fileCount, fileSize);
            Console.WriteLine("MaxArchiveSize={0}, CompressionLevel={1}", 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[1000];
            for (int i = 0; i < archiveNames.Length; i++)
            {
                if (i < 100)
                {
                    archiveNames[i] = String.Format(
                        (i == 0 ? "{0}-{1}.zip" : "{0}-{1}.z{2:d02}"),
                        fileCount, fileSize, i);
                }
                else
                {
                    archiveNames[i] = String.Format(
                        "{0}-{1}.{2:d03}", fileCount, fileSize, i);
                }
            }

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

            IList <ArchiveFileInfo> fileInfo;

            using (ZipEngine zipEngine = new ZipEngine())
            {
                zipEngine.CompressionLevel = compLevel;

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

                OptionStreamContext streamContext = new OptionStreamContext(archiveNames, dirA, null);
                streamContext.OptionHandler =
                    delegate(string optionName, object[] parameters)
                {
                    // For testing purposes, force zip64 for only moderately large files.
                    switch (optionName)
                    {
                    case  "forceZip64":
                        return(fileSize > UInt16.MaxValue);

                    default:
                        return(null);
                    }
                };

                zipEngine.Pack(streamContext, files, maxArchiveSize);

                string checkArchiveName = archiveNames[0];
                if (File.Exists(archiveNames[1]))
                {
                    checkArchiveName = archiveNames[1];
                }
                using (Stream archiveStream = File.OpenRead(checkArchiveName))
                {
                    bool isArchive = zipEngine.IsArchive(archiveStream);
                    Assert.IsTrue(isArchive, "Checking that created archive appears valid.");
                }

                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;
                    }
                }

                Assert.AreNotEqual <int>(0, createdArchiveNames.Count);

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

                Assert.AreEqual <int>(fileCount, fileInfo.Count);

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

            bool directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB);

            Assert.IsTrue(directoryMatch,
                          "Testing whether zip output directory matches input directory.");

            return(fileInfo);
        }