Reputation: 187
I have a function that requires a Filestream as input.
I want to hand several Files to that function which I get from uploaded zip-Files. Is it possible to create the Filestream without extracting the file to a temporary folder?
I imagin something like this:
string path = @"C:\somepathtomyzip";
string filepath = "nameofimagefile"
using (ZipArchive archive = ZipFile.OpenRead(path))
{
ZipArchiveEntry entry = archive.GetEntry(file_path);
//generate Filestream from entry
myFunction(filestreamIneed);
}
Upvotes: 0
Views: 2725
Reputation: 21766
You can use ZipArchiveEntry.Open()
and copy the output from the returned Stream
instance to a FileStream
instance:
using (ZipArchive archive = ZipFile.OpenRead(path))
{
ZipArchiveEntry entry = archive.GetEntry(file_path);
var memoryStream = return entry.Open();
using (var fileStream = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite))
{
memoryStream.CopyTo(fileStream); // fileStream is not populated
}
}
Upvotes: 2