基本文件的I/O --压缩文件

使用 GZipStream 类压缩和解压缩数据。下面的代码示例在当前目录中创建一个文件 (test.txt),在其中添加文本并将文件的内容显示到控制台。然后代码使用 GZipStream 类创建该文件的压缩版本 (test.txt.gz) 并比较两个文件的大小。最后,代码读取压缩的文件,将其解压并向当前目录写出一个新文件 (test.txt.gz.txt)。代码然后显示解压缩文件内容也可以使用 DeflateStream 类压缩和解压缩数据。

        public static void Main(string[] args)
        {
            string path = "test.txt";
            // Create the text file if it doesn't already exist.
            if (!File.Exists(path))
            {
                Console.WriteLine("Creating a new test.txt file");
                string[] text = new string[] {"This is a test text file.",
                    "This file will be compressed and written to the disk.",
                    "Once the file is written, it can be decompressed",
                    "using various compression tools.",
                    "The GZipStream and DeflateStream class use the same",
                    "compression algorithms, the primary difference is that",
                    "the GZipStream class includes a cyclic redundancy check",
                    "that can be useful for detecting data corruption.",
                    "One other side note: both the GZipStream and DeflateStream",
                    "classes operate on streams as opposed to file-based",
                    "compression; data is read on a byte-by-byte basis, so it",
                    "is not possible to perform multiple passes to determine the",
                    "best compression method. Already compressed data can actually",
                    "increase in size if compressed with these classes."};

                File.WriteAllLines(path, text);

                //Console.ReadKey();
            }

            Console.WriteLine("Contents of {0}", path);
            Console.WriteLine(File.ReadAllText(path));

            CompressFile(path);//压缩
            Console.WriteLine();

            UncompressFile(path + ".gz");//解压
            Console.WriteLine();

            Console.WriteLine("Contents of {0}", path + ".gz.txt");
            Console.WriteLine(File.ReadAllText(path + ".gz.txt"));
            Console.ReadKey();
        }


        public static void CompressFile(string path)
        {
            FileStream sourceFile = File.OpenRead(path);
            FileStream destinationFile = File.Create(path + ".gz");

            byte[] buffer = new byte[sourceFile.Length];
            sourceFile.Read(buffer, 0, buffer.Length);

            using (GZipStream output = new GZipStream(destinationFile,
                CompressionMode.Compress))
            {
                Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
                    destinationFile.Name, false);

                output.Write(buffer, 0, buffer.Length);
            }

            // Close the files.
            sourceFile.Close();
            destinationFile.Close();
        }

        public static void UncompressFile(string path)
        {
            FileStream sourceFile = File.OpenRead(path);
            FileStream destinationFile = File.Create(path + ".txt");

            // Because the uncompressed size of the file is unknown, 
            // we are using an arbitrary buffer size.
            byte[] buffer = new byte[4096];
            int n;

            using (GZipStream input = new GZipStream(sourceFile,
                CompressionMode.Decompress, false))
            {
                Console.WriteLine("Decompressing {0} to {1}.", sourceFile.Name,
                    destinationFile.Name);


                n = input.Read(buffer, 0, buffer.Length);
                destinationFile.Write(buffer, 0, n);
            }

            // Close the files.
            sourceFile.Close();
            destinationFile.Close();
        }

       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个命令是一个编译 `ffplay.c` 文件的命令,其中包含了 FFmpeg 库和 SDL 库的头文件路径和库文件路径。 具体来说,`-I` 选项用于指定头文件路径,`-L` 选项用于指定库文件路径,`-l` 选项用于指定需要链接的库文件名。下面是这个命令中的各个选项的含义: - `-I../libavformat`: 指定了 FFmpeg 中 `libavformat` 库的头文件路径。 - `-I../libavcodec`: 指定了 FFmpeg 中 `libavcodec` 库的头文件路径。 - `-I../libavutil`: 指定了 FFmpeg 中 `libavutil` 库的头文件路径。 - `-I../libswresample`: 指定了 FFmpeg 中 `libswresample` 库的头文件路径。 - `-I../libswscale`: 指定了 FFmpeg 中 `libswscale` 库的头文件路径。 - `-I../libavfilter`: 指定了 FFmpeg 中 `libavfilter` 库的头文件路径。 - `-L../libavcodec`: 指定了 FFmpeg 中 `libavcodec` 库的库文件路径。 - `-L../libavformat`: 指定了 FFmpeg 中 `libavformat` 库的库文件路径。 - `-L../libavutil`: 指定了 FFmpeg 中 `libavutil` 库的库文件路径。 - `-L../libswresample`: 指定了 FFmpeg 中 `libswresample` 库的库文件路径。 - `-L../libswscale`: 指定了 FFmpeg 中 `libswscale` 库的库文件路径。 - `-L../libavfilter`: 指定了 FFmpeg 中 `libavfilter` 库的库文件路径。 - `-lavformat`: 指定了需要链接的 FFmpeg 中 `libavformat` 库的名称。 - `-lavcodec`: 指定了需要链接的 FFmpeg 中 `libavcodec` 库的名称。 - `-lswresample`: 指定了需要链接的 FFmpeg 中 `libswresample` 库的名称。 - `-lswscale`: 指定了需要链接的 FFmpeg 中 `libswscale` 库的名称。 - `-lavutil`: 指定了需要链接的 FFmpeg 中 `libavutil` 库的名称。 - `-lavfilter`: 指定了需要链接的 FFmpeg 中 `libavfilter` 库的名称。 - `-lm`: 指定需要链接的数学库。 - `-lz`: 指定需要链接的压缩库。 - `-pthread`: 指定需要链接的线程库。 - `-lSDL2`: 指定需要链接的 SDL2 库的名称。 这个命令的作用是将 `ffplay.c` 编译成可执行文件 `ffplay`,并链接上 FFmpeg 和 SDL 库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值