public void _Internal_Streams_WinZip_Zip(int fodderOption, string password, string label) { if (!WinZipIsPresent) throw new Exception("skipping test [_Internal_Streams_WinZip_Zip] : winzip is not present"); int[] fileCounts = { 1, 2, _rnd.Next(8) + 6, _rnd.Next(18) + 16, _rnd.Next(48) + 56 }; for (int m = 0; m < fileCounts.Length; m++) { string dirToZip = String.Format("trial{0:D2}", m); if (!Directory.Exists(dirToZip)) Directory.CreateDirectory(dirToZip); int fileCount = fileCounts[m]; string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("Streams_Winzip_Zip.{0}.{1}.{2}.zip", fodderOption, label, m)); string[] files = null; if (fodderOption == 0) { // zero length files files = new string[fileCount]; for (int i = 0; i < fileCount; i++) files[i] = CreateZeroLengthFile(i, dirToZip); } else if (fodderOption == 1) files = TestUtilities.GenerateFilesFlat(dirToZip, fileCount, 100, 72000); else { // mixed files = new string[fileCount]; for (int i = 0; i < fileCount; i++) { if (_rnd.Next(3) == 0) files[i] = CreateZeroLengthFile(i, dirToZip); else { files[i] = Path.Combine(dirToZip, String.Format("nonzero{0:D4}.txt", i)); TestUtilities.CreateAndFillFileText(files[i], _rnd.Next(60000) + 100); } } } // Create the zip archive via WinZip.exe string pwdOption = String.IsNullOrEmpty(password) ? "" : "-s" + password; string formatString = "-a -p {0} -yx {1} {2}\\*.*"; string wzzipOut = this.Exec(wzzip, String.Format(formatString, pwdOption, zipFileToCreate, dirToZip)); // Verify the number of files in the zip Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), files.Length, "Incorrect number of entries in the zip file."); // extract the files string extractDir = String.Format("extract{0:D2}", m); Directory.CreateDirectory(extractDir); byte[] buffer = new byte[2048]; int n; using (var raw = File.OpenRead(zipFileToCreate)) { using (var input = new ZipInputStream(raw)) { input.Password = password; ZipEntry e; while ((e = input.GetNextEntry()) != null) { TestContext.WriteLine("entry: {0}", e.FileName); string outputPath = Path.Combine(extractDir, e.FileName); if (e.IsDirectory) { // create the directory Directory.CreateDirectory(outputPath); continue; } // create the file using (var output = File.Create(outputPath)) { while ((n = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, n); } } // we don't set the timestamps or attributes // on the file/directory. } } } string[] filesUnzipped = Directory.GetFiles(extractDir); // Verify the number of files extracted Assert.AreEqual<int>(files.Length, filesUnzipped.Length, "Incorrect number of files extracted."); } }
public void ZIS_ZOS_VaryCompression() { string testBin = TestUtilities.GetTestBinDir(CurrentDir); string resourceDir = Path.Combine(testBin, "Resources"); var filesToAdd = Directory.GetFiles(resourceDir); Func<int, int, bool> chooseCompression = (ix, cycle) => { var name = Path.GetFileName(filesToAdd[ix]); switch (cycle) { case 0: return !(name.EndsWith(".zip") || name.EndsWith(".docx") || name.EndsWith(".xslx")); case 1: return ((ix%2)==0); default: return (ix == filesToAdd.Length - 1); } }; // Three cycles - three different ways to vary compression for (int k=0; k < 3; k++) { string zipFileToCreate = String.Format("VaryCompression-{0}.zip", k); TestContext.WriteLine(""); TestContext.WriteLine("Creating zip, cycle {0}", k); using (var fileStream = File.OpenWrite(zipFileToCreate)) { using (var zos = new ZipOutputStream(fileStream, true)) { for (int i=0; i < filesToAdd.Length; i++) { var file = filesToAdd[i]; var shortName = Path.GetFileName(file); bool compress = chooseCompression(i, k); if (compress) zos.CompressionLevel = Alienlab.Zlib.CompressionLevel.Default; else zos.CompressionLevel = Alienlab.Zlib.CompressionLevel.None; zos.PutNextEntry(shortName); using (var input = File.OpenRead(file)) { CopyStream(input, zos); } } } } TestContext.WriteLine(""); TestContext.WriteLine("Extracting cycle {0}", k); string extractDir = "extract-" + k; Directory.CreateDirectory(extractDir); using (var raw = File.OpenRead(zipFileToCreate)) { using (var input = new ZipInputStream(raw)) { ZipEntry e; while ((e = input.GetNextEntry()) != null) { TestContext.WriteLine("entry: {0}", e.FileName); string outputPath = Path.Combine(extractDir, e.FileName); if (e.IsDirectory) { // create the directory Directory.CreateDirectory(outputPath); } else { // create the file using (var output = File.Create(outputPath)) { CopyStream(input,output); } } } } } string[] filesUnzipped = Directory.GetFiles(extractDir); Assert.AreEqual<int>(filesToAdd.Length, filesUnzipped.Length, "Incorrect number of files extracted."); } }
public void _Internal_Streams_7z_Zip(int flavor, string label) { if (!SevenZipIsPresent) { TestContext.WriteLine("skipping test [_Internal_Streams_7z_Zip] : SevenZip is not present"); return; } int[] fileCounts = { 1, 2, _rnd.Next(8) + 6, _rnd.Next(18) + 16, _rnd.Next(48) + 56 }; for (int m = 0; m < fileCounts.Length; m++) { string dirToZip = String.Format("trial{0:D2}", m); if (!Directory.Exists(dirToZip)) Directory.CreateDirectory(dirToZip); int fileCount = fileCounts[m]; string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("Streams_7z_Zip.{0}.{1}.{2}.zip", flavor, label, m)); string[] files = null; if (flavor == 0) { // zero length files files = new string[fileCount]; for (int i = 0; i < fileCount; i++) files[i] = CreateZeroLengthFile(i, dirToZip); } else if (flavor == 1) files = TestUtilities.GenerateFilesFlat(dirToZip, fileCount, 100, 72000); else { // mixed files = new string[fileCount]; for (int i = 0; i < fileCount; i++) { if (_rnd.Next(3) == 0) files[i] = CreateZeroLengthFile(i, dirToZip); else { files[i] = Path.Combine(dirToZip, String.Format("nonzero{0:D4}.txt", i)); TestUtilities.CreateAndFillFileText(files[i], _rnd.Next(60000) + 100); } } } // Create the zip archive via 7z.exe this.Exec(sevenZip, String.Format("a {0} {1}", zipFileToCreate, dirToZip)); // Verify the number of files in the zip Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), files.Length, "Incorrect number of entries in the zip file."); // extract the files string extractDir = String.Format("extract{0:D2}", m); byte[] buffer = new byte[2048]; int n; using (var raw = File.OpenRead(zipFileToCreate)) { using (var input = new ZipInputStream(raw)) { ZipEntry e; while ((e = input.GetNextEntry()) != null) { TestContext.WriteLine("entry: {0}", e.FileName); string outputPath = Path.Combine(extractDir, e.FileName); if (e.IsDirectory) { // create the directory Directory.CreateDirectory(outputPath); } else { // create the file using (var output = File.Create(outputPath)) { while ((n = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, n); } } } // we don't set the timestamps or attributes // on the file/directory. } } } // winzip does not include the base path in the filename; // 7zip does. string[] filesUnzipped = Directory.GetFiles(Path.Combine(extractDir, dirToZip)); // Verify the number of files extracted Assert.AreEqual<int>(files.Length, filesUnzipped.Length, "Incorrect number of files extracted."); } }