首页 新闻 会员 周边 捐助

C#文件流操作

0
悬赏园豆:20 [已解决问题] 解决于 2018-04-10 11:44

已知多个文件内容(字符串),怎么将它们压缩成一个文件流

C#
yjcd的主页 yjcd | 初学一级 | 园豆:61
提问于:2018-03-19 11:12
< >
分享
最佳答案
0

  上面感觉有点复杂,直接读取多个文件的字符串再合并感觉简单很多。既然楼主告诉已知多个文件内容,我介绍一下压缩操作。

  代码如下:

static void Main(string[] args)
        {
            //1.获取从文件中读取的文本(假如文字如下)
            const string input = "测试文字:你好";

            //2.压缩
            var compressResult = GZip.Compress(input);

            //2.1 显示压缩结果
            Console.WriteLine(compressResult);

            //3.解压
            var decompressResult = GZip.Decompress(compressResult);

            //3.1 显示解压结果
            Console.WriteLine(decompressResult);

            Console.Read();
        }

  一个助手类封装 GZip.cs

public class GZip
    {
        /// <summary>
        ///     压缩
        /// </summary>
        /// <param name="text">文本</param>
        public static string Compress(string text)
        {
            var buffer = Encoding.UTF8.GetBytes(text);
            return Convert.ToBase64String(Compress(buffer));
        }

        /// <summary>
        ///     解压缩
        /// </summary>
        /// <param name="text">文本</param>
        public static string Decompress(string text)
        {
            var buffer = Convert.FromBase64String(text);
            using (var ms = new MemoryStream(buffer))
            {
                using (var zip = new GZipStream(ms, CompressionMode.Decompress))
                {
                    using (var reader = new StreamReader(zip))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }

        /// <summary>
        ///     压缩
        /// </summary>
        /// <param name="buffer">字节流</param>
        public static byte[] Compress(byte[] buffer)
        {
            if (buffer == null)
                return null;
            using (var ms = new MemoryStream())
            {
                using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    zip.Write(buffer, 0, buffer.Length);
                }

                return ms.ToArray();
            }
        }
    }

  可以看到,Compress() 和 Decompress() 就使用了流进行操作。

  结果如下:

收获园豆:20
反骨仔 | 菜鸟二级 |园豆:444 | 2018-03-20 11:46
其他回答(2)
0
/// <summary>
/// 合并文件
/// </summary>
/// <param name="strDirectory"></param>
/// <param name="strMergeResultFile"></param>     
private void MergeFiles(string strInputDirectory, string strMergeResultFile)
{
    string[] arrFileNames = Directory.GetFiles(strInputDirectory);
    int iSumFile = arrFileNames.Length;
    //progressBar1.Maximum = iSumFile;
    FileStream AddStream = new FileStream(strMergeResultFile + "_temp", FileMode.OpenOrCreate);
    BinaryWriter AddWriter = new BinaryWriter(AddStream);
 
    long firstFileLength = 0;
    FileStream TempStream = null;
    BinaryReader TempReader = null;
    //文件长度列表
    string allFileLength = "";
    //文件长度和文件内容叠加
    {
        for (int i = 0; i < iSumFile; i++)
        {
            TempStream = new FileStream(arrFileNames[i], FileMode.Open);
            TempReader = new BinaryReader(TempStream);
            AddWriter.Write(TempReader.ReadBytes((int)TempStream.Length));
            if (firstFileLength == 0)
            {
                firstFileLength = TempReader.BaseStream.Length;
                allFileLength = firstFileLength.ToString().PadLeft(20, '0');
            }
            //暂时只处理2个文件的情况,下面是多文件
            //allFileLength += "," + TempReader.BaseStream.Length.ToString().PadLeft(20, '0');
 
            TempReader.Close();
            TempStream.Close();
        }
        allFileLength = allFileLength.Trim(',');
 
        //释放资源
        AddWriter.Flush();
        AddWriter.Close();
        AddStream.Close();
        TempStream = null;
        TempReader = null;
    }
    //创建含有头信息的文件- 也达到了加密的效果
    {
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        byte[] BytesMessage = UTF8.GetBytes(allFileLength);
        AddStream = new FileStream(strMergeResultFile + "_r", FileMode.OpenOrCreate);
        AddWriter = new BinaryWriter(AddStream);
        //文件头
        AddWriter.Write(BytesMessage);
 
        //写入原来文件
        TempStream = new FileStream(strMergeResultFile, FileMode.Open);
        TempReader = new BinaryReader(TempStream);
        AddWriter.Write(TempReader.ReadBytes((int)TempStream.Length));
        long cc = TempReader.BaseStream.Length;
 
        TempReader.Close();
        AddWriter.Flush();
        AddWriter.Close();
        TempStream.Close();
        //释放
        TempReader = null;
        AddWriter = null;
        TempStream = null;
    }
    //删掉临时
    File.Delete(strMergeResultFile + "_temp");
}
悟行 | 园豆:12559 (专家六级) | 2018-03-19 11:43
0

qdqwdqw

yjcd | 园豆:61 (初学一级) | 2018-03-20 11:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册