如何从压缩文件夹中获取文件并进行编码,然后将其保存到数据库中而不提取文件?

时间:2019-05-07 13:21:02

标签: c# .net database encode

服务器具有将近50 GB的压缩文件。我需要一种最佳方法来从这些压缩文件夹中提取文件,并以base 64对其进行编码,然后将它们另存为blob。如果可能,我不希望提取整个压缩文件夹。 请引导我。

1 个答案:

答案 0 :(得分:1)

尝试以下操作:

using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;

namespace zipStream
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var file = File.OpenRead(@"YOUR-REMOTE-FILE-NAME");
            ZipArchive ar = new ZipArchive(file, ZipArchiveMode.Read);
            foreach (ZipArchiveEntry entry in ar.Entries)
            {
                using (Stream stream = entry.Open())
                {
                    try
                    {
                        Byte[] inArray = new Byte[(int)entry.Length];
                        Char[] outArray = new Char[(int)((entry.Length + 10) * 2)];
                        stream.Read(inArray, 0, (int)entry.Length);
                        Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0);
                        Console.WriteLine($"Processed {entry.Name}");
                    }
                    catch (Exception e)
                    {
                        var msg = e.Message;
                        Console.WriteLine($"Failed to process {entry.Name}");
                        System.Diagnostics.Debugger.Break();
                    }
                }
                // at this point you have your file content in outArray variable
                // you can find some guidance on writing blobs to a db here:
                // https://www.c-sharpcorner.com/uploadfile/Ashush/working-with-binary-large-objects-blobs/
            }

        }

    }
}