c# - 在不使用磁盘的情况下即时压缩流

标签 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" );
}

类似的问题 ( 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();
        }
    }
}

然后在你的操作方法中使用它:

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();

关于c# - 在不使用磁盘的情况下即时压缩流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52376130/

相关文章:

c# - c# 中的奇怪结果

c# - PowerShell 模块是否更接近 C# 中的类的 "thing"?

azure - 负载平衡有状态 Web 应用程序

c# - 如何在我的 ASP.Net MVC 5 应用程序的同一页面中编辑和删除 POST 请求

c# - 拒绝对 asp.net 中除 "~/"路径以外的所有页面匿名

c# - 使用 Newtonsoft.Json 反序列化

amazon-web-services - 基于IP地址的Amazon S3文件访问策略

c# - MVC 5 - 角色 - IsUserInRole 和将用户添加到角色

web-applications - Amazon S3的基于Web的客户端

ruby-on-rails - Rails 4 + Devise + 回形针 + S3 示例?