Unzip a LARGE zip file in Azure File Storage w/o “Out of Memory” exception"Storing state locally with Azure FunctionsHow can I unzip a file to a .NET memory stream?Unzip a zipped file using phpOut of Memory Exception Ionic Zip ExtractionCreate in memory zip from a fileUpload Big ZipArchive-MemoryStream to AzureCreate a Zip from file streams in PHPError uploading .zip file / unzipping using Kudu console in AzureDownload multiple blob files as a zip from azure storageZip and Unzip within MemorystreamConverting zip file to memory stream throws Out of Memory exception
What could prevent players from leaving an island?
Why did the US Airways Flight 1549 passengers stay on the wings?
conditional probability of dependent random variables
What is the reason behind water not falling from a bucket at the top of loop?
How to increase Solr JVM memory
Pronouns when writing from the point of view of a robot
Vectorised way to calculate mean of left and right neighbours in a vector
A Checkmate of Dubious Legality
Need reasons why a satellite network would not work
Plotting Autoregressive Functions / Linear Difference Equations
How can I use commands with sudo without changing owner of the files?
A verb for when some rights are not violated?
Suppressing analytical evaluation in NDSolve
On the consistency of different well-polished astronomy software
What license to choose for my PhD thesis?
How does the 'Brain in a Vat Argument' differ from the 'Simulation Argument'?
What is the difference between "un plan" and "une carte" (in the context of map)?
Would the shaking of an earthquake be visible to somebody in a low-flying aircraft?
…down the primrose path
What does C++ language definition say about the extent of the static keyword?
Can a Hogwarts student refuse the Sorting Hat's decision?
Is it okay to use different fingers every time while playing a song on keyboard? Is it considered a bad practice?
Why do proponents of guns oppose gun competency tests?
Is there a command-line tool for converting html files to pdf?
Unzip a LARGE zip file in Azure File Storage w/o “Out of Memory” exception"
Storing state locally with Azure FunctionsHow can I unzip a file to a .NET memory stream?Unzip a zipped file using phpOut of Memory Exception Ionic Zip ExtractionCreate in memory zip from a fileUpload Big ZipArchive-MemoryStream to AzureCreate a Zip from file streams in PHPError uploading .zip file / unzipping using Kudu console in AzureDownload multiple blob files as a zip from azure storageZip and Unzip within MemorystreamConverting zip file to memory stream throws Out of Memory exception
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Here's what I'm dealing with...
Some process (out of our control) will occasionally drop a zip file into a directory in Azure File Storage. That directory name is InBound
. So let's say a file called bigbook.zip
is dropped into the InBound
folder.
I need to create an Azure Function App that runs every 5 minutes and looks for zip files in the InBound
directory. If any exists, then one-by-one, we create a new directory by the same name as the zip file in another directory (called InProcess
). So in our example, I would create InProcess/bigbook
.
Now inside InProcess/bigbook
, I need to unzip bigbook.zip
. So by the time the process is done running InProcess/bigbook
will contain all the contents of bigbook.zip
.
Please note: This function I am creating is a Console App that will run as an Azure Function App. So there will be no file system access (at least, as far as I'm aware, anyway.) There is no option to download the zip file, unzip it, and then move the contents.
I am having a devil of a time figuring out how to do this in memory only. No matter what I try, I keep running into an Out Of Memory exception. For now, I am just doing this on my localhost running in debug in Visual Studio 2017, .NET 4.7. In that setting, I am not able to convert the test zip file, which is 515,069KB.
This was my first attempt:
private async Task<MemoryStream> GetMemoryStreamAsync(CloudFile inBoundfile)
MemoryStream memstream = new MemoryStream();
await inBoundfile.DownloadToStreamAsync(memstream).ConfigureAwait(false);
return memstream;
And this (with high hopes) was my second attempt, thinking that DownloadRangeToStream
would work better than just DownloadToStream
.
private MemoryStream GetMemoryStreamByRange(CloudFile inBoundfile)
MemoryStream outPutStream = new MemoryStream();
inBoundfile.FetchAttributes();
int bufferLength = 1 * 1024 * 1024;//1 MB chunk
long blobRemainingLength = inBoundfile.Properties.Length;
long offset = 0;
while (blobRemainingLength > 0)
long chunkLength = (long)Math.Min(bufferLength, blobRemainingLength);
using (var ms = new MemoryStream())
inBoundfile.DownloadRangeToStream(ms, offset, chunkLength);
lock (outPutStream)
outPutStream.Position = offset;
var bytes = ms.ToArray();
outPutStream.Write(bytes, 0, bytes.Length);
offset += chunkLength;
blobRemainingLength -= chunkLength;
return outPutStream;
But either way, I am running into memory issues. I presume it's because the MemoryStream I am trying to create gets too large?
How else can I tackle this? And again, downloading the zip file is not an option, as the app will ultimately be an Azure Function App. I'm also pretty sure that using a FileStream isn't an option either, as that requires a local file path, which I don't have. (I only have a remote Azure URL)
Could I somehow create a temp file in the same Azure Storage account that the zip file is in, and stream the zip file to that temp file instead of to a memory stream? (Thinking out loud.)
The goal is to get the stream into a ZipArchive
using:
ZipArchive archive = new ZipArchive(stream)
And from there I can extract all the contents. But getting to that point w/o memory errors is proving a real bugger.
Any ideas?
c# .net azure zip
|
show 4 more comments
Here's what I'm dealing with...
Some process (out of our control) will occasionally drop a zip file into a directory in Azure File Storage. That directory name is InBound
. So let's say a file called bigbook.zip
is dropped into the InBound
folder.
I need to create an Azure Function App that runs every 5 minutes and looks for zip files in the InBound
directory. If any exists, then one-by-one, we create a new directory by the same name as the zip file in another directory (called InProcess
). So in our example, I would create InProcess/bigbook
.
Now inside InProcess/bigbook
, I need to unzip bigbook.zip
. So by the time the process is done running InProcess/bigbook
will contain all the contents of bigbook.zip
.
Please note: This function I am creating is a Console App that will run as an Azure Function App. So there will be no file system access (at least, as far as I'm aware, anyway.) There is no option to download the zip file, unzip it, and then move the contents.
I am having a devil of a time figuring out how to do this in memory only. No matter what I try, I keep running into an Out Of Memory exception. For now, I am just doing this on my localhost running in debug in Visual Studio 2017, .NET 4.7. In that setting, I am not able to convert the test zip file, which is 515,069KB.
This was my first attempt:
private async Task<MemoryStream> GetMemoryStreamAsync(CloudFile inBoundfile)
MemoryStream memstream = new MemoryStream();
await inBoundfile.DownloadToStreamAsync(memstream).ConfigureAwait(false);
return memstream;
And this (with high hopes) was my second attempt, thinking that DownloadRangeToStream
would work better than just DownloadToStream
.
private MemoryStream GetMemoryStreamByRange(CloudFile inBoundfile)
MemoryStream outPutStream = new MemoryStream();
inBoundfile.FetchAttributes();
int bufferLength = 1 * 1024 * 1024;//1 MB chunk
long blobRemainingLength = inBoundfile.Properties.Length;
long offset = 0;
while (blobRemainingLength > 0)
long chunkLength = (long)Math.Min(bufferLength, blobRemainingLength);
using (var ms = new MemoryStream())
inBoundfile.DownloadRangeToStream(ms, offset, chunkLength);
lock (outPutStream)
outPutStream.Position = offset;
var bytes = ms.ToArray();
outPutStream.Write(bytes, 0, bytes.Length);
offset += chunkLength;
blobRemainingLength -= chunkLength;
return outPutStream;
But either way, I am running into memory issues. I presume it's because the MemoryStream I am trying to create gets too large?
How else can I tackle this? And again, downloading the zip file is not an option, as the app will ultimately be an Azure Function App. I'm also pretty sure that using a FileStream isn't an option either, as that requires a local file path, which I don't have. (I only have a remote Azure URL)
Could I somehow create a temp file in the same Azure Storage account that the zip file is in, and stream the zip file to that temp file instead of to a memory stream? (Thinking out loud.)
The goal is to get the stream into a ZipArchive
using:
ZipArchive archive = new ZipArchive(stream)
And from there I can extract all the contents. But getting to that point w/o memory errors is proving a real bugger.
Any ideas?
c# .net azure zip
have you tried using CloudFile? docs.microsoft.com/en-us/dotnet/api/… CloudFile has methods to create, copy and delete. Might do the trick for you.
– Sean
Mar 27 at 2:45
@Sean, thank you, but I'm not sure what you mean I AM using a CloudFile. Both of the methods I put in the OP take a parameter(CloudFile inBoundfile)
. What I need to do is convert that CloudFile to a stream so I can use it in `ZipArchive(stream)'. That's the whole problem I'm having.
– Casey Crookston
Mar 27 at 2:47
I meant the cloud file class. You mentioned you only have the Azure URL. CloudFile has a constructor that takes a URI. You might be able to create a new Cloudfile object from your url then open the cloud file stream.
– Sean
Mar 27 at 2:52
I'm still not sure how that helps my get any closer to my ultimate goal. Maybe I'm not understanding? I have a CloudFile, and what you are saying is that I should use the URL of that CloudFile to create another CloudFile? What I need to do is take an existing CloudFile and convert it to a memory stream.
– Casey Crookston
Mar 27 at 2:54
@CaseyCrookston, it's working fine at my local computer by using DownloadToStream(memory_stream), and the .zip file is around 600M. And it will take up about 600M memory about my local computer during download. Can you just push your code to azure, to see if it's working?
– Ivan Yang
Mar 27 at 9:07
|
show 4 more comments
Here's what I'm dealing with...
Some process (out of our control) will occasionally drop a zip file into a directory in Azure File Storage. That directory name is InBound
. So let's say a file called bigbook.zip
is dropped into the InBound
folder.
I need to create an Azure Function App that runs every 5 minutes and looks for zip files in the InBound
directory. If any exists, then one-by-one, we create a new directory by the same name as the zip file in another directory (called InProcess
). So in our example, I would create InProcess/bigbook
.
Now inside InProcess/bigbook
, I need to unzip bigbook.zip
. So by the time the process is done running InProcess/bigbook
will contain all the contents of bigbook.zip
.
Please note: This function I am creating is a Console App that will run as an Azure Function App. So there will be no file system access (at least, as far as I'm aware, anyway.) There is no option to download the zip file, unzip it, and then move the contents.
I am having a devil of a time figuring out how to do this in memory only. No matter what I try, I keep running into an Out Of Memory exception. For now, I am just doing this on my localhost running in debug in Visual Studio 2017, .NET 4.7. In that setting, I am not able to convert the test zip file, which is 515,069KB.
This was my first attempt:
private async Task<MemoryStream> GetMemoryStreamAsync(CloudFile inBoundfile)
MemoryStream memstream = new MemoryStream();
await inBoundfile.DownloadToStreamAsync(memstream).ConfigureAwait(false);
return memstream;
And this (with high hopes) was my second attempt, thinking that DownloadRangeToStream
would work better than just DownloadToStream
.
private MemoryStream GetMemoryStreamByRange(CloudFile inBoundfile)
MemoryStream outPutStream = new MemoryStream();
inBoundfile.FetchAttributes();
int bufferLength = 1 * 1024 * 1024;//1 MB chunk
long blobRemainingLength = inBoundfile.Properties.Length;
long offset = 0;
while (blobRemainingLength > 0)
long chunkLength = (long)Math.Min(bufferLength, blobRemainingLength);
using (var ms = new MemoryStream())
inBoundfile.DownloadRangeToStream(ms, offset, chunkLength);
lock (outPutStream)
outPutStream.Position = offset;
var bytes = ms.ToArray();
outPutStream.Write(bytes, 0, bytes.Length);
offset += chunkLength;
blobRemainingLength -= chunkLength;
return outPutStream;
But either way, I am running into memory issues. I presume it's because the MemoryStream I am trying to create gets too large?
How else can I tackle this? And again, downloading the zip file is not an option, as the app will ultimately be an Azure Function App. I'm also pretty sure that using a FileStream isn't an option either, as that requires a local file path, which I don't have. (I only have a remote Azure URL)
Could I somehow create a temp file in the same Azure Storage account that the zip file is in, and stream the zip file to that temp file instead of to a memory stream? (Thinking out loud.)
The goal is to get the stream into a ZipArchive
using:
ZipArchive archive = new ZipArchive(stream)
And from there I can extract all the contents. But getting to that point w/o memory errors is proving a real bugger.
Any ideas?
c# .net azure zip
Here's what I'm dealing with...
Some process (out of our control) will occasionally drop a zip file into a directory in Azure File Storage. That directory name is InBound
. So let's say a file called bigbook.zip
is dropped into the InBound
folder.
I need to create an Azure Function App that runs every 5 minutes and looks for zip files in the InBound
directory. If any exists, then one-by-one, we create a new directory by the same name as the zip file in another directory (called InProcess
). So in our example, I would create InProcess/bigbook
.
Now inside InProcess/bigbook
, I need to unzip bigbook.zip
. So by the time the process is done running InProcess/bigbook
will contain all the contents of bigbook.zip
.
Please note: This function I am creating is a Console App that will run as an Azure Function App. So there will be no file system access (at least, as far as I'm aware, anyway.) There is no option to download the zip file, unzip it, and then move the contents.
I am having a devil of a time figuring out how to do this in memory only. No matter what I try, I keep running into an Out Of Memory exception. For now, I am just doing this on my localhost running in debug in Visual Studio 2017, .NET 4.7. In that setting, I am not able to convert the test zip file, which is 515,069KB.
This was my first attempt:
private async Task<MemoryStream> GetMemoryStreamAsync(CloudFile inBoundfile)
MemoryStream memstream = new MemoryStream();
await inBoundfile.DownloadToStreamAsync(memstream).ConfigureAwait(false);
return memstream;
And this (with high hopes) was my second attempt, thinking that DownloadRangeToStream
would work better than just DownloadToStream
.
private MemoryStream GetMemoryStreamByRange(CloudFile inBoundfile)
MemoryStream outPutStream = new MemoryStream();
inBoundfile.FetchAttributes();
int bufferLength = 1 * 1024 * 1024;//1 MB chunk
long blobRemainingLength = inBoundfile.Properties.Length;
long offset = 0;
while (blobRemainingLength > 0)
long chunkLength = (long)Math.Min(bufferLength, blobRemainingLength);
using (var ms = new MemoryStream())
inBoundfile.DownloadRangeToStream(ms, offset, chunkLength);
lock (outPutStream)
outPutStream.Position = offset;
var bytes = ms.ToArray();
outPutStream.Write(bytes, 0, bytes.Length);
offset += chunkLength;
blobRemainingLength -= chunkLength;
return outPutStream;
But either way, I am running into memory issues. I presume it's because the MemoryStream I am trying to create gets too large?
How else can I tackle this? And again, downloading the zip file is not an option, as the app will ultimately be an Azure Function App. I'm also pretty sure that using a FileStream isn't an option either, as that requires a local file path, which I don't have. (I only have a remote Azure URL)
Could I somehow create a temp file in the same Azure Storage account that the zip file is in, and stream the zip file to that temp file instead of to a memory stream? (Thinking out loud.)
The goal is to get the stream into a ZipArchive
using:
ZipArchive archive = new ZipArchive(stream)
And from there I can extract all the contents. But getting to that point w/o memory errors is proving a real bugger.
Any ideas?
c# .net azure zip
c# .net azure zip
edited Mar 27 at 2:50
Casey Crookston
asked Mar 27 at 2:35
Casey CrookstonCasey Crookston
3,7369 gold badges40 silver badges88 bronze badges
3,7369 gold badges40 silver badges88 bronze badges
have you tried using CloudFile? docs.microsoft.com/en-us/dotnet/api/… CloudFile has methods to create, copy and delete. Might do the trick for you.
– Sean
Mar 27 at 2:45
@Sean, thank you, but I'm not sure what you mean I AM using a CloudFile. Both of the methods I put in the OP take a parameter(CloudFile inBoundfile)
. What I need to do is convert that CloudFile to a stream so I can use it in `ZipArchive(stream)'. That's the whole problem I'm having.
– Casey Crookston
Mar 27 at 2:47
I meant the cloud file class. You mentioned you only have the Azure URL. CloudFile has a constructor that takes a URI. You might be able to create a new Cloudfile object from your url then open the cloud file stream.
– Sean
Mar 27 at 2:52
I'm still not sure how that helps my get any closer to my ultimate goal. Maybe I'm not understanding? I have a CloudFile, and what you are saying is that I should use the URL of that CloudFile to create another CloudFile? What I need to do is take an existing CloudFile and convert it to a memory stream.
– Casey Crookston
Mar 27 at 2:54
@CaseyCrookston, it's working fine at my local computer by using DownloadToStream(memory_stream), and the .zip file is around 600M. And it will take up about 600M memory about my local computer during download. Can you just push your code to azure, to see if it's working?
– Ivan Yang
Mar 27 at 9:07
|
show 4 more comments
have you tried using CloudFile? docs.microsoft.com/en-us/dotnet/api/… CloudFile has methods to create, copy and delete. Might do the trick for you.
– Sean
Mar 27 at 2:45
@Sean, thank you, but I'm not sure what you mean I AM using a CloudFile. Both of the methods I put in the OP take a parameter(CloudFile inBoundfile)
. What I need to do is convert that CloudFile to a stream so I can use it in `ZipArchive(stream)'. That's the whole problem I'm having.
– Casey Crookston
Mar 27 at 2:47
I meant the cloud file class. You mentioned you only have the Azure URL. CloudFile has a constructor that takes a URI. You might be able to create a new Cloudfile object from your url then open the cloud file stream.
– Sean
Mar 27 at 2:52
I'm still not sure how that helps my get any closer to my ultimate goal. Maybe I'm not understanding? I have a CloudFile, and what you are saying is that I should use the URL of that CloudFile to create another CloudFile? What I need to do is take an existing CloudFile and convert it to a memory stream.
– Casey Crookston
Mar 27 at 2:54
@CaseyCrookston, it's working fine at my local computer by using DownloadToStream(memory_stream), and the .zip file is around 600M. And it will take up about 600M memory about my local computer during download. Can you just push your code to azure, to see if it's working?
– Ivan Yang
Mar 27 at 9:07
have you tried using CloudFile? docs.microsoft.com/en-us/dotnet/api/… CloudFile has methods to create, copy and delete. Might do the trick for you.
– Sean
Mar 27 at 2:45
have you tried using CloudFile? docs.microsoft.com/en-us/dotnet/api/… CloudFile has methods to create, copy and delete. Might do the trick for you.
– Sean
Mar 27 at 2:45
@Sean, thank you, but I'm not sure what you mean I AM using a CloudFile. Both of the methods I put in the OP take a parameter
(CloudFile inBoundfile)
. What I need to do is convert that CloudFile to a stream so I can use it in `ZipArchive(stream)'. That's the whole problem I'm having.– Casey Crookston
Mar 27 at 2:47
@Sean, thank you, but I'm not sure what you mean I AM using a CloudFile. Both of the methods I put in the OP take a parameter
(CloudFile inBoundfile)
. What I need to do is convert that CloudFile to a stream so I can use it in `ZipArchive(stream)'. That's the whole problem I'm having.– Casey Crookston
Mar 27 at 2:47
I meant the cloud file class. You mentioned you only have the Azure URL. CloudFile has a constructor that takes a URI. You might be able to create a new Cloudfile object from your url then open the cloud file stream.
– Sean
Mar 27 at 2:52
I meant the cloud file class. You mentioned you only have the Azure URL. CloudFile has a constructor that takes a URI. You might be able to create a new Cloudfile object from your url then open the cloud file stream.
– Sean
Mar 27 at 2:52
I'm still not sure how that helps my get any closer to my ultimate goal. Maybe I'm not understanding? I have a CloudFile, and what you are saying is that I should use the URL of that CloudFile to create another CloudFile? What I need to do is take an existing CloudFile and convert it to a memory stream.
– Casey Crookston
Mar 27 at 2:54
I'm still not sure how that helps my get any closer to my ultimate goal. Maybe I'm not understanding? I have a CloudFile, and what you are saying is that I should use the URL of that CloudFile to create another CloudFile? What I need to do is take an existing CloudFile and convert it to a memory stream.
– Casey Crookston
Mar 27 at 2:54
@CaseyCrookston, it's working fine at my local computer by using DownloadToStream(memory_stream), and the .zip file is around 600M. And it will take up about 600M memory about my local computer during download. Can you just push your code to azure, to see if it's working?
– Ivan Yang
Mar 27 at 9:07
@CaseyCrookston, it's working fine at my local computer by using DownloadToStream(memory_stream), and the .zip file is around 600M. And it will take up about 600M memory about my local computer during download. Can you just push your code to azure, to see if it's working?
– Ivan Yang
Mar 27 at 9:07
|
show 4 more comments
2 Answers
2
active
oldest
votes
Using Azure Storage File Share this is the only way it worked for me without loading the entire ZIP into Memory. I tested with a 3GB ZIP File (with thousands of files or with a big file inside) and Memory/CPU was low and stable. I hope it helps!
var zipFiles = _directory.ListFilesAndDirectories()
.OfType<CloudFile>()
.Where(x => x.Name.ToLower().Contains(".zip"))
.ToList();
foreach (var zipFile in zipFiles)
using (var zipArchive = new ZipArchive(zipFile.OpenRead()))
foreach (var entry in zipArchive.Entries)
if (entry.Length > 0)
CloudFile extractedFile = _directory.GetFileReference(entry.Name);
using (var entryStream = entry.Open())
byte[] buffer = new byte[16 * 1024];
using (var ms = extractedFile.OpenWrite(entry.Length))
int read;
while ((read = entryStream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
add a comment |
I would suggest you use memory snapshots to see why you are running out of memory within Visual Studio. You can use the tutorial in this article to find the culprit. Doing local development with a smaller file may help you continue to work if your machine is simply running out of memory.
When it comes to doing this within Azure, a node in the Consumption plan is limited to 1.5GB of total memory. If you expect to receive files larger than that then you should look at one of the other App Service plans that give you more memory to work with.
It is possible to store files within the function's local directory, so that is an option. You can't guaruntee that you will be using the same local directory between executions, but this should work as long as you are using the file you downloaded within the same execution.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368945%2funzip-a-large-zip-file-in-azure-file-storage-w-o-out-of-memory-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using Azure Storage File Share this is the only way it worked for me without loading the entire ZIP into Memory. I tested with a 3GB ZIP File (with thousands of files or with a big file inside) and Memory/CPU was low and stable. I hope it helps!
var zipFiles = _directory.ListFilesAndDirectories()
.OfType<CloudFile>()
.Where(x => x.Name.ToLower().Contains(".zip"))
.ToList();
foreach (var zipFile in zipFiles)
using (var zipArchive = new ZipArchive(zipFile.OpenRead()))
foreach (var entry in zipArchive.Entries)
if (entry.Length > 0)
CloudFile extractedFile = _directory.GetFileReference(entry.Name);
using (var entryStream = entry.Open())
byte[] buffer = new byte[16 * 1024];
using (var ms = extractedFile.OpenWrite(entry.Length))
int read;
while ((read = entryStream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
add a comment |
Using Azure Storage File Share this is the only way it worked for me without loading the entire ZIP into Memory. I tested with a 3GB ZIP File (with thousands of files or with a big file inside) and Memory/CPU was low and stable. I hope it helps!
var zipFiles = _directory.ListFilesAndDirectories()
.OfType<CloudFile>()
.Where(x => x.Name.ToLower().Contains(".zip"))
.ToList();
foreach (var zipFile in zipFiles)
using (var zipArchive = new ZipArchive(zipFile.OpenRead()))
foreach (var entry in zipArchive.Entries)
if (entry.Length > 0)
CloudFile extractedFile = _directory.GetFileReference(entry.Name);
using (var entryStream = entry.Open())
byte[] buffer = new byte[16 * 1024];
using (var ms = extractedFile.OpenWrite(entry.Length))
int read;
while ((read = entryStream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
add a comment |
Using Azure Storage File Share this is the only way it worked for me without loading the entire ZIP into Memory. I tested with a 3GB ZIP File (with thousands of files or with a big file inside) and Memory/CPU was low and stable. I hope it helps!
var zipFiles = _directory.ListFilesAndDirectories()
.OfType<CloudFile>()
.Where(x => x.Name.ToLower().Contains(".zip"))
.ToList();
foreach (var zipFile in zipFiles)
using (var zipArchive = new ZipArchive(zipFile.OpenRead()))
foreach (var entry in zipArchive.Entries)
if (entry.Length > 0)
CloudFile extractedFile = _directory.GetFileReference(entry.Name);
using (var entryStream = entry.Open())
byte[] buffer = new byte[16 * 1024];
using (var ms = extractedFile.OpenWrite(entry.Length))
int read;
while ((read = entryStream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
Using Azure Storage File Share this is the only way it worked for me without loading the entire ZIP into Memory. I tested with a 3GB ZIP File (with thousands of files or with a big file inside) and Memory/CPU was low and stable. I hope it helps!
var zipFiles = _directory.ListFilesAndDirectories()
.OfType<CloudFile>()
.Where(x => x.Name.ToLower().Contains(".zip"))
.ToList();
foreach (var zipFile in zipFiles)
using (var zipArchive = new ZipArchive(zipFile.OpenRead()))
foreach (var entry in zipArchive.Entries)
if (entry.Length > 0)
CloudFile extractedFile = _directory.GetFileReference(entry.Name);
using (var entryStream = entry.Open())
byte[] buffer = new byte[16 * 1024];
using (var ms = extractedFile.OpenWrite(entry.Length))
int read;
while ((read = entryStream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
answered May 31 at 15:43
rGiosarGiosa
1401 gold badge3 silver badges13 bronze badges
1401 gold badge3 silver badges13 bronze badges
add a comment |
add a comment |
I would suggest you use memory snapshots to see why you are running out of memory within Visual Studio. You can use the tutorial in this article to find the culprit. Doing local development with a smaller file may help you continue to work if your machine is simply running out of memory.
When it comes to doing this within Azure, a node in the Consumption plan is limited to 1.5GB of total memory. If you expect to receive files larger than that then you should look at one of the other App Service plans that give you more memory to work with.
It is possible to store files within the function's local directory, so that is an option. You can't guaruntee that you will be using the same local directory between executions, but this should work as long as you are using the file you downloaded within the same execution.
add a comment |
I would suggest you use memory snapshots to see why you are running out of memory within Visual Studio. You can use the tutorial in this article to find the culprit. Doing local development with a smaller file may help you continue to work if your machine is simply running out of memory.
When it comes to doing this within Azure, a node in the Consumption plan is limited to 1.5GB of total memory. If you expect to receive files larger than that then you should look at one of the other App Service plans that give you more memory to work with.
It is possible to store files within the function's local directory, so that is an option. You can't guaruntee that you will be using the same local directory between executions, but this should work as long as you are using the file you downloaded within the same execution.
add a comment |
I would suggest you use memory snapshots to see why you are running out of memory within Visual Studio. You can use the tutorial in this article to find the culprit. Doing local development with a smaller file may help you continue to work if your machine is simply running out of memory.
When it comes to doing this within Azure, a node in the Consumption plan is limited to 1.5GB of total memory. If you expect to receive files larger than that then you should look at one of the other App Service plans that give you more memory to work with.
It is possible to store files within the function's local directory, so that is an option. You can't guaruntee that you will be using the same local directory between executions, but this should work as long as you are using the file you downloaded within the same execution.
I would suggest you use memory snapshots to see why you are running out of memory within Visual Studio. You can use the tutorial in this article to find the culprit. Doing local development with a smaller file may help you continue to work if your machine is simply running out of memory.
When it comes to doing this within Azure, a node in the Consumption plan is limited to 1.5GB of total memory. If you expect to receive files larger than that then you should look at one of the other App Service plans that give you more memory to work with.
It is possible to store files within the function's local directory, so that is an option. You can't guaruntee that you will be using the same local directory between executions, but this should work as long as you are using the file you downloaded within the same execution.
answered Mar 29 at 15:27
SamaraSoucy-MSFTSamaraSoucy-MSFT
1,0071 gold badge1 silver badge12 bronze badges
1,0071 gold badge1 silver badge12 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368945%2funzip-a-large-zip-file-in-azure-file-storage-w-o-out-of-memory-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
have you tried using CloudFile? docs.microsoft.com/en-us/dotnet/api/… CloudFile has methods to create, copy and delete. Might do the trick for you.
– Sean
Mar 27 at 2:45
@Sean, thank you, but I'm not sure what you mean I AM using a CloudFile. Both of the methods I put in the OP take a parameter
(CloudFile inBoundfile)
. What I need to do is convert that CloudFile to a stream so I can use it in `ZipArchive(stream)'. That's the whole problem I'm having.– Casey Crookston
Mar 27 at 2:47
I meant the cloud file class. You mentioned you only have the Azure URL. CloudFile has a constructor that takes a URI. You might be able to create a new Cloudfile object from your url then open the cloud file stream.
– Sean
Mar 27 at 2:52
I'm still not sure how that helps my get any closer to my ultimate goal. Maybe I'm not understanding? I have a CloudFile, and what you are saying is that I should use the URL of that CloudFile to create another CloudFile? What I need to do is take an existing CloudFile and convert it to a memory stream.
– Casey Crookston
Mar 27 at 2:54
@CaseyCrookston, it's working fine at my local computer by using DownloadToStream(memory_stream), and the .zip file is around 600M. And it will take up about 600M memory about my local computer during download. Can you just push your code to azure, to see if it's working?
– Ivan Yang
Mar 27 at 9:07