public void CreateZipShouldLeaveOutputStreamOpenIfRequested(bool leaveOpen) { const string tempFileName = "a(2).dat"; using (var tempFolder = new Utils.TempDir()) { // Create test input file string addFile = Path.Combine(tempFolder.Fullpath, tempFileName); MakeTempFile(addFile, 16); // Create the zip with fast zip var target = new TrackedMemoryStream(); var fastZip = new FastZip(); fastZip.CreateZip(target, tempFolder.Fullpath, false, @"a\(2\)\.dat", null, leaveOpen: leaveOpen); // Check that the output stream was disposed (or not) as expected Assert.That(target.IsDisposed, Is.Not.EqualTo(leaveOpen), "IsDisposed should be the opposite of leaveOpen"); // Check that the file contents are correct in both cases var archive = new MemoryStream(target.ToArray()); using (ZipFile zf = new ZipFile(archive)) { Assert.AreEqual(1, zf.Count); ZipEntry entry = zf[0]; Assert.AreEqual(tempFileName, entry.Name); Assert.AreEqual(16, entry.Size); Assert.IsTrue(zf.TestArchive(true)); } } }
public void CreateEmptyDirectories(string password) { using (var tempFilePath = new Utils.TempDir()) { string name = Path.Combine(tempFilePath.Fullpath, "x.zip"); // Create empty test folders (The folder that we'll zip, and the test sub folder). string archiveRootDir = Path.Combine(tempFilePath.Fullpath, ZipTempDir); string targetDir = Path.Combine(archiveRootDir, "floyd"); Directory.CreateDirectory(targetDir); // Create the archive with FastZip var fastZip = new FastZip { CreateEmptyDirectories = true, Password = password, }; fastZip.CreateZip(name, archiveRootDir, true, null); // Test that the archive contains the empty folder entry using (var zipFile = new ZipFile(name)) { Assert.That(zipFile.Count, Is.EqualTo(1), "Should only be one entry in the file"); var folderEntry = zipFile.GetEntry("floyd/"); Assert.That(folderEntry.IsDirectory, Is.True, "The entry must be a folder"); Assert.IsTrue(zipFile.TestArchive(true)); } } }
public void RootPathIsRespected() { using (var extractDirectory = new Utils.TempDir()) using (var tarFileName = new Utils.TempFile()) using (var tempDirectory = new Utils.TempDir()) { tempDirectory.CreateDummyFile(); using (var tarFile = File.Open(tarFileName.Filename, FileMode.Create)) { using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile)) { tarOutputStream.RootPath = tempDirectory.Fullpath; var entry = TarEntry.CreateEntryFromFile(tempDirectory.Fullpath); tarOutputStream.WriteEntry(entry, true); } } using (var file = File.OpenRead(tarFileName.Filename)) { using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8)) { archive.ExtractContents(extractDirectory.Fullpath); } } var expectationDirectory = new DirectoryInfo(tempDirectory.Fullpath); foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories)) { var relativePath = checkFile.FullName.Substring(expectationDirectory.FullName.Length + 1); FileAssert.Exists(Path.Combine(extractDirectory.Fullpath, relativePath)); FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory.Fullpath, relativePath)); } } }
public void CreateExceptions() { Assert.Throws <DirectoryNotFoundException>(() => { using (var tempDir = new Utils.TempDir()) { var fastZip = new FastZip(); var badPath = Path.Combine(Path.GetTempPath(), Utils.GetDummyFileName()); var addFile = Path.Combine(tempDir.Fullpath, "test.zip"); fastZip.CreateZip(addFile, badPath, false, null); } }); }
private static TrackedMemoryStream CreateFastZipTestArchiveWithAnEntry(FastZip fastZip, Action <FileInfo> alterFile = null) { var target = new TrackedMemoryStream(); using (var tempFolder = new Utils.TempDir()) { // Create test input file var addFile = Path.Combine(tempFolder.Fullpath, SingleEntryFileName); MakeTempFile(addFile, 16); var fi = new FileInfo(addFile); alterFile?.Invoke(fi); fastZip.CreateZip(target, tempFolder.Fullpath, false, SingleEntryFileName, null, leaveOpen: true); } return(target); }
public void ExtractZipShouldSetTimeOnFilesWithEmptyConstructor(DateTimeKind dtk) { // Create the archive with a fixed datetime var targetTime = ExpectedFixedTime(dtk); var target = CreateFastZipTestArchiveWithAnEntry(new FastZip(targetTime)); // Extract the archive with an empty constructor var fastZip = new FastZip(); using (var extractDir = new Utils.TempDir()) { fastZip.ExtractZip(target, extractDir.Fullpath, FastZip.Overwrite.Always, _ => true, "", "", true, true, false); var fi = new FileInfo(Path.Combine(extractDir.Fullpath, SingleEntryFileName)); Assert.AreEqual(targetTime, FileTimeFromTimeSetting(fi, TimeSetting.Fixed)); } }
private void TestFileNames(IEnumerable <string> names) { var zippy = new FastZip(); using (var tempDir = new Utils.TempDir()) using (var tempZip = new Utils.TempFile()) { int nameCount = 0; foreach (var name in names) { tempDir.CreateDummyFile(name); nameCount++; } zippy.CreateZip(tempZip.Filename, tempDir.Fullpath, true, null); using (ZipFile z = new ZipFile(tempZip.Filename)) { Assert.AreEqual(nameCount, z.Count); foreach (var name in names) { var index = z.FindEntry(name, true); Assert.AreNotEqual(index, -1, "Zip entry \"{0}\" not found", name); var entry = z[index]; if (ZipStrings.UseUnicode) { Assert.IsTrue(entry.IsUnicodeText, "Zip entry #{0} not marked as unicode", index); } else { Assert.IsFalse(entry.IsUnicodeText, "Zip entry #{0} marked as unicode", index); } Assert.AreEqual(name, entry.Name); var nameBytes = string.Join(" ", Encoding.BigEndianUnicode.GetBytes(entry.Name).Select(b => b.ToString("x2"))); Console.WriteLine($" - Zip entry: {entry.Name} ({nameBytes})"); } } } }
public void ExtractingCorruptTarShouldntLeakFiles() { using (var memoryStream = new MemoryStream()) { //Create a tar.gz in the output stream using (var gzipStream = new GZipOutputStream(memoryStream)) { gzipStream.IsStreamOwner = false; using (var tarOut = TarArchive.CreateOutputTarArchive(gzipStream)) using (var dummyFile = Utils.GetDummyFile(32000)) { tarOut.IsStreamOwner = false; tarOut.WriteEntry(TarEntry.CreateEntryFromFile(dummyFile.Filename), false); } } // corrupt archive - make sure the file still has more than one block memoryStream.SetLength(16000); memoryStream.Seek(0, SeekOrigin.Begin); // try to extract using (var gzipStream = new GZipInputStream(memoryStream)) { string tempDirName; gzipStream.IsStreamOwner = false; using (var tempDir = new Utils.TempDir()) { tempDirName = tempDir.Fullpath; using (var tarIn = TarArchive.CreateInputTarArchive(gzipStream, null)) { tarIn.IsStreamOwner = false; Assert.Throws <SharpZipBaseException>(() => tarIn.ExtractContents(tempDir.Fullpath)); } } Assert.That(Directory.Exists(tempDirName), Is.False, "Temporary folder should have been removed"); } } }
public void ExtractZipShouldSetTimeOnFilesFromConstructorDateTime(DateTimeKind dtk) { // Create the archive with a fixed "bad" datetime var target = CreateFastZipTestArchiveWithAnEntry(new FastZip(UnexpectedFixedTime(dtk))); // Extract the archive with a fixed time override var targetTime = ExpectedFixedTime(dtk); var fastZip = new FastZip(targetTime); using (var extractDir = new Utils.TempDir()) { fastZip.ExtractZip(target, extractDir.Fullpath, FastZip.Overwrite.Always, _ => true, "", "", true, true, false); var fi = new FileInfo(Path.Combine(extractDir.Fullpath, SingleEntryFileName)); var fileTime = FileTimeFromTimeSetting(fi, TimeSetting.Fixed); if (fileTime.Kind != dtk) { fileTime = fileTime.ToUniversalTime(); } Assert.AreEqual(targetTime, fileTime); } }
public void ExtractZipShouldSetTimeOnFilesFromConstructorTimeSetting(TimeSetting timeSetting) { var targetTime = ExpectedFixedTime(); var archiveStream = CreateFastZipTestArchiveWithAnEntry(new FastZip(targetTime)); if (timeSetting == TimeSetting.Fixed) { Assert.Ignore("Fixed time without specifying a time is undefined"); } var fastZip = new FastZip(timeSetting); using (var extractDir = new Utils.TempDir()) { fastZip.ExtractZip(archiveStream, extractDir.Fullpath, FastZip.Overwrite.Always, _ => true, "", "", true, true, false); var fi = new FileInfo(Path.Combine(extractDir.Fullpath, SingleEntryFileName)); var actualTime = FileTimeFromTimeSetting(fi, timeSetting); // Assert that the time is within +/- 2s of the target time to allow for timing/rounding discrepancies Assert.LessOrEqual(Math.Abs((targetTime - actualTime).TotalSeconds), 2); } }
public void ContentEqualAfterAfterArchived([Values(0, 1, 64)] int contentSize) { using (var sourceDir = new Utils.TempDir()) using (var targetDir = new Utils.TempDir()) using (var zipFile = Utils.GetDummyFile(0)) { var sourceFile = sourceDir.CreateDummyFile(contentSize); var sourceContent = File.ReadAllBytes(sourceFile); new FastZip().CreateZip(zipFile.Filename, sourceDir.Fullpath, true, null); Assert.DoesNotThrow(() => { new FastZip().ExtractZip(zipFile.Filename, targetDir.Fullpath, null); }, "Exception during extraction of test archive"); var targetFile = Path.Combine(targetDir.Fullpath, Path.GetFileName(sourceFile)); var targetContent = File.ReadAllBytes(targetFile); Assert.AreEqual(sourceContent.Length, targetContent.Length, "Extracted file size does not match source file size"); Assert.AreEqual(sourceContent, targetContent, "Extracted content does not match source content"); } }
public void EndBlockHandling() { int dummySize = 70145; long outCount, inCount; using (var ms = new MemoryStream()) { using (var tarOut = TarArchive.CreateOutputTarArchive(ms)) using (var dummyFile = Utils.GetDummyFile(dummySize)) { tarOut.IsStreamOwner = false; tarOut.WriteEntry(TarEntry.CreateEntryFromFile(dummyFile.Filename), false); } outCount = ms.Position; ms.Seek(0, SeekOrigin.Begin); using (var tarIn = TarArchive.CreateInputTarArchive(ms, null)) using (var tempDir = new Utils.TempDir()) { tarIn.IsStreamOwner = false; tarIn.ExtractContents(tempDir.Fullpath); foreach (var file in Directory.GetFiles(tempDir.Fullpath, "*", SearchOption.AllDirectories)) { Console.WriteLine($"Extracted \"{file}\""); } } inCount = ms.Position; Console.WriteLine($"Output count: {outCount}"); Console.WriteLine($"Input count: {inCount}"); Assert.AreEqual(inCount, outCount, "Bytes read and bytes written should be equal"); } }