public static void DirsEqual(string actual, string expected) { var expectedList = FileData.InPath(expected); var actualList = Directory.GetFiles(actual, "*.*", SearchOption.AllDirectories); var actualFolders = Directory.GetDirectories(actual, "*.*", SearchOption.AllDirectories); var actualCount = actualList.Length + actualFolders.Length; Assert.Equal(expectedList.Count, actualCount); ItemEqual(actualList, expectedList, isFile: true); ItemEqual(actualFolders, expectedList, isFile: false); }
public static async Task CreateFromDir(string directory, Stream archiveStream, ZipArchiveMode mode, bool useSpansForWriting = false) { var files = FileData.InPath(directory); using (ZipArchive archive = new ZipArchive(archiveStream, mode, true)) { foreach (var i in files) { if (i.IsFolder) { string entryName = i.FullName; ZipArchiveEntry e = archive.CreateEntry(entryName.Replace('\\', '/') + "/"); e.LastWriteTime = i.LastModifiedDate; } } foreach (var i in files) { if (i.IsFile) { string entryName = i.FullName; var installStream = await StreamHelpers.CreateTempCopyStream(Path.Combine(i.OrigFolder, i.FullName)); if (installStream != null) { ZipArchiveEntry e = archive.CreateEntry(entryName.Replace('\\', '/')); e.LastWriteTime = i.LastModifiedDate; using (Stream entryStream = e.Open()) { int bytesRead; var buffer = new byte[1024]; if (useSpansForWriting) { while ((bytesRead = installStream.Read(new Span <byte>(buffer))) != 0) { entryStream.Write(new ReadOnlySpan <byte>(buffer, 0, bytesRead)); } } else { while ((bytesRead = installStream.Read(buffer, 0, buffer.Length)) != 0) { entryStream.Write(buffer, 0, bytesRead); } } } } } } } }
public static async Task CreateFromDir(String directory, Stream archiveStream, ZipArchiveMode mode) { var files = FileData.InPath(directory); using (ZipArchive archive = new ZipArchive(archiveStream, mode, true)) { foreach (var i in files) { if (i.IsFolder) { String entryName = i.FullName; archive.CreateEntry(entryName.Replace('\\', '/') + "/"); } } foreach (var i in files) { if (i.IsFile) { String entryName = i.FullName; var installStream = await StreamHelpers.CreateTempCopyStream(Path.Combine(i.OrigFolder, i.FullName)); if (installStream != null) { ZipArchiveEntry e = archive.CreateEntry(entryName.Replace('\\', '/')); try { e.LastWriteTime = i.LastModifiedDate; } catch (ArgumentOutOfRangeException) { e.LastWriteTime = DateTimeOffset.Now; } using (Stream entryStream = e.Open()) { installStream.CopyTo(entryStream); } } } } } }
public static void IsZipSameAsDir(Stream archiveFile, string directory, ZipArchiveMode mode, bool requireExplicit, bool checkTimes) { int count = 0; using (ZipArchive archive = new ZipArchive(archiveFile, mode)) { List <FileData> files = FileData.InPath(directory); Assert.All <FileData>(files, (file) => { count++; string entryName = file.FullName; if (file.IsFolder) { entryName += Path.DirectorySeparatorChar; } ZipArchiveEntry entry = archive.GetEntry(entryName); if (entry == null) { entryName = FlipSlashes(entryName); entry = archive.GetEntry(entryName); } if (file.IsFile) { Assert.NotNull(entry); long givenLength = entry.Length; var buffer = new byte[entry.Length]; using (Stream entrystream = entry.Open()) { ReadAllBytes(entrystream, buffer, 0, buffer.Length); #if NETCOREAPP uint zipcrc = entry.Crc32; Assert.Equal(CRC.CalculateCRC(buffer), zipcrc); #endif if (file.Length != givenLength) { buffer = NormalizeLineEndings(buffer); } Assert.Equal(file.Length, buffer.Length); ulong crc = CRC.CalculateCRC(buffer); Assert.Equal(file.CRC, crc.ToString()); } if (checkTimes) { const int zipTimestampResolution = 2; // Zip follows the FAT timestamp resolution of two seconds for file records DateTime lower = file.LastModifiedDate.AddSeconds(-zipTimestampResolution); DateTime upper = file.LastModifiedDate.AddSeconds(zipTimestampResolution); Assert.InRange(entry.LastWriteTime.Ticks, lower.Ticks, upper.Ticks); } Assert.Equal(file.Name, entry.Name); Assert.Equal(entryName, entry.FullName); Assert.Equal(entryName, entry.ToString()); Assert.Equal(archive, entry.Archive); } else if (file.IsFolder) { if (entry == null) //entry not found { string entryNameOtherSlash = FlipSlashes(entryName); bool isEmpty = !files.Any( f => f.IsFile && (f.FullName.StartsWith(entryName, StringComparison.OrdinalIgnoreCase) || f.FullName.StartsWith(entryNameOtherSlash, StringComparison.OrdinalIgnoreCase))); if (requireExplicit || isEmpty) { Assert.Contains("emptydir", entryName); } if ((!requireExplicit && !isEmpty) || entryName.Contains("emptydir")) { count--; //discount this entry } } else { using (Stream es = entry.Open()) { try { Assert.Equal(0, es.Length); } catch (NotSupportedException) { try { Assert.Equal(-1, es.ReadByte()); } catch (Exception) { Console.WriteLine("Didn't return EOF"); throw; } } } } } }); Assert.Equal(count, archive.Entries.Count); } }
public static void IsZipSameAsDir(Stream archiveFile, String directory, ZipArchiveMode mode, Boolean dontRequireExplicit, Boolean dontCheckTimes) { int count = 0; using (ZipArchive archive = new ZipArchive(archiveFile, mode)) { var allFilesInDir = FileData.InPath(directory); foreach (var file in allFilesInDir) { count++; String entryName = file.FullName; if (file.IsFolder) { entryName += Path.DirectorySeparatorChar; } ZipArchiveEntry entry = archive.GetEntry(entryName); if (entry == null) { entryName = FlipSlashes(entryName); entry = archive.GetEntry(entryName); } if (file.IsFile) { try { Assert.NotNull(entry); } catch (Exception) { Console.WriteLine("File Entry {0} in directory but not archive: {1}", entryName, file.FullName); throw; } Int64 givenLength = entry.Length; var buffer = new byte[entry.Length]; using (Stream entrystream = entry.Open()) { entrystream.Read(buffer, 0, buffer.Length); String crc = CRC.CalculateCRC(buffer); Assert.Equal(file.Length, givenLength); Assert.Equal(file.CRC, crc); } if (!dontCheckTimes) { Double offBy = (file.LastModifiedDate - entry.LastWriteTime.DateTime).TotalSeconds; Assert.True( (offBy >= -2 && offBy <= 2) || // Temporary adjustment for active issue 1326 ((offBy >= 3598 && offBy <= 3602)), String.Format("{0}, {1}, {2}", file.LastModifiedDate.ToString(), entry.LastWriteTime.DateTime.ToString(), file.FullName)); } Assert.Equal(file.Name, entry.Name); Assert.Equal(entryName, entry.FullName); Assert.Equal(entryName, entry.ToString()); Assert.Equal(archive, entry.Archive); } else if (file.IsFolder) { if (entry == null) //entry not found { string entryNameOtherSlash = FlipSlashes(entryName); Boolean isEmtpy = !allFilesInDir.Any( f => f.IsFile && (f.FullName.StartsWith(entryName, StringComparison.OrdinalIgnoreCase) || f.FullName.StartsWith(entryNameOtherSlash, StringComparison.OrdinalIgnoreCase))); if ((!dontRequireExplicit || isEmtpy) && !entryName.Contains("emptydir")) { Assert.True(false, String.Format("Folder Entry {0} in directory but not archive: {1}", entryName, directory)); } if ((dontRequireExplicit && !isEmtpy) || entryName.Contains("emptydir")) { count--; //discount this entry } } else { using (Stream es = entry.Open()) { try { Assert.Equal(0, es.Length); } catch (NotSupportedException) { try { Assert.Equal(-1, es.ReadByte()); } catch (Exception) { Console.WriteLine("Didn't return EOF"); throw; } } } } } } Assert.Equal(count, archive.Entries.Count); } }