在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System.IO; using System.IO.Compression; using System.Text; class Program { static void Main() { try { // 1.Starting file is 26,747 bytes. string anyString = File.ReadAllText("TextFile1.txt"); // 2.Output file is 7,388 bytes. CompressStringToFile("new.gz", anyString); } catch { // Couldn't compress. } } public static void CompressStringToFile(string fileName, string value) { // A.Write string to temporary file. string temp = Path.GetTempFileName(); File.WriteAllText(temp, value); // B. Read file into byte array buffer. byte[] b; using (FileStream f = new FileStream(temp, FileMode.Open)) { b = new byte[f.Length]; f.Read(b, 0, (int)f.Length); } // C. Use GZipStream to write compressed bytes to target file. using (FileStream f2 = new FileStream(fileName, FileMode.Create)) using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false)) { gz.Write(b, 0, b.Length); } } } form:http://www.dotnetperls.com/gzipstream |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论