简体   繁体   English

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

[英]How do I get files from zipped folder and encode and save them to database without extracting the files?

Server has almost 50 GBs of zipped files. 服务器具有将近50 GB的压缩文件。 I need a best approach to extract files from these zipped folder and base 64 encode them and saved them into Database as blob. 我需要一种最佳方法来从这些压缩文件夹中提取文件,并以base 64对其进行编码,然后将它们另存为blob。 I wish not to extract the whole zipped folder if possible. 如果可能,我不希望提取整个压缩文件夹。 Please guide me. 请指导我。

Try the following: 请尝试以下操作:

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/
            }

        }

    }
}

暂无
暂无

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

相关问题 Winforms从datagridview中的文件夹加载文件,编辑名称并保存 - Winforms load files from folder in datagridview, edit name and save them 直接在sql数据库中显示网页中的pdf文件,无需将其保存到服务器文件系统 - Displaying pdf files in a web page from a sql database directly without needing to save them to the server file system 如何获取文件夹中以特定字符串开头的所有文件? - How do I get all files in a folder that start with a particular string? 上传文件并以二进制形式保存在文件夹中或数据库中 - Upload files and save in folder or save in database as binary 如何将ppt文件保存到WPF数据库并检索它们进行编辑? - How can I save ppt files to a WPF database and retrieve them for editing? 如何遍历文件夹中的文件并从关联的“列表”列获取数据? - How do I loop through Files in a Folder and get the data from associated List columns? 如何读取给定文件夹中的所有文件? - How do I read from all files in a given folder? 我如何在 my.exe 中包含批处理文件,而不是在 the.exe 所在的文件夹中需要它们 - How do i include batch files inside of my .exe Instead of needing them in the folder the .exe is located in 如何在不打开文件夹的情况下从文件夹浏览文件 - How to Browse files from folder without open it 如何在未提取的压缩内容上使用反射 - How to use reflection on zipped content without extracting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM