简体   繁体   English

如何使用c#.net将多个不同文件合并为具有自定义文件类型的单个文件?

[英]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. 我需要将多个文件(视频,音乐和文本)组合成具有自定义文件类型(例如*.abcd )和自定义文件数据结构的单个文件,并且该文件只能由我的程序读取,而我的程序应为能够分隔此文件的各个部分。 How do this in .net & c# ? 如何在.netc#执行此操作?

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. 就像M463正确指出的那样,您可以使用System.IO.Compression将这些文件压缩在一起并对其进行加密...尽管加密是一门完全不同的技巧,但又令人头疼。
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. 毕竟,这正是我能在10-15分钟内得出的结果。 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). 我已经将每个文件的大小限制为Int32的最大长度(但是,将其更改为Int64又名long不会很麻烦)。 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). 您甚至可以修改代码段,以通过MemoryStreams(我认为是Systtem.IO.MemorySteam )在RAM中进行加载和写入。 But whatever, this should give you a start! 但是,无论如何,这应该给您一个开始!

How about an encrypted .zip-container containing your files? 包含文件的加密的.zip容器如何? Handling of .zip-files is already available in the .NET-Framework, take a look at the System.IO.Compression namespace. .NET框架中已经可以处理.zip文件,请查看System.IO.Compression命名空间。 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... 如果您确实要...,甚至可以通过重命名文件来强制使用其他文件扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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