Skip to content

Commit 8d396ba

Browse files
Added compress and download util classes
1 parent 3c38284 commit 8d396ba

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

samples/csharp/common/Compress.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using ICSharpCode.SharpZipLib.Core;
2+
using ICSharpCode.SharpZipLib.GZip;
3+
using ICSharpCode.SharpZipLib.Tar;
4+
using System;
5+
using System.IO;
6+
using System.IO.Compression;
7+
using System.Linq;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace Common
12+
{
13+
public class Compress
14+
{
15+
public static void ExtractGZip(string gzipFileName, string targetDir)
16+
{
17+
// Use a 4K buffer. Any larger is a waste.
18+
byte[] dataBuffer = new byte[4096];
19+
20+
using (System.IO.Stream fs = new FileStream(gzipFileName, FileMode.Open, FileAccess.Read))
21+
{
22+
using (GZipInputStream gzipStream = new GZipInputStream(fs))
23+
{
24+
// Change this to your needs
25+
string fnOut = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(gzipFileName));
26+
27+
using (FileStream fsOut = File.Create(fnOut))
28+
{
29+
StreamUtils.Copy(gzipStream, fsOut, dataBuffer);
30+
}
31+
}
32+
}
33+
}
34+
35+
public static void UnZip(String gzArchiveName, String destFolder)
36+
{
37+
var flag = gzArchiveName.Split(Path.DirectorySeparatorChar).Last().Split('.').First() + ".bin";
38+
if (File.Exists(Path.Combine(destFolder, flag))) return;
39+
40+
Console.WriteLine($"Extracting.");
41+
var task = Task.Run(() =>
42+
{
43+
ZipFile.ExtractToDirectory(gzArchiveName, destFolder);
44+
});
45+
46+
while (!task.IsCompleted)
47+
{
48+
Thread.Sleep(200);
49+
Console.Write(".");
50+
}
51+
52+
File.Create(Path.Combine(destFolder, flag));
53+
Console.WriteLine("");
54+
Console.WriteLine("Extracting is completed.");
55+
}
56+
57+
public static void ExtractTGZ(String gzArchiveName, String destFolder)
58+
{
59+
var flag = gzArchiveName.Split(Path.DirectorySeparatorChar).Last().Split('.').First() + ".bin";
60+
if (File.Exists(Path.Combine(destFolder, flag))) return;
61+
62+
Console.WriteLine($"Extracting.");
63+
var task = Task.Run(() =>
64+
{
65+
using (var inStream = File.OpenRead(gzArchiveName))
66+
{
67+
using (var gzipStream = new GZipInputStream(inStream))
68+
{
69+
using (TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream))
70+
tarArchive.ExtractContents(destFolder);
71+
}
72+
}
73+
});
74+
75+
while (!task.IsCompleted)
76+
{
77+
Thread.Sleep(200);
78+
Console.Write(".");
79+
}
80+
81+
File.Create(Path.Combine(destFolder, flag));
82+
Console.WriteLine("");
83+
Console.WriteLine("Extracting is completed.");
84+
}
85+
}
86+
}

samples/csharp/common/Web.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace Common
11+
{
12+
public class Web
13+
{
14+
public static bool Download(string url, string destDir, string destFileName)
15+
{
16+
if (destFileName == null)
17+
destFileName = url.Split(Path.DirectorySeparatorChar).Last();
18+
19+
Directory.CreateDirectory(destDir);
20+
21+
string relativeFilePath = Path.Combine(destDir, destFileName);
22+
23+
if (File.Exists(relativeFilePath))
24+
{
25+
Console.WriteLine($"{relativeFilePath} already exists.");
26+
return false;
27+
}
28+
29+
var wc = new WebClient();
30+
Console.WriteLine($"Downloading {relativeFilePath}");
31+
var download = Task.Run(() => wc.DownloadFile(url, relativeFilePath));
32+
while (!download.IsCompleted)
33+
{
34+
Thread.Sleep(1000);
35+
Console.Write(".");
36+
}
37+
Console.WriteLine("");
38+
Console.WriteLine($"Downloaded {relativeFilePath}");
39+
40+
return true;
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)