博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转][C#]压缩解压缩类 GZipStream
阅读量:4322 次
发布时间:2019-06-06

本文共 2979 字,大约阅读时间需要 9 分钟。

本文来自:
using System;using System.IO;using System.IO.Compression;namespace zip{    public class Program    {        public static void Main()        {            // Path to directory of files to compress and decompress.            string dirpath = @"c:\users\public\reports";            DirectoryInfo di = new DirectoryInfo(dirpath);            // Compress the directory's files.            foreach (FileInfo fi in di.GetFiles())            {                Compress(fi);            }            // Decompress all *.gz files in the directory.            foreach (FileInfo fi in di.GetFiles("*.gz"))            {                Decompress(fi);            }        }        public static void Compress(FileInfo fi)        {            // Get the stream of the source file.            using (FileStream inFile = fi.OpenRead())            {                // Prevent compressing hidden and                 // already compressed files.                if ((File.GetAttributes(fi.FullName)                     & FileAttributes.Hidden)                    != FileAttributes.Hidden & fi.Extension != ".gz")                {                    // Create the compressed file.                    using (FileStream outFile =                                 File.Create(fi.FullName + ".gz"))                    {                        using (GZipStream Compress =                             new GZipStream(outFile,                             CompressionMode.Compress))                        {                            // Copy the source file into                             // the compression stream.                        inFile.CopyTo(Compress);                            Console.WriteLine("Compressed {0} from {1} to {2} bytes.",                                fi.Name, fi.Length.ToString(), outFile.Length.ToString());                        }                    }                }            }        }        public static void Decompress(FileInfo fi)        {            // Get the stream of the source file.            using (FileStream inFile = fi.OpenRead())            {                // Get original file extension, for example                // "doc" from report.doc.gz.                string curFile = fi.FullName;                string origName = curFile.Remove(curFile.Length -                         fi.Extension.Length);                //Create the decompressed file.                using (FileStream outFile = File.Create(origName))                {                    using (GZipStream Decompress = new GZipStream(inFile,                            CompressionMode.Decompress))                    {                        // Copy the decompression stream                         // into the output file.                        Decompress.CopyTo(outFile);                                                Console.WriteLine("Decompressed: {0}", fi.Name);                    }                }            }        }    }}

 

转载于:https://www.cnblogs.com/z5337/p/7015640.html

你可能感兴趣的文章
js遍历Object所有属性
查看>>
再也不学AJAX了!(三)跨域获取资源 ③ - WebSocket & postMessage
查看>>
pycharm设置python文件颜色
查看>>
不换行输出的两种方式
查看>>
贵在坚持不懈
查看>>
hdu 1251 统计难题
查看>>
java中关于String 类型数据 的存储方式
查看>>
javascript中的with语句
查看>>
常用设计模式:装饰者模式
查看>>
python接口自动化--get请求
查看>>
ajax 上传文件
查看>>
lintcode-easy-Flatten Binary Tree to Linked List
查看>>
从远程队列中读取消息
查看>>
typescript 接口的新认识
查看>>
java常见面试题及三大框架面试
查看>>
懒惰的肥兔博文导读
查看>>
[db] mongodb 存取修改简单小实例
查看>>
面试百题003——求子数组的最大和
查看>>
jq.validate 自定义验证两个日期
查看>>
公布一个以前写的随机数生成的方法
查看>>