C#下载远程文件并打包
C#下载远程文件并打包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using System; using System.IO; using System.IO.Compression; using System.Net; class Program { static void Main() { string [] files = { "http://www.xxx.com/xx1.xls" , "http://www.xxx.com/xx2.xls" , }; string output = "files.zip" ; using (FileStream newZipFile = new FileStream(output, FileMode.Create)) { using (ZipArchive zipArchive = new ZipArchive(newZipFile, ZipArchiveMode.Create)) { foreach ( string fileURL in files) { string fileName = Path.GetFileName(fileURL); byte [] fileBytes = DownloadFile(fileURL); if (fileBytes != null ) { ZipArchiveEntry zipEntry = zipArchive.CreateEntry(fileName, CompressionLevel.Fastest); using (Stream entryStream = zipEntry.Open()) { entryStream.Write(fileBytes, 0, fileBytes.Length); } } } } } Console.WriteLine( "Zipped File: " + output); } static byte [] DownloadFile( string url) { using (WebClient client = new WebClient()) { try { return client.DownloadData(url); } catch (Exception ex) { Console.WriteLine(ex.Message); return null ; } } } } |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决