-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompression.cs
224 lines (211 loc) · 9.21 KB
/
Compression.cs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.IO;
using System.IO.Compression;
using System.Xml;
using Cake.Common.Diagnostics;
using Cake.Common.IO;
using Cake.Core;
using Cake.Core.IO;
namespace Dnn.CakeUtils
{
public static class Compression
{
public static void AddBinaryFileToZip(this ICakeContext context, FilePath zipFile, byte[] content, string name)
{
context.AddBinaryFileToZip(zipFile, content, name, false);
}
public static void AddBinaryFileToZip(this ICakeContext context, FilePath zipFile, byte[] content, string name, bool append)
{
using (var sr = new MemoryStream(content))
{
context.AddStreamToZip(zipFile, sr, name, append);
}
}
public static void AddXmlFileToZip(this ICakeContext context, FilePath zipFile, XmlDocument content, string name)
{
context.AddXmlFileToZip(zipFile, content, name, false);
}
public static void AddXmlFileToZip(this ICakeContext context, FilePath zipFile, XmlDocument content, string name, bool append)
{
using (var ms = new MemoryStream())
{
var writer = new XmlTextWriter(ms, System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
content.WriteContentTo(writer);
writer.Flush();
ms.Flush();
ms.Position = 0;
context.AddStreamToZip(zipFile, ms, name, append);
}
}
public static void AddTextFileToZip(this ICakeContext context, FilePath zipFile, string content, string name)
{
context.AddTextFileToZip(zipFile, content, name, false);
}
public static void AddTextFileToZip(this ICakeContext context, FilePath zipFile, string content, string name, bool append)
{
using (var sr = context.GenerateStreamFromString(content))
{
context.AddStreamToZip(zipFile, sr, name, append);
}
}
public static void AddFilesToZip(this ICakeContext context, FilePath zipFile, FilePathCollection files)
{
context.AddFilesToZip(zipFile, ".", "", files, false);
}
public static void AddFilesToZip(this ICakeContext context, FilePath zipFile, FilePathCollection files, bool append)
{
context.AddFilesToZip(zipFile, ".", "", files, append);
}
public static void AddFilesToZip(this ICakeContext context, FilePath zipFile, DirectoryPath start, FilePathCollection files)
{
context.AddFilesToZip(zipFile, start, "", files, false);
}
public static void AddFilesToZip(this ICakeContext context, FilePath zipFile, DirectoryPath start, FilePathCollection files, bool append)
{
context.AddFilesToZip(zipFile, start, "", files, append);
}
public static void AddFilesToZip(this ICakeContext context, FilePath zipFile, DirectoryPath start, string newStart, FilePathCollection files)
{
context.AddFilesToZip(zipFile, start, newStart, files, false);
}
public static void AddFilesToZip(this ICakeContext context, FilePath zipFile, DirectoryPath start, string newStart, FilePathCollection files, bool append)
{
zipFile = context.MakeAbsolute(zipFile);
if (!append)
{
if (context.FileExists(zipFile))
{
context.DeleteFile(zipFile);
}
}
if (newStart != "")
{
newStart = newStart.EnsureEndsWith("/");
}
var rootPath = context.MakeAbsolute(start).FullPath;
foreach (var file in files)
{
context.Information("Reading " + file.FullPath);
using (var fileToZip = new FileStream(file.FullPath, FileMode.Open, FileAccess.Read))
{
var pathInZip = newStart == ""
? file.FullPath.Substring(rootPath.Length + 1)
: newStart + file.FullPath.Substring(rootPath.Length + 1);
context.AddStreamToZip(zipFile, fileToZip, pathInZip, true);
}
}
}
public static void AddStreamToZip(this ICakeContext context, FilePath zipFile, Stream fileToZip, string name)
{
context.AddStreamToZip(zipFile, fileToZip, name, false);
}
public static void AddStreamToZip(this ICakeContext context, FilePath zipFile, Stream fileToZip, string name, bool append)
{
zipFile = context.MakeAbsolute(zipFile);
if (!append)
{
if (context.FileExists(zipFile))
{
context.DeleteFile(zipFile);
}
}
using (var outFile = File.Open(zipFile.FullPath, FileMode.OpenOrCreate))
{
using (var outStream = new ZipArchive(outFile, append ? ZipArchiveMode.Update : ZipArchiveMode.Create))
{
context.Information("Zipping " + name);
var entry = outStream.CreateEntry(name);
fileToZip.CopyTo(entry.Open());
}
}
}
public static Stream ZipToStream(this ICakeContext context, DirectoryPath root, FilePathCollection files)
{
var rootPath = context.MakeAbsolute(root).FullPath;
var outStream = new MemoryStream();
using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true))
{
foreach (var file in files)
{
context.Information("Zipping " + file.FullPath);
var fileInArchive = archive.CreateEntry(file.FullPath.Substring(rootPath.Length + 1), CompressionLevel.Optimal);
using (var entryStream = fileInArchive.Open())
using (var fileToCompressStream = new FileStream(file.FullPath, FileMode.Open, FileAccess.Read))
{
fileToCompressStream.CopyTo(entryStream);
}
}
}
return outStream;
}
public static byte[] ZipToBytes(this ICakeContext context, DirectoryPath root, FilePathCollection files)
{
var rootPath = context.MakeAbsolute(root).FullPath;
byte[] compressedBytes;
using (var outStream = new MemoryStream())
{
using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true))
{
foreach (var file in files)
{
context.Information("Zipping " + file.FullPath);
var fileInArchive = archive.CreateEntry(file.FullPath.Substring(rootPath.Length + 1), CompressionLevel.Optimal);
using (var entryStream = fileInArchive.Open())
using (var fileToCompressStream = new FileStream(file.FullPath, FileMode.Open, FileAccess.Read))
{
fileToCompressStream.CopyTo(entryStream);
}
}
}
compressedBytes = outStream.ToArray();
}
return compressedBytes;
}
public static void AddFile(this ICakeContext context, FilePath sourcePath, Stream zip)
{
using (FileStream src = new FileStream(context.MakeAbsolute(sourcePath).FullPath, FileMode.Open, FileAccess.Read))
{
src.CopyToAsync(zip);
}
}
public static void CopyStream(this ICakeContext context, Stream source, Stream destination, int bufferSize)
{
byte[] bBuffer = new byte[bufferSize + 1];
int iLengthOfReadChunk;
do
{
iLengthOfReadChunk = source.Read(bBuffer, 0, bufferSize);
destination.Write(bBuffer, 0, iLengthOfReadChunk);
if (iLengthOfReadChunk == 0)
{
break;
}
}
while (true);
}
public static MemoryStream GenerateStreamFromString(this ICakeContext context, string value)
{
return new MemoryStream(System.Text.Encoding.UTF8.GetBytes(value ?? ""));
}
public static void SaveStream(this ICakeContext context, Stream input, FilePath filePath)
{
if (input.CanSeek)
{
input.Seek(0, SeekOrigin.Begin);
}
using (var outFile = new FileStream(context.MakeAbsolute(filePath).FullPath, FileMode.OpenOrCreate, FileAccess.Write))
{
input.CopyTo(outFile);
}
}
public static void SaveBytes(this ICakeContext context, byte[] input, FilePath filePath)
{
using (var outFile = new FileStream(context.MakeAbsolute(filePath).FullPath, FileMode.OpenOrCreate, FileAccess.Write))
using (var fileToSave = new MemoryStream(input))
{
fileToSave.CopyTo(outFile);
}
}
}
}