Skip to content

Commit

Permalink
Add TempFileZipPackage
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhoholiev committed Jan 3, 2024
1 parent ede8a68 commit 06df646
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 0 deletions.
9 changes: 9 additions & 0 deletions openxml4Net/OPC/Internal/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public static String GetFilename(string filepath)
return "";
}

public static FileStream GetTempFileStream()
{
string fileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

return File.Create(
fileName,
8192,
FileOptions.DeleteOnClose | FileOptions.Asynchronous);
}
}

}
70 changes: 70 additions & 0 deletions openxml4Net/OPC/Internal/TempFilePackagePart.cs
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 openxml4Net/OPC/Internal/TempFilePackagePartOutputStream.cs
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);
}
}
33 changes: 33 additions & 0 deletions openxml4Net/OPC/TempFileZipPackage.cs
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;
}
}
}

0 comments on commit 06df646

Please sign in to comment.