Click here to Skip to main content
15,879,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody I am not a professional developer but I like very much vb.net and sometimes i create some application.
Now I would like to to ask for a question:
I am working on an easy application. in this application you can select an mp3 file and one jpg file and associate toghether.
then I would like to save this in one new file with a new extension.
How can I write this file? and most of all, how can I read this file e un-associate the 2 native mp3 and jpg files?

Thank you in advance to everybody

Moved from answer:

Thank you everybody for your answer, maybe I did not exlaplain properly:
Let suppose have 2 files : file name A.mp3 of 2 mb and file B.jpg of 1 mb.
inside an hypothetical my own application I want to create then write a file name C.jxx
which it will be a binary file including files A.mp3 + B.jpg like this scheme:

_______________________________ ___________________________
||||||||| File A.mp3 |||||||||| + ||||||| File B.jpg ||||||||
_____________________________________________________________
||||||||||||||||||||||||| File C.jxx ||||||||||||||||||||||

once the file c.jxx has been written the application must be able to load it
and turn back to original 2 files A.mp3 and B.jpg in order to play the music and View
the pictures.
how would it be possible to do this with Vb.net? or C# but as you not is .Net
some ideas?


Jorkax
Posted
Updated 22-Jan-11 7:16am
v2

You can archive the two files together, i.e. zip, but you will not be able to extract them using native mp3 or image applications as they will not know what the file contains.
 
Share this answer
 
As Mark Nischalke suggested, you can either zip them up together or merely use a binary concatenation to create a single file. At runtime, you can then extract the files as required to a temp directory and then pass them to their final consumers (presumably 3rd party applications).

On the other hand, if your code directly parses both the mp3 and the jpg, you may not need to extract the files since you can easily read the binary stream of data. You'd need to save the file offsets and lengths in your binary archive's header or in a separate file, but that would be quite trivial.
 
Share this answer
 
As Mark has already said you can use one of the many zip libraries to do this, such as SharpZipLib[^], although there are plenty of others if you search for something like free .net zip library.

The added advantage is that you will save a little space by zipping them.

The same library will enable you to extract them when you need to use them.
 
Share this answer
 
Write to a file is easy:
VB
Using writer As New StreamWriter(myOutputFilePath)
    writer.WriteLine("Text to write")
End Using

Outputing two separate vales to a file is just a case of doing two WriteLine operations

Reading it back is easy:
VB
Dim lines As String() = File.ReadAllLines(myOutputFilePath)
If lines.Length >= 2 Then
    Dim myMP3 As String = lines(0)
    Dim myJPG As String = lines(1)
End If


[edit]Just realised: This does not save the data contained in the MP3 and JPG files. Instead, it saves the paths to them. If you need to actually save the file contents, then it is a little (but not too much) more complicated. Very similar set of routines, but you have to be a little more sophisticated. If that is what you want, then ask here. - OriginalGriff[/edit]


"Thank you so much for your answer, but as you said this does not write or save any file,
would you suggest me ho to do it, please?"



There are a huge number of ways to do this.
1) You could use a ZIP as previously suggested.
2) You could create a serializable class, holding holding the contents of an mp3 and a jpg, and let the the framework handle serialization.
3) Do it your self, writing the sizes and file contents to a combined file, and reading them back yourself.
4) Other.

The first option is discussed in other answers.
The second I don't like to use, as it can make future proofing your app difficult: if you change your class, the serialization / deserialization can prove a nightmare.
The third is future proof, but a bit of a pain. Except, there is a middle ground: use a BinaryReader, and a BinaryWriter to make your life easy:
C#
private void butWriteCombined_Click(object sender, EventArgs e)
    {
    byte[] mp3 = File.ReadAllBytes(@"F:\Temp\(Don't Fear) the Reaper.mp3");
    byte[] jpg = File.ReadAllBytes(@"F:\Temp\(Don't Fear) the Reaper.jpg");
    using (BinaryWriter bw = new BinaryWriter(File.Open(@"F:\Temp\(Don't Fear) the Reaper.Combined", FileMode.Create)))
        {
        bw.Write(mp3.Length);
        bw.Write(mp3);
        bw.Write(jpg.Length);
        bw.Write(jpg);
        }
    }
private void ReadCombined_Click(object sender, EventArgs e)
    {
    byte[] mp3;
    byte[] jpg;
    using (BinaryReader br = new BinaryReader(File.Open(@"F:\Temp\(Don't Fear) the Reaper.Combined", FileMode.Open)))
        {
        int mp3Size = br.ReadInt32();
        mp3 = br.ReadBytes(mp3Size);
        int jpgSize = br.ReadInt32();
        jpg = br.ReadBytes(jpgSize);
        }
    File.WriteAllBytes(@"F:\Temp\(Don't Fear) the Reaper Extracted.mp3", mp3);
    File.WriteAllBytes(@"F:\Temp\(Don't Fear) the Reaper Extracted.jpg", jpg);
    }
Obviously, you will want to make the file name user controllable, and probably want to add original file names into teh combined file, but look at MSDN for details of BinaryWriter and BinaryReader - you'll get the idea in no time!
 
Share this answer
 
v3
Comments
jorkax 22-Jan-11 17:10pm    
Thank you so much for your answer, but as you said this does not write or save any file,
would you suggest me ho to do it, please?
OriginalGriff 23-Jan-11 4:08am    
Answer updated
jorkax 23-Jan-11 6:04am    
Thank you very much !!!!
I found very very interesting
I will try it in few hours !!!
Thank you again!!

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