[转][C#]压缩解压缩类 GZipStream

本文来自:https://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream(v=vs.100).aspx

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);

                    }
                }
            }
        }

    }
}
时间: 2024-07-30 23:55:44

[转][C#]压缩解压缩类 GZipStream的相关文章

hadoop的压缩解压缩,reduce端join,map端join

hadoop的压缩解压缩 hadoop对于常见的几种压缩算法对于我们的mapreduce都是内置支持,不需要我们关心.经过map之后,数据会产生输出经过shuffle,这个时候的shuffle过程特别需要消耗网络资源,它传输的数据量越少,对作业的运行时间越有意义,在这种情况下,我们可以对输出进行一个压缩.输出压缩之后,reducer就要接收,然后再解压,reducer处理完之后也需要做输出,也可以做压缩.对于我们程序而言,输入的压缩是我们原来的,不是程序决定的,因为输入源就是这样子,reduce

Qt之zip压缩/解压缩(QuaZIP)

摘要: 简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它工具打开. 简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它工具打开. Qt中提供了qCompress/qUncompress来进行文件的压缩与解压,但存在

大数据技术之压缩解压缩案例

7.10 压缩/解压缩案例 7.10.1 对数据流的压缩和解压缩 CompressionCodec有两个方法可以用于轻松地压缩或解压缩数据.要想对正在被写入一个输出流的数据进行压缩,我们可以使用createOutputStream(OutputStreamout)方法创建一个CompressionOutputStream,将其以压缩格式写入底层的流.相反,要想对从输入流读取而来的数据进行解压缩,则调用createInputStream(InputStreamin)函数,从而获得一个Compres

谢烟客---------Linux之压缩解压缩及归档工具的使用

压缩工具 compress/uncompress:  .Z结尾 gzip/gunzip: .gz结尾 bzip2/bunzip2: .bz2结尾 xz/unxz: .xz后缀 压缩及归档工具: zip/unzip 归档工具: tar,cpio(特殊场景) gzip/gunzip/zcat命令 1.LZ77压缩算法 2.压缩比不高:快速.高效 3.压缩/解压缩后删除原文件,为了节约空间 4.纯文本压缩,二进制压缩意义不大 5.压缩目录,-r gzip [OPTIONS...] FILE     -

Linux压缩解压缩(unzip,tar)

unzip tar 常用解压缩命令: tar -zxvpf:解压缩 tar -zcvpf: 压缩 # tar [-j|-z] [cv] [-f 建立的檔名] filename... <==打包与压缩 # tar [-j|-z] [tv] [-f 建立的檔名]             <==察看檔名 # tar [-j|-z] [xv] [-f 建立的檔名] [-C 目录]   <==解压缩 参数: -z:透过gzip的支持进行压缩/解压缩,此时档名最好为*.tar.gz. -j:透过bz

2016-8-28 压缩解压缩及归档 while脚本

文件管理命令――压缩解压缩及归档基本工具 压缩.解压缩命令 压缩格式:gz, bz2, xz, zip, Z 压缩算法:算法不同,压缩比也会不同: 早期    压缩:        compress(压缩比很小): FILENAME.Z ―― 压缩后的文件名    解压:        uncompress gzip.bzip2.xz只能压缩文件,并且默认压缩完成后删除源文件,zip可以压缩目录 gzip: .gz    gzip /PATH/TO/SOMEFILE:压缩完成后会删除原文件   

【linux_笔记】Linux_文件管理命令—压缩解压缩及归档基本工具

学习资源来自:www.magedu.com 学习记录过程中难免出现错误,如有发现,还望大神们指出. 示例操作部分有的与历史操作有关,如果先前的示例操作没有执行过的话,可能会有部分示例的操作无法执行.示例仅供参考.(示例见附件) 文件管理命令--压缩解压缩及归档基本工具 压缩.解压缩命令 压缩格式:gz, bz2, xz, zip, Z 压缩算法:算法不同,压缩比也会不同: 早期    压缩:        compress(压缩比很小): FILENAME.Z -- 压缩后的文件名    解压:

基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// <summary> /// 压缩 /// </summary> /// <param name="sourceDirectory"></param> /// <param name="targetZipName"></param> /// <param name=&qu

Linux常用命令(第二版) --压缩解压缩命令

压缩解压缩命令: ----------.gz---------- 1.压缩 gzip[GNU zip]: /bin/gzip 格式: gzip 选项 [文件] #压缩文件,压缩后扩展名为.gz,Linux下最常用 #比较常见的软件源代码包的格式 特点: 1)只能压缩文件,不能压缩目录 2)不保留原文件 e.g. gzip newfile #原newfile删除 2.解压 1)gunzip[GNU zip]: /bin/gunzip 2)gzip -d #解压.gz E.g. gzip -d ne