public static void Compress(List <Tuple <string, byte[]> > files, string zipPath) { if (files == null) { throw new ArgumentNullException(nameof(files)); } if (string.IsNullOrEmpty(zipPath)) { throw new ArgumentNullException(nameof(zipPath)); } RetryFile.Delete(zipPath); using (FileStream file = RetryFile.Create(zipPath)) { using (ZipArchive zip = new ZipArchive(file, ZipArchiveMode.Create, true, Encoding.UTF8)) { foreach (Tuple <string, byte[]> tuple in files) { var entry = zip.CreateEntry(tuple.Item1, CompressionLevel.Optimal); using (BinaryWriter writer = new BinaryWriter(entry.Open())) { writer.Write(tuple.Item2); } } } } }
public static void Compress(List <Tuple <string, object> > files, string zipPath) { if (files == null) { throw new ArgumentNullException(nameof(files)); } if (string.IsNullOrEmpty(zipPath)) { throw new ArgumentNullException(nameof(zipPath)); } RetryFile.Delete(zipPath); using (FileStream file = RetryFile.Create(zipPath)) { using (ZipArchive zip = new ZipArchive(file, ZipArchiveMode.Create, true, Encoding.UTF8)) { foreach (Tuple <string, object> tuple in files) { var entry = zip.CreateEntry(tuple.Item1, CompressionLevel.Optimal); Type dataType = tuple.Item2.GetType(); if (dataType == typeof(string)) { using (var stream = entry.Open()) { using (FileStream fs = RetryFile.OpenRead((string)tuple.Item2)) { fs.CopyTo(stream); } } } else if (dataType == typeof(byte[])) { using (BinaryWriter writer = new BinaryWriter(entry.Open())) { writer.Write((byte[])tuple.Item2); } } else { // 暂且忽略错误的参数吧。 } } } } }