Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load memoryStream using CopyAsync #58

Open
wants to merge 1 commit into
base: dev-pr
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 66 additions & 33 deletions NanoXLSX/LowLevel/XlsxReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,81 @@ public XlsxReader(Stream stream, ImportOptions options = null)
/// </exception>
public void Read()
{
try
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try block as wrapper is still necessary since FileStream could fail with several other Exceptions than a common IOException (An own version is used anyway --> Exceptions.IOException). The only Exception that is used as interface is this mentioned IOException.
See also: .NET documentation about possible exception for FileStream

using (memoryStream = new MemoryStream())
{
using (memoryStream = new MemoryStream())
ZipArchive zf;
if (inputStream == null && !string.IsNullOrEmpty(filePath))
{
ZipArchive zf;
if (inputStream == null && !string.IsNullOrEmpty(filePath))
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
fs.CopyTo(memoryStream);
}
fs.CopyTo(memoryStream);
}
else if (inputStream != null)
}
else if (inputStream != null)
{
using (inputStream)
{
using (inputStream)
{
inputStream.CopyTo(memoryStream);
}
inputStream.CopyTo(memoryStream);
}
else
}
else
{
throw new IOException("No valid stream or file path was provided to open");
}

memoryStream.Position = 0;
zf = new ZipArchive(memoryStream, ZipArchiveMode.Read);

ReadZip(zf);
}
}


/// <summary>
/// Reads the XLSX file from a file path or a file stream asynchronous
/// </summary>
/// <exception cref="Exceptions.IOException">
/// May throw an IOException in case of an error. The asynchronous operation may hide the exception.
/// </exception>
/// <returns>Task object (void)</returns>
public async Task ReadAsync()
{
using (memoryStream = new MemoryStream())
{
ZipArchive zf;
if (inputStream == null && !string.IsNullOrEmpty(filePath))
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
throw new IOException("No valid stream or file path was provided to open");
await fs.CopyToAsync(memoryStream);
}
}
else if (inputStream != null)
{
using (inputStream)
{
await inputStream.CopyToAsync(memoryStream);
}
}
else
{
throw new IOException("No valid stream or file path was provided to open");
}

memoryStream.Position = 0;
zf = new ZipArchive(memoryStream, ZipArchiveMode.Read);
memoryStream.Position = 0;
zf = new ZipArchive(memoryStream, ZipArchiveMode.Read);

await Task.Run(() =>
{
ReadZip(zf);
}).ConfigureAwait(false);
}
}

private void ReadZip(ZipArchive zf)
{
try
{
MemoryStream ms;

SharedStringsReader sharedStrings = new SharedStringsReader(importOptions);
Expand Down Expand Up @@ -152,29 +201,13 @@ public void Read()
{
throw new IOException("No worksheet was found in the workbook");
}
}
}
catch (Exception ex)
{
throw new IOException("There was an error while reading an XLSX file. Please see the inner exception:", ex);
}
}

/// <summary>
/// Reads the XLSX file from a file path or a file stream asynchronous
/// </summary>
/// <exception cref="Exceptions.IOException">
/// May throw an IOException in case of an error. The asynchronous operation may hide the exception.
/// </exception>
/// <returns>Task object (void)</returns>
public async Task ReadAsync()
{
await Task.Run(() =>
{
Read();
}).ConfigureAwait(false);
}

/// <summary>
/// Resolves the workbook with all worksheets from the loaded file
/// </summary>
Expand Down