我正在尝试在我的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" );
}
类似的问题(Creating a ZIP Archive in Memory Using System.IO.Compression)仅包含涉及写入磁盘的答案。
答案 0 :(得分:2)
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();
}
}
}
然后在您的操作方法中使用它:
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();