public partial class Form1 : Form
{
private Thread DownloadThread;
public Form1()
{
InitializeComponent();
Directory.CreateDirectory( Path.GetDirectoryName( Application.ExecutablePath ) + "\\maps\\" );
}
private void toolStripButton1_Click( object sender, EventArgs e )
{
listBox1.Items.Clear();
int start, end;
Int32.TryParse( ToolStripTextBoxFromID.Text, out start );
Int32.TryParse( ToolStripTextBoxEndID.Text, out end );
ToolStripProgressBar.Minimum = start;
ToolStripProgressBar.Maximum = end;
ToolStripProgressBar.Value = start;
DownloadThread = new Thread( new ThreadStart( DownloadMaps ) );
DownloadThread.Start();
}
public string NewItem
{
set
{
this.Invoke( (MethodInvoker)delegate
{
this.listBox1.Items.Add( value );
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
this.listBox1.SelectedIndex = -1;
} );
}
}
public int Progress
{
set
{
this.Invoke( (MethodInvoker)delegate { this.ToolStripProgressBar.Value = value; } );
this.Invoke( (MethodInvoker)delegate { this.ToolStripLabelCurrentID.Text = value.ToString(); } );
}
}
public void DownloadMaps()
{
Int64 StartID, EndID;
Int64.TryParse( ToolStripTextBoxFromID.Text, out StartID );
Int64.TryParse( ToolStripTextBoxEndID.Text, out EndID );
string url = "...";
HttpWebRequest req = null;
HttpWebResponse res = null;
string dir = Path.GetDirectoryName( Application.ExecutablePath ) + "\\maps\\";
string filename = "";
Stream str = null;
byte[] buf = null;
FileStream fstr = null;
int id;
for( Int64 i = StartID; i <= EndID; i++ )
{
req = (HttpWebRequest)WebRequest.Create( url + i.ToString() );
res = (HttpWebResponse)req.GetResponse();
filename = "";
NewItem = "id = " + i.ToString() + " = " + res.StatusCode.ToString();
int.TryParse( i.ToString(), out id );
Progress = id;
if( res.StatusCode != HttpStatusCode.OK )
continue;
foreach( string r in res.Headers )
{
string s = res.Headers[r];
if( s.IndexOf( "filename" ) != -1 )
{
int start = s.IndexOf( "filename" ) + ("filename").Length + 2,
end = s.Length - start - 1;
filename = s.Substring( start, end );
break;
}
}
NewItem = "completed analyzing headers...";
if( filename == string.Empty )
{
NewItem = "invalid filename... skipping file...";
req.Abort();
res.Close();
continue;
}
NewItem = "filename = \"" + filename + "\"";
NewItem = "write stream to file \"" + dir + filename + "\"";
str = res.GetResponseStream();
buf = new byte[1024];
fstr = new FileStream( dir + filename, FileMode.Create );
StreamExtensions.CopyTo( str, fstr );
str.Close();
fstr.Close();
req.Abort();
res.Close();
NewItem = "completed write to file";
}
}
private void toolStripButton2_Click( object sender, EventArgs e )
{
DownloadThread.Abort();
}
}
public static class StreamExtensions
{
public static void CopyTo( this System.IO.Stream src, System.IO.Stream dest )
{
if( src == null )
throw new System.ArgumentNullException( "src" );
if( dest == null )
throw new System.ArgumentNullException( "dest" );
System.Diagnostics.Debug.Assert( src.CanRead, "src.CanRead" );
System.Diagnostics.Debug.Assert( dest.CanWrite, "dest.CanWrite" );
int readCount;
var buffer = new byte[8192];
while( (readCount = src.Read( buffer, 0, buffer.Length )) != 0 )
dest.Write( buffer, 0, readCount );
}
}