}
public override void Write(
- byte[] buffer,
- int offset,
- int count
+ byte[] buffer,
+ int offset,
+ int count
)
{
_baseStream.Write(buffer, offset, count);
}
public override int Read(
- byte[] buffer,
- int offset,
- int count
+ byte[] buffer,
+ int offset,
+ int count
)
{
return _baseStream.Read(buffer, offset, count);
}
public override void SetLength(
- long value
+ long value
)
{
_baseStream.SetLength(value);
}
public override long Seek(
- long offset,
- SeekOrigin origin
+ long offset,
+ SeekOrigin origin
)
{
return _baseStream.Seek(offset, origin);
}
}
- // For some strange reason, if the package FileAccess == Read,
- // then can't get length from the stream. Not supported.
- // The workaround - write the stream to a memory stream, then return the length of the
- // memory stream.
public override long Length
{
get {
+ // ZipArchiveEntry's read stream doesn't provide a length since it's a raw DeflateStream
+ // Return length from the archive entry.
if (_packageFileAccess == FileAccess.Read)
- {
- using (MemoryStream ms = new MemoryStream())
- using (Stream zs = _zipArchiveEntry.Open())
- {
- CopyStream(zs, ms);
- return ms.Length;
- }
- }
+ return _zipArchiveEntry.Length;
else
return _baseStream.Length;
}
}
- private static void CopyStream(Stream source, Stream target)
- {
- const int BufSize = 0x4096;
- byte[] buf = new byte[BufSize];
- int bytesRead = 0;
- while ((bytesRead = source.Read(buf, 0, BufSize)) > 0)
- target.Write(buf, 0, bytesRead);
- }
-
public override bool CanSeek
{
get { return _baseStream.CanSeek; }
}
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
}
Assert.Equal(sumLen, 44768);
}
- fiGuidName.Delete();
+ fiGuidName.Delete();
}
private string NL = Environment.NewLine;
{
Package package = Package.Open(tempGuidName.FullName, FileMode.Truncate, FileAccess.ReadWrite);
});
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
{
Package package = Package.Open(tempGuidName.FullName, FileMode.Truncate, FileAccess.Write);
});
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
{
Package package = Package.Open(tempGuidName.FullName, FileMode.Truncate, FileAccess.Read);
});
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
{
Package package = Package.Open(tempGuidName.FullName, FileMode.OpenOrCreate, FileAccess.Write);
});
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
{
Package package = Package.Open(tempGuidName.FullName, FileMode.OpenOrCreate, FileAccess.Read);
});
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
{
Package package = Package.Open(tempGuidName.FullName, FileMode.Open, FileAccess.Write);
});
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(2, xd.DescendantNodes().Count());
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
}
});
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
}
});
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(2, xd.DescendantNodes().Count());
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
}
});
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(2, xd.DescendantNodes().Count());
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
{
Package package = Package.Open(tempGuidName.FullName, FileMode.CreateNew, FileAccess.Write);
});
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
{
Package package = Package.Open(tempGuidName.FullName, FileMode.CreateNew, FileAccess.Read);
});
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
var tempGuidName = GetTempFileInfoWithExtension(".docx");
// opening the package attempts to read the package, and no permissions.
AssertExtensions.Throws<ArgumentException>(null, () => Package.Open(tempGuidName.FullName, FileMode.Create, FileAccess.Write));
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(2, xd.DescendantNodes().Count());
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(2, xd.DescendantNodes().Count());
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(2, xd.DescendantNodes().Count());
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(2, xd.DescendantNodes().Count());
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(2, xd.DescendantNodes().Count());
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
});
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
sw.Write(s_DocumentXml);
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Assert.Equal(0, partStream.Length);
}
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
}
});
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
Stream partStream = packagePartDocument.GetStream(FileMode.Append, FileAccess.Read);
});
}
- tempGuidName.Delete();
+ tempGuidName.Delete();
}
[Fact]
}
}
+ [Fact]
+ [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Desktop doesn't support Package.Open with FileAccess.Write")]
+ public void CreateWithFileAccessWrite()
+ {
+ string[] fileNames = new [] { "file1.txt", "file2.txt", "file3.txt" };
+
+ using (Stream stream = new MemoryStream())
+ {
+ using (Package package = Package.Open(stream, FileMode.Create, FileAccess.Write))
+ {
+ foreach (string fileName in fileNames)
+ {
+ PackagePart part = package.CreatePart(PackUriHelper.CreatePartUri(new Uri(fileName, UriKind.Relative)),
+ System.Net.Mime.MediaTypeNames.Text.Plain,
+ CompressionOption.Fast);
+ using (StreamWriter writer = new StreamWriter(part.GetStream(), Encoding.ASCII))
+ {
+ // just write the filename as content
+ writer.Write(fileName);
+ }
+ }
+ }
+
+ // reopen for read and validate the content
+ stream.Seek(0, SeekOrigin.Begin);
+ using (Package readPackage = Package.Open(stream))
+ {
+ foreach (string fileName in fileNames)
+ {
+ PackagePart part = readPackage.GetPart(PackUriHelper.CreatePartUri(new Uri(fileName, UriKind.Relative)));
+
+ using (Stream partStream = part.GetStream())
+ using (StreamReader reader = new StreamReader(partStream, Encoding.ASCII))
+ {
+ Assert.Equal(fileName.Length, partStream.Length);
+ Assert.Equal(fileName, reader.ReadToEnd());
+ }
+ }
+ }
+ }
+ }
+
+ [Fact]
+ [OuterLoop]
+ public void VeryLargePart()
+ {
+ // FileAccess.Write is important, this tells ZipPackage to open the underlying ZipArchive in
+ // ZipArchiveMode.Create mode as opposed to ZipArchiveMode.Update
+ // When ZipArchive is opened in Create it will write entries directly to the zip stream
+ // When ZipArchive is opened in Update it will write uncompressed data to memory until
+ // the archive is closed.
+ using (Stream stream = new MemoryStream())
+ {
+ Uri partUri = PackUriHelper.CreatePartUri(new Uri("test.bin", UriKind.Relative));
+
+ // should compress *very well*
+ byte[] buffer = new byte[1024 * 1024];
+ for (int i = 0; i < buffer.Length; i++)
+ {
+ buffer[i] = (byte)(i % 2);
+ }
+
+ const long SizeInMb = 6 * 1024; // 6GB
+ long totalLength = SizeInMb * buffer.Length;
+
+ // issue on desktop we cannot use FileAccess.Write on a ZipArchive
+ using (Package package = Package.Open(stream, FileMode.Create, PlatformDetection.IsFullFramework ? FileAccess.ReadWrite : FileAccess.Write))
+ {
+ PackagePart part = package.CreatePart(partUri,
+ System.Net.Mime.MediaTypeNames.Application.Octet,
+ CompressionOption.Fast);
+
+
+ using (Stream partStream = part.GetStream())
+ {
+ for (long i = 0; i < SizeInMb; i++)
+ {
+ partStream.Write(buffer, 0, buffer.Length);
+ }
+ }
+ }
+
+ // reopen for read and make sure we can get the part length & data matches
+ stream.Seek(0, SeekOrigin.Begin);
+ using (Package readPackage = Package.Open(stream))
+ {
+ PackagePart part = readPackage.GetPart(partUri);
+
+ using (Stream partStream = part.GetStream())
+ {
+ Assert.Equal(totalLength, partStream.Length);
+ byte[] readBuffer = new byte[buffer.Length];
+ for (long i = 0; i < SizeInMb; i++)
+ {
+ int actualRead = partStream.Read(readBuffer, 0, readBuffer.Length);
+
+ Assert.Equal(actualRead, readBuffer.Length);
+ Assert.True(buffer.AsSpan().SequenceEqual(readBuffer));
+ }
+ }
+ }
+ }
+ }
+
private const string DocumentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
}