forked from branstraub/gzip-release
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cs
More file actions
executable file
·57 lines (50 loc) · 1.63 KB
/
Utility.cs
File metadata and controls
executable file
·57 lines (50 loc) · 1.63 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
using Microsoft.WindowsAzure.Storage.Queue;
namespace gzip
{
class Utility
{
public async Task EnsureGzipFiles(CloudBlobContainer containerS, CloudBlobContainer containerD, string fileName)
{
var blobInfo = containerS.GetBlobReference(fileName);
await Upload(blobInfo, containerD);
}
public async Task Upload(IListBlobItem blobInfo, CloudBlobContainer containerD)
{
var blob = (CloudBlob)blobInfo;
byte[] compressedBytes;
using (var memoryStream = new MemoryStream())
{
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
using (var blobStream = await blob.OpenReadAsync())
{
await blobStream.CopyToAsync(gzipStream);
}
compressedBytes = memoryStream.ToArray();
}
containerD.CreateIfNotExistsAsync().Wait();
var destinationBlob = containerD.GetBlockBlobReference(blob.Name);
if (await destinationBlob.ExistsAsync())
{
//Console.WriteLine($"file exists {blob.Name}");
return;
}
// Upload the compressed bytes to the new blob
await destinationBlob.UploadFromByteArrayAsync(compressedBytes, 0, compressedBytes.Length);
// Set the blob headers
destinationBlob.Properties.ContentType = blob.Properties.ContentType;
destinationBlob.Properties.ContentEncoding = "gzip";
destinationBlob.SetProperties();
}
}
}