在C#中压缩文件 [英] Compressing file in C#

查看:116
本文介绍了在C#中压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想问一些关于文件的问题

例如mp3文件

当读作二进制数组字节时

为此,我使用算法将字节数组的大小减小为字符串或char后来成为他返回的严重困难?

因为如果我使用霍夫曼算法(例如) ,字节数组应该成为发生什么类型的值

这是我读取和写入文件的代码

hi I wanted to ask few question about files
For example mp3 file
When read as a binary array of bytes
For this I use algorithms to reduce the size of the byte array to a string or char would later become serious difficulties for his return or not?
Because if I use the Hoffman algorithm(for example),the byte array should become to what type of value to make that happen
this is my code for read and write the file


FileStream iFile = new FileStream(@"E:\Work\Project\Compressing\Data\Old Data\01.mp3", FileMode.Open);
                            long lengthInBytes = iFile.Length;
                            BinaryReader bin = new BinaryReader(iFile);
                            byte[] byteArray = bin.ReadBytes((int)lengthInBytes);
                            System.Text.Encoding encEncoder = System.Text.ASCIIEncoding.ASCII;
                            string str = encEncoder.GetString(byteArray);
                            byte[] byteOut = Encoding.ASCII.GetBytes(str);
                            var bw = new BinaryWriter(File.Open(@"E:\Work\Project\Compressing\Data\New Data\01.mp3", FileMode.OpenOrCreate));
                            bw.Write(byteArray);







感谢您抽出宝贵时间/>


我尝试了什么:



i使用该代码进行读写file




Thank you for taking the time

What I have tried:

i used that code for read and write the file

推荐答案

该代码不压缩任何内容!

部分原因是它将输入数据写入输出流而不是byteOut数据,部分是因为即使它确实如此,byteOut仍然包含与byteArray相同的数据...

如果你想用C#压缩数据,那么看看GzipStream类(System.IO.Compression) [ ^ ]对于初学者。
That code doesn't compress anything!
Partly because it writes the input data to the output stream instead of the byteOut data, and partly because even if it did, byteOut contains the same data as byteArray anyway...
If you want to compress data in C#, then look at the GZipStream Class (System.IO.Compression)[^] for starters.


这篇关于在C#中压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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