Skip to content
Merged
Show file tree
Hide file tree
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
38 changes: 22 additions & 16 deletions EPPlus/Packaging/ZipPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,31 @@ internal ZipPackage(Stream stream)
{
var rels = new Dictionary<string, string>();
stream.Seek(0, SeekOrigin.Begin);
using (ZipInputStream zip = new ZipInputStream(stream))
using (stream)
using (ZipFile zip = ReadZipFile(stream))
{
var e = zip.GetNextEntry();
if(e==null)
var firstEntry = zip.Entries.FirstOrDefault();
if (firstEntry == null)
{
throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor."));
}
if (e.FileName.Contains("\\"))
if (firstEntry.FileName.Contains("\\"))
{
_dirSeparator = '\\';
}
else
{
_dirSeparator = '/';
}
while (e != null)
foreach (var e in zip)
{
if (e.UncompressedSize > 0)
{
var b = new byte[e.UncompressedSize];
var size = zip.Read(b, 0, (int)e.UncompressedSize);
using (var entryStream = new MemoryStream(b))
{
e.Extract(entryStream);
}
if (e.FileName.Equals("[content_types].xml", StringComparison.OrdinalIgnoreCase))
{
AddContentTypes(Encoding.UTF8.GetString(b));
Expand All @@ -141,10 +145,6 @@ internal ZipPackage(Stream stream)
}
}
}
else
{
}
e = zip.GetNextEntry();
}

foreach (var p in Parts)
Expand All @@ -169,16 +169,22 @@ internal ZipPackage(Stream stream)
{
throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor."));
}
if (!hasContentTypeXml)
{
throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor."));
}
zip.Close();
zip.Dispose();
}
}
}

private static ZipFile ReadZipFile(Stream stream)
{
try
{
return ZipFile.Read(stream);
}
catch (ZipException ex)
{
throw new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.", ex);
}
}

private void AddContentTypes(string xml)
{
var doc = new XmlDocument();
Expand Down
4 changes: 4 additions & 0 deletions EPPlusTest/EPPlusTest.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<WarningsAsErrors>$(WarningsAsErrors);CA1416</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<None Update="Workbooks\test-zip64-descriptor.xlsx" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\BitmapImage.gif" />
<None Remove="Resources\Test1.jpg" />
Expand Down
38 changes: 35 additions & 3 deletions EPPlusTest/ExcelPackageTests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace EPPlusTest
{
[TestClass]
public class ExcelPackageTests
{

[TestMethod]
public void ReadWorkbookWithZip64DataDescriptors()
{
var path = Path.Combine(AppContext.BaseDirectory, "Workbooks", "test-zip64-descriptor.xlsx");
using (var stream = File.OpenRead(path))
using (var package = new ExcelPackage(stream))
{
Assert.AreEqual(1, package.Workbook.Worksheets.Count);
var worksheet = package.Workbook.Worksheets["Sheet1"];
Assert.IsNotNull(worksheet);
Assert.AreEqual("test", worksheet.Cells["A1"].Value);
Assert.AreEqual("A1", worksheet.Dimension.Address);
}
}

[TestMethod]
public void SaveAndReloadWorkbookWithZip64DataDescriptors()
{
var path = Path.Combine(AppContext.BaseDirectory, "Workbooks", "test-zip64-descriptor.xlsx");
using (var input = File.OpenRead(path))
using (var output = new MemoryStream())
{
using (var package = new ExcelPackage(input))
{
package.SaveAs(output);
}
output.Position = 0;
using (var package = new ExcelPackage(output))
{
Assert.AreEqual(1, package.Workbook.Worksheets.Count);
Assert.AreEqual("test", package.Workbook.Worksheets["Sheet1"].Cells["A1"].Value);
}
Assert.IsTrue(input.CanRead, "Loading the package must leave the caller's input stream open.");
}
}
}
}
Binary file added EPPlusTest/Workbooks/test-zip64-descriptor.xlsx
Binary file not shown.
Loading