forked from winsw/winsw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download.cs
executable file
·61 lines (54 loc) · 1.69 KB
/
Download.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace winsw
{
/// <summary>
/// Specify the download activities prior to the launch.
/// This enables self-updating services.
/// </summary>
public class Download
{
public readonly string From;
public readonly string To;
internal Download(string f, string t)
{
From = f;
To = t;
if (From == null)
{
throw new ArgumentNullException("from", "Download creation requires a from location.");
}
if (To == null)
{
throw new ArgumentNullException("to", "Download creation requires a to location.");
}
}
public void Perform()
{
WebRequest req = WebRequest.Create(From);
WebResponse rsp = req.GetResponse();
FileStream tmpstream = new FileStream(To + ".tmp", FileMode.Create);
CopyStream(rsp.GetResponseStream(), tmpstream);
// only after we successfully downloaded a file, overwrite the existing one
if (File.Exists(To))
File.Delete(To);
File.Move(To + ".tmp", To);
}
private static void CopyStream(Stream i, Stream o)
{
byte[] buf = new byte[8192];
while (true)
{
int len = i.Read(buf, 0, buf.Length);
if (len <= 0) break;
o.Write(buf, 0, len);
}
i.Close();
o.Close();
}
}
}