/// <summary>
///
/// </summary>
class MemoryStreamStaticDataSource : ICSharpCode.SharpZipLib.Zip.IStaticDataSource, IDisposable
{
private MemoryStream MemoryStream { get ; set ; }
/// <summary>
/// MemoryStream
/// </summary>
/// <param name="bytes"></param>
public MemoryStreamStaticDataSource( byte [] bytes)
{
MemoryStream = new MemoryStream(bytes) { Position = 0 };
}
#region IStaticDataSource Members
public Stream GetSource()
{
return MemoryStream;
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (MemoryStream != null )
MemoryStream.Dispose();
}
#endregion
}
* This source code was highlighted with Source Code Highlighter .
/// <summary>
/// ,
/// </summary>
/// <returns></returns>
private static byte [] GetFirstFileData()
{
return GetFileData( @" ." );
}
/// <summary>
/// ,
/// </summary>
/// <returns></returns>
private static byte [] GetSecondFileData()
{
return GetFileData( @" ." );
}
private static byte [] GetFileData( string textdata)
{
return Encoding .UTF8.GetBytes(textdata);
}
* This source code was highlighted with Source Code Highlighter .
/// <summary>
/// , zip
/// </summary>
/// <param name="e"></param>
protected override void OnLoad( EventArgs e)
{
base .OnLoad(e);
Response.Clear();
// MIME
Response.ContentType = "application/zip" ;
//, file.zip
Response.AddHeader( "Content-Disposition" , string .Format( "attachment; filename=\"{0}\"" , HttpUtility.UrlEncodeUnicode( "file.zip" )));
// ( ) Response
byte [] bytes = GetZipData();
Response.OutputStream.Write(bytes, 0, bytes.Length);
}
private static byte [] GetZipData()
{
//
using (MemoryStream ms = new MemoryStream())
{
using (ICSharpCode.SharpZipLib.Zip.ZipFile file = new ICSharpCode.SharpZipLib.Zip.ZipFile(ms))
{
file.BeginUpdate();
//
file.Add( new MemoryStreamStaticDataSource(GetFirstFileData()), "file1.txt" );
//
file.Add( new MemoryStreamStaticDataSource(GetSecondFileData()), "file2.txt" );
file.CommitUpdate();
}
return ms.ToArray();
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/56843/
All Articles