如何从压缩文件夹中获取文件并对其进行编码并将其保存到数据库而不解压缩文件?
How do I get files from zipped folder and encode and save them to database without extracting the files?
服务器有将近 50 GB 的压缩文件。我需要一种最佳方法来从这些压缩文件夹中提取文件,然后对它们进行 base 64 编码并将它们作为 blob 保存到数据库中。如果可能的话,我不希望提取整个压缩文件夹。
请指导我。
尝试以下操作:
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/
}
}
}
}
服务器有将近 50 GB 的压缩文件。我需要一种最佳方法来从这些压缩文件夹中提取文件,然后对它们进行 base 64 编码并将它们作为 blob 保存到数据库中。如果可能的话,我不希望提取整个压缩文件夹。 请指导我。
尝试以下操作:
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/
}
}
}
}