如何使用c#.net将多个不同的文件合并到具有客户文件类型的单个文件中?

时间:2015-06-12 15:36:56

标签: c#

我需要将多个文件(视频和音乐和文字)合并为一个文件,其中包含自定义文件类型(例如:*.abcd)和自定义文件数据结构,该文件只能由我的程序和我的程序应该能够分离这个文件的一部分。 .net& c#

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

2 个答案:

答案 0 :(得分:3)

正如M463正确指出的那样,您可以使用System.IO.Compression将这些文件压缩在一起并加密它们......虽然加密是一种完全不同的艺术而另一种令人头疼的问题。
一个更好的选择是在文件的前几个字节中包含一些元数据,然后存储文件'字节作为原始字节。这样可以避免任何人通过在文本编辑器中查看内容来确定内容。同样,如果您想真正保护您的数据,加密是不可避免的。但是一个简单的算法就是:

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());
            }
        }
    }
}  

现在很明显,这不是您遇到过的最佳算法,也不是最优化的代码片段。毕竟,这正是我在10-15分钟内想出来的。所以我还没有测试过它。但重点是,它给你的想法不是吗?我已将每个文件的大小限制为Int32的最大长度(但是,将其更改为Int64 a.k.a long并不会造成太大麻烦。但它给了你一个想法。您甚至可以通过MemoryStreams修改片段以加载和写入RAM(我认为Systtem.IO.MemorySteam)。但无论如何,这应该会给你一个开始!

答案 1 :(得分:1)

包含文件的加密.zip容器怎么样? .zip文件的处理已在.NET-Framework中提供,请查看library(splitstackshape) out = cbind(dat, setnames(cSplit(dat, "parm_value", "_", fixed = FALSE), c("lower_bound", "upper_bound"))) #> out # parm_value lower_bound upper_bound #1 30_34 30 34 #2 60_64 60 64 #3 65_69 65 69 #4 75_79 75 79 #5 90_94 90 94 命名空间。或者你可以使用一些第三方库。

如果你真的想要,你甚至可以通过重命名文件来强制使用不同的文件扩展名。

相关问题