Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
C#

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



Thank you for taking the time

What I have tried:

i used that code for read and write the file
Posted
Updated 11-Apr-16 22:29pm

1 solution

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.
   
Comments
mj01001 12-Apr-16 4:40am
   
no sorry it's my first post i want use the hoffman algorithm for compressing but i dont know how to use the byte array to do that
i just need to know that
OriginalGriff 12-Apr-16 4:48am
   
Well, for starters, your existing code can be a lot smaller:

byte[] data = File.ReadAllBytes(path);

and

File.WriteAllBytes(path, data);
Replaces all of your code! :laugh:

Once you have the data, it gets a little more complex, but there are loads of examples on the net that Google can find.
Here's one on MSDN:
https://code.msdn.microsoft.com/Building-a-Huffman-f29b3e81
phil.o 12-Apr-16 4:57am
   
Even with a Zip stream, I seriously doubt a worthy compression ratio will be obtained with a mp3 file :)
OriginalGriff 12-Apr-16 5:24am
   
Normally, I'd agree. But ... I just tried with a random 10.3MB MP3, and got it down to 9.3 using "legacy" compression.
10% isn't too bad, and could be significant.
mj01001 12-Apr-16 4:57am
   
tnx can i have your gmail?
if i have porblem!
OriginalGriff 12-Apr-16 5:25am
   
:laugh:
No!
I don't use gmail - I have my own domain - and you can talk to people as we are at the moment, with the advantage that it may help others with a similar problem, not just you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900