问题
I have a zip file within a zip file and inside the inner zip file I have an Excel and a PDF/Tif document.
I'm currently developing a tool that reads all these files and store them in the database. Before I used to store both files on a folder first, but since the zip files holds loads of files I received an out of disk space exception.
So now I want to try to store the files in the database directly without knowing the folder location.
// Path.Combine(exportPathNoZip,entry.FullName) --> \unzip\Files1\Files1.ZIP
using (ZipArchive archive = ZipFile.OpenRead(Path.Combine(exportPathNoZip,entry.FullName)))
{
// These are the files inside the zip file and contain the Excel and the image
foreach (ZipArchiveEntry item in archive.Entries)
{
// Save image to database
if (item.FullName.EndsWith(".tif", StringComparison.OrdinalIgnoreCase) ||
item.FullName.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase) ||
item.FullName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
{
byte[] file = File.ReadAllBytes(?????);
_formerRepository.InsertFormerFileAsBinary(file);
}
}
}
But this will not work because the File.ReadAllBytes()
will require a path. So what can I do? The storing the files locally will give me a out of disk space exception and without it I have no path.
Please be aware that I'd like to use the System.IO.Compression
library for this only.
回答1:
Given that
_formerRepository.InsertFormerFileAsBinary
takes byte[], then this should work:
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
item.Open().CopyTo(ms);
_formerRepository.InsertFormerFileAsBinary(ms.ToArray());
}
And for reference: Creating a byte array from a stream
来源:https://stackoverflow.com/questions/40651417/how-to-store-a-pdf-within-a-zip-file-without-saving-it-locally