forked from nissl-lab/npoi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ede8a68
commit 06df646
Showing
4 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.IO; | ||
using NPOI.OpenXml4Net.OPC.Internal.Marshallers; | ||
|
||
namespace NPOI.OpenXml4Net.OPC.Internal; | ||
|
||
public class TempFilePackagePart : PackagePart | ||
{ | ||
internal Stream _data; | ||
|
||
public TempFilePackagePart(OPCPackage pack, PackagePartName partName, string contentType) | ||
: base(pack, partName, contentType) | ||
{ | ||
} | ||
|
||
public TempFilePackagePart(OPCPackage pack, PackagePartName partName, string contentType, bool loadRelationships) | ||
: base(pack, partName, new ContentType(contentType), loadRelationships) | ||
{ | ||
} | ||
|
||
protected override Stream GetInputStreamImpl() | ||
{ | ||
FileStream stream = FileHelper.GetTempFileStream(); | ||
|
||
if (_data is null) | ||
{ | ||
return stream; | ||
} | ||
|
||
StreamHelper.CopyStream(_data, stream); | ||
stream.Position = 0; | ||
|
||
return stream; | ||
} | ||
|
||
protected override Stream GetOutputStreamImpl() | ||
{ | ||
return new TempFilePackagePartOutputStream(this); | ||
} | ||
|
||
public override long Size => _data?.Length ?? 0; | ||
|
||
public override void Clear() | ||
{ | ||
_data?.Dispose(); | ||
_data = null; | ||
} | ||
|
||
public override bool Save(Stream zos) | ||
{ | ||
return new ZipPartMarshaller().Marshall(this, zos); | ||
} | ||
|
||
public override bool Load(Stream ios) | ||
{ | ||
// Save it | ||
StreamHelper.CopyStream(ios, _data); | ||
// All done | ||
return true; | ||
} | ||
|
||
public override void Close() | ||
{ | ||
// Do nothing | ||
} | ||
|
||
public override void Flush() | ||
{ | ||
// Do nothing | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
openxml4Net/OPC/Internal/TempFilePackagePartOutputStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.IO; | ||
using System; | ||
|
||
namespace NPOI.OpenXml4Net.OPC.Internal; | ||
|
||
public sealed class TempFilePackagePartOutputStream : Stream | ||
{ | ||
private readonly Stream _buff; | ||
|
||
public TempFilePackagePartOutputStream(TempFilePackagePart part) | ||
{ | ||
part._data = FileHelper.GetTempFileStream(); | ||
_buff = part._data; | ||
} | ||
|
||
public override bool CanRead => false; | ||
|
||
public override bool CanWrite => true; | ||
|
||
public override bool CanSeek => false; | ||
|
||
public override long Length => _buff.Length; | ||
|
||
public override long Position | ||
{ | ||
get => _buff.Position; | ||
set => _buff.Position = value; | ||
} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
_buff.SetLength(value); | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
return _buff.Seek(offset, origin); | ||
} | ||
|
||
/** | ||
* Close this stream and flush the content. | ||
* @see #flush() | ||
*/ | ||
public override void Close() | ||
{ | ||
Flush(); | ||
} | ||
|
||
/** | ||
* Flush this output stream. This method is called by the close() method. | ||
* Warning : don't call this method for output consistency. | ||
* @see #close() | ||
*/ | ||
public override void Flush() | ||
{ | ||
_buff.Flush(); | ||
|
||
/* | ||
* Clear this streams buffer, in case flush() is called a second time | ||
* Fix bug 1921637 - provided by Rainer Schwarze | ||
*/ | ||
_buff.Position = 0; | ||
} | ||
|
||
public void Write(int b) | ||
{ | ||
_buff.WriteByte((byte)b); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
_buff.Write(buffer, offset, count); | ||
} | ||
|
||
public void Write(byte[] buffer) | ||
{ | ||
_buff.Write(buffer, (int)_buff.Position, buffer.Length); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using NPOI.OpenXml4Net.Exceptions; | ||
using System.IO; | ||
using NPOI.OpenXml4Net.OPC.Internal; | ||
|
||
namespace NPOI.OpenXml4Net.OPC; | ||
|
||
public class TempFileZipPackage : ZipPackage | ||
{ | ||
public TempFileZipPackage() | ||
{ | ||
} | ||
|
||
public TempFileZipPackage(Stream in1, PackageAccess access) | ||
: base(in1, access) | ||
{ | ||
} | ||
|
||
protected override PackagePart CreatePartImpl(PackagePartName partName, string contentType, bool loadRelationships) | ||
{ | ||
try | ||
{ | ||
return new TempFilePackagePart( | ||
this, | ||
partName, | ||
contentType, | ||
loadRelationships); | ||
} | ||
catch (InvalidFormatException) | ||
{ | ||
return null; | ||
} | ||
} | ||
} |