[英]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.