简体   繁体   中英

How do I merge several different files into a single file with custome file type using c#.net?

I need to combine multiple files (video & music & text) into single file with a custom file type (for example: *.abcd ) and custom file data structure , and the file should only be read by my program and my program should be able to separate parts of this file. How do this in .net & c# ?

https://www.imageupload.co.uk/image/ZWnh

Like M463 rightly pointed out, you could use System.IO.Compression to compress those files together and encrypt them...although encryption is a completely different art and another headache.
A better option would be to have some metadata in, say, the first few bytes of the file and then store the files' bytes as raw bytes. This would avoid any person from figuring the contents by just looking at it in a text editor. Again, if you want to really protect your data, encryption is unavoidable. But a simple algorithm to begin with would be this:

using System;
using System.IO;
using System.Collections.Generic;

namespace Compression
{
    public class ClassName
    {
        public static void Compress(string[] fileNames, string resultantFileName)
        {
            List<byte> bytesToWrite = new List<byte>();

            //add metadata about the number of files
            int filesLength = fileNames.Length;
            bytesToWrite.AddRange(BitConverter.GetBytes(filesLength));

            List<byte[]> files = new List<byte[]>();
            foreach(string fileName in fileNames)
            {
                byte[] bytes = File.ReadAllBytes(fileName);
                //add metadata about the size of each file
                bytesToWrite.AddRange(BitConverter.GetBytes(bytes.Length));
                files.Add(bytes);
            }
            foreach(byte[] bytes in files)
            {
                //write the actual files itself
                bytesToWrite.AddRange(bytes);
            }
            File.WriteAllBytes(resultantFileName, bytesToWrite.ToArray());
        }

        public static void Decompress(string fileName)
        {
            List<byte> bytes = new List<byte>(File.ReadAllBytes(fileName));
            //this int represents the number of files in the byte array
            int filesLength = BitConverter.ToInt32(bytes.ToArray(), 0);

            List<int> sizes = new List<int>();
            //get the size of each file
            for(int i = 0; i < filesLength; i++)
            {
                //the first 2 bytes represent the number of files
                //then each succeding int represents the size of each file
                int size = BitConverter.ToInt32(bytes.ToArray(), 2 + i * 2);
                sizes.Add(size);
            }

            //now read all the files
            for(int i = 0; i < filesLength; i++)
            {
                int lastByteTillNow = 0;
                for(int j = 0; j < i; j++)
                    lastByteTillNow += sizes[j];
                File.WriteAllBytes("file " + i, bytes.GetRange(2 + 2 * filesLength + lastByteTillNow, sizes[i]).ToArray());
            }
        }
    }
}  

Now obviously this is not the best algorithm you've come across nor is it the most optimized snippet of code. After all, it is just what I could come up with in 10-15 mins. So I haven't even tested it yet. However, the point is, it gives you the idea doesn't it? I have limited the size of each file to the maximum length of an Int32 (however, changing it to Int64 aka long wouldnt be much of a trouble). But it gives you an idea. You can even modify the snippet to load and write to and from the RAM via MemoryStreams ( Systtem.IO.MemorySteam I think). But whatever, this should give you a start!

How about an encrypted .zip-container containing your files? Handling of .zip-files is already available in the .NET-Framework, take a look at the System.IO.Compression namespace. Or you could use some third party library.

You could even force a different file extension by just renaming the file, if you really want to...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM