forked from extnet/Ext.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipResult.cs
More file actions
98 lines (83 loc) · 3.2 KB
/
ZipResult.cs
File metadata and controls
98 lines (83 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace Ext.Net.MVC.Examples
{
public class ZipResult : ActionResult
{
private List<FileInfo> files;
private string fileName;
private string folder;
public ZipResult(List<FileInfo> files, string fileName)
{
this.files = files;
this.fileName = fileName;
}
public ZipResult(string folder, string fileName)
{
this.folder = folder;
this.fileName = fileName;
}
private ZipOutputStream oZipStream;
public override void ExecuteResult(ControllerContext context)
{
var hContext = context.HttpContext;
hContext.Response.ContentType = "application/octet-stream";
hContext.Response.AppendHeader("Connection", "keep-alive");
hContext.Response.AppendHeader("Content-Disposition", string.Concat(" attachment; filename = ", fileName, ".zip"));
FileStream ostream;
byte[] obuffer;
oZipStream = new ZipOutputStream(hContext.Response.OutputStream);
oZipStream.SetLevel(6);
ZipEntry oZipEntry;
if (this.folder != null)
{
int folderOffset = this.folder.Length + (this.folder.EndsWith("\\") ? 0 : 1);
this.CompressFolder(this.folder, folderOffset);
}
else
{
foreach (FileInfo file in this.files)
{
oZipEntry = new ZipEntry(file.Name);
oZipStream.PutNextEntry(oZipEntry);
ostream = File.OpenRead(file.FullName);
obuffer = new byte[ostream.Length];
ostream.Read(obuffer, 0, obuffer.Length);
oZipStream.Write(obuffer, 0, obuffer.Length);
}
}
oZipStream.Finish();
oZipStream.Close();
hContext.Response.End();
}
protected void CompressFolder(string folder, int folderOffset)
{
DirectoryInfo di = new DirectoryInfo(folder);
foreach (DirectoryInfo item in di.GetDirectories())
{
this.CompressFolder(item.FullName, folderOffset);
}
foreach (FileInfo item in di.GetFiles())
{
if (item.Name == "config.xml")
{
continue;
}
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string entryName = item.FullName.Substring(folderOffset);
entryName = ZipEntry.CleanName(entryName);
ZipEntry entry = new ZipEntry(entryName);
this.oZipStream.PutNextEntry(entry);
this.oZipStream.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
}
}