/// <summary> /// Zips a single file with optionally an alternate filename in the central directory entry. /// Useful for e.g. creating a Node JS AWS lambda package from a web packed project where the entry should be called /// <c>index.js</c> /// </summary> /// <param name="settings">The settings.</param> /// <exception cref="ArgumentException"> /// 'Artifacts' cannot be null or empty - settings /// or /// 'ZipFile' cannot be null or empty - settings /// </exception> /// <exception cref="FileNotFoundException">File not found</exception> public static void ZipSingleFile(CrossPlatformZipSettings settings) { if (string.IsNullOrWhiteSpace(settings.Artifacts)) { throw new ArgumentException("'Artifacts' cannot be null or empty", nameof(settings)); } if (string.IsNullOrWhiteSpace(settings.ZipFile)) { throw new ArgumentException("'ZipFile' cannot be null or empty", nameof(settings)); } if (!File.Exists(settings.Artifacts)) { throw new FileNotFoundException("File not found", settings.Artifacts); } var platformTraits = PlatformTraitsGeneratorFactory.GetPlatformTraits(settings.TargetPlatform); using (var archive = CreateZipFile(settings.ZipFile, settings.CompressionLevel, settings.TargetPlatform)) { AddSingleEntry( archive, new FileInfo(settings.Artifacts), // ReSharper disable once AssignNullToNotNullAttribute - should already have been verified new DirectoryInfo(Path.GetDirectoryName(settings.Artifacts)), platformTraits, settings.LogMessage, settings.LogError, settings.AlternateFileName); } }
/// <summary> /// Zips the specified zip file, storing paths in the central directory appropriate for the target operating system. /// </summary> /// <param name="settings">The settings.</param> /// <exception cref="ArgumentException"> /// 'Artifacts' cannot be null or empty - settings /// or /// 'ZipFile' cannot be null or empty - settings /// </exception> /// <exception cref="FileNotFoundException">No files found to zip at '{path}'</exception> public static void Zip(CrossPlatformZipSettings settings) { List <FileSystemInfo> filesToZip; DirectoryInfo zipRoot; if (string.IsNullOrWhiteSpace(settings.Artifacts)) { throw new ArgumentException("'Artifacts' cannot be null or empty", nameof(settings)); } if (string.IsNullOrWhiteSpace(settings.ZipFile)) { throw new ArgumentException("'ZipFile' cannot be null or empty", nameof(settings)); } var path = Path.GetFullPath(settings.Artifacts.Trim()); if (Directory.Exists(path)) { filesToZip = Directory.EnumerateFileSystemEntries(path, "*", SearchOption.AllDirectories).Select( f => File.Exists(f) ? new FileInfo(f) : (FileSystemInfo) new DirectoryInfo(f)) .ToList(); zipRoot = new DirectoryInfo(path); } else if (!string.IsNullOrEmpty(settings.AlternateFileName)) { ZipSingleFile(settings); return; } else { filesToZip = new List <FileSystemInfo> { new FileInfo(path) }; // ReSharper disable once AssignNullToNotNullAttribute - already checked path is valid above zipRoot = new DirectoryInfo(Path.GetDirectoryName(path)); } if (!filesToZip.Any()) { throw new FileNotFoundException($"No files found to zip at '{path}'"); } var platformTraits = PlatformTraitsGeneratorFactory.GetPlatformTraits(settings.TargetPlatform); platformTraits.PreValidateFileList(filesToZip); using (var archive = CreateZipFile(settings.ZipFile, settings.CompressionLevel, settings.TargetPlatform)) { foreach (var fso in filesToZip) { AddSingleEntry(archive, fso, zipRoot, platformTraits, settings.LogMessage, settings.LogError); } } }