GZipStream到FileStream:空输出 [英] GZipStream to FileStream : empty output

查看:69
本文介绍了GZipStream到FileStream:空输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在下面的代码中遇到了一个奇怪的问题.
我在自定义类中有2种方法:一种将数据从文件压缩到字节数组,另一种将数据从字节数组(属性fileContent)解压缩到文件.压缩数据在所有情况下都可以正常工作,而解压缩则不能.

I'm encountering a curious issue with the code below.
I have 2 methods in a custom class : one to compress data from a file to a byte array and another one to decompress data from a byte array (the property fileContent) to a file. Compressing data works fine in all cases while decompressing does not.

奇怪的是,当我尝试解压缩小文件(通常是小文本文件...)时,输出为空.使用更大的文件,我可以获得原始文件.

Curiously, when I try to decompress small files (typically small text files ...), the output is empty. With bigger files, I can get the original file.

根据您的说法,我做错了吗?

According to you, did I made a mistake ?

        public void FromFileSystem(string filePath)
        {
            using (var fs_input = File.Open(filePath, FileMode.Open))
            using (var ms_output = new MemoryStream())
            using (var gzipStream = new GZipStream(ms_output, CompressionMode.Compress))
            {
                fs_input.CopyTo(gzipStream);
                //NormalSize = fs_input.Length;
                //Filename = Path.GetFileName(fs_input.Name);
                FileContent = ms_output.ToArray();
                /*CompressedSize = FileContent.Length;
                Gzip = true;
                if (!D_created.HasValue) D_created = File.GetCreationTime(fs_input.Name);
                else D_edited = DateTime.Now;
                if (Creator != null) Creator = MyAccount.CurrentUser;
                else Editor = MyAccount.CurrentUser;*/
                fs_input.Close();
            }
        }

        public void ToFileSystem(string filePath)
        {
            using (var ms_input = new MemoryStream(fileContent))
            using (var fs_output = File.Create(filePath))
            using (var gzipStream = new GZipStream(ms_input, CompressionMode.Decompress))
            {
                gzipStream.CopyTo(fs_output);
            }
        }


在此先感谢您的帮助


Thanks in advance for your help

推荐答案

我想我自己找到了解决方案.

I think I just found the solution myself.

这是方法 FromFileSystem的更正版本:

        public void FromFileSystem(string filePath)
        {
            using (var fs_input = File.Open(filePath, FileMode.Open))
            using (var ms_output = new MemoryStream())
            {
                using (var gzipStream = new GZipStream(ms_output, CompressionMode.Compress)) fs_input.CopyTo(gzipStream);
                NormalSize = fs_input.Length;
                Filename = Path.GetFileName(fs_input.Name);
                FileContent = ms_output.ToArray();
                CompressedSize = FileContent.Length;
                Gzip = true;
                if (!D_created.HasValue) D_created = File.GetCreationTime(fs_input.Name);
                else D_edited = DateTime.Now;
                if (Creator != null) Creator = MyAccount.CurrentUser;
                else Editor = MyAccount.CurrentUser;
            }
        }

必须先关闭GzipStream,然后才能从中获取字节.

The GzipStream must be closed before getting the bytes from it.

希望对别人有帮助.


这篇关于GZipStream到FileStream:空输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆