Spa*_*ile 3 c# amazon-s3 asp.net-mvc-5
我正在尝试在我的 C# MVC 项目中编写一个方法,该方法从 S3(或任何地方)流式传输文件,并将其动态压缩为 zip 文件,然后再将压缩流发送给用户。到目前为止,我已经找到了几种通过将流保存到磁盘然后正常返回来从流创建 zip 文件的方法,但我想跳过保存到磁盘并使用缓冲区到流的方法。我正在尝试下载一个非常大的文件(4GB+),该文件很容易压缩到其原始大小的一小部分。
到目前为止,我已经避免了磁盘,但似乎首先将整个文件加载到内存中:
using( var memoryStream = new MemoryStream() )
{
using( var archive = new ZipArchive( memoryStream, ZipArchiveMode.Create, true ) )
{
var zipEntry = archive.CreateEntry( File );
using( var entryStream = zipEntry.Open() )
{
S3.StreamFile( File, Bucket ).CopyTo( entryStream );
}
}
return base.File( memoryStream.ToArray(), "application/zip", File + ".zip" );
}
Run Code Online (Sandbox Code Playgroud)
类似的问题(Creating a ZIP Archive in Memory using System.IO. Compression)仅包含涉及写入磁盘的答案。
ZipArchive 类需要一个提供当前位置的流。TrackablePositionStream下面的类通过在发生写入调用时递增字段来保存位置
public class TrackablePositionStream : Stream
{
private readonly Stream _stream;
private long _position = 0;
public TrackablePositionStream(Stream stream)
{
this._stream = stream;
}
public override void Flush()
{
this._stream.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
this._position += count;
this._stream.Write(buffer, offset, count);
}
public override bool CanRead => this._stream.CanRead;
public override bool CanSeek => this._stream.CanSeek;
public override bool CanWrite => this._stream.CanWrite;
public override long Length => this._stream.Length;
public override long Position
{
get
{
return this._position;
}
set
{
throw new NotImplementedException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的操作方法中使用它:
using( var archive = new ZipArchive(new TrackablePositionStream(response.OutputStream), ZipArchiveMode.Create, true ) )
{
var zipEntry = archive.CreateEntry( File );
using(var entryStream = zipEntry.Open() )
{
S3.StreamFile( File, Bucket ).CopyTo( entryStream );
}
}
return new EmptyResult();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1742 次 |
| 最近记录: |