Program.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.FtpClient;
  6. using System.Threading;
  7. using System.Collections.Generic;
  8. using System.Text.RegularExpressions;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using System.IO.Compression;
  12. using System.Text;
  13. namespace Tests {
  14. /// <summary>
  15. /// Torture test
  16. /// </summary>
  17. class Program {
  18. static readonly string m_host = "ftptest";
  19. static readonly string m_user = "ftptest";
  20. static readonly string m_pass = "ftptest";
  21. static void Main(string[] args) {
  22. FtpTrace.FlushOnWrite = true;
  23. FtpTrace.AddListener(new ConsoleTraceListener());
  24. try {
  25. /*foreach (int i in new int[] {
  26. (int)FtpDataConnectionType.EPSV,
  27. (int)FtpDataConnectionType.EPRT,
  28. (int)FtpDataConnectionType.PASV,
  29. (int)FtpDataConnectionType.PORT
  30. }) {
  31. using (FtpClient cl = new FtpClient()) {
  32. cl.Credentials = new NetworkCredential(m_user, m_pass);
  33. cl.Host = m_host;
  34. cl.EncryptionMode = FtpEncryptionMode.None;
  35. cl.ValidateCertificate += new FtpSslValidation(cl_ValidateCertificate);
  36. cl.DataConnectionType = (FtpDataConnectionType)i;
  37. //cl.Encoding = System.Text.Encoding.Default;
  38. cl.Connect();
  39. Upload(cl);
  40. Download(cl);
  41. Delete(cl);
  42. }
  43. }*/
  44. //StreamResponses();
  45. //TestServer();
  46. //TestManualEncoding();
  47. //TestServer();
  48. //TestDisposeWithMultipleThreads();
  49. //TestMODCOMP_PWD_Parser();
  50. //TestDispose();
  51. //TestHash();
  52. TestNameListing();
  53. //TestOpenVMSParser();
  54. // TestIISParser();
  55. //GetMicrosoftFTPListing();
  56. //TestReset();
  57. //TestUTF8();
  58. //TestDirectoryWithDots();
  59. //TestUnixListParser();
  60. // TestFileZillaKick();
  61. //TestUnixList();
  62. //TestNetBSDServer();
  63. // TestConnectionFailure();
  64. //TestGetObjectInfo();
  65. //TestFtpPath();
  66. //TestUnixListing();
  67. TestListPath();
  68. }
  69. catch (Exception ex) {
  70. Console.WriteLine(ex.ToString());
  71. }
  72. Console.WriteLine("--DONE--");
  73. Console.ReadKey();
  74. }
  75. static void TestListPath() {
  76. using(FtpClient cl = new FtpClient()) {
  77. cl.Credentials = new NetworkCredential(m_user, m_pass);
  78. cl.Host = m_host;
  79. cl.EncryptionMode = FtpEncryptionMode.None;
  80. cl.GetListing();
  81. Console.WriteLine("Path listing succeeded");
  82. cl.GetListing(null, FtpListOption.NoPath);
  83. Console.WriteLine("No path listing succeeded");
  84. }
  85. }
  86. static void StreamResponses() {
  87. using (FtpClient cl = new FtpClient()) {
  88. cl.Credentials = new NetworkCredential(m_user, m_pass);
  89. cl.Host = m_host;
  90. cl.EncryptionMode = FtpEncryptionMode.None;
  91. cl.ValidateCertificate += new FtpSslValidation(delegate(FtpClient control, FtpSslValidationEventArgs e) {
  92. e.Accept = true;
  93. });
  94. using (FtpDataStream s = (FtpDataStream)cl.OpenWrite("test.txt")) {
  95. FtpReply r = s.CommandStatus;
  96. Console.WriteLine();
  97. Console.WriteLine("Response to STOR:");
  98. Console.WriteLine("Code: {0}", r.Code);
  99. Console.WriteLine("Message: {0}", r.Message);
  100. Console.WriteLine("Informational: {0}", r.InfoMessages);
  101. r = s.Close();
  102. Console.WriteLine();
  103. Console.WriteLine("Response after close:");
  104. Console.WriteLine("Code: {0}", r.Code);
  105. Console.WriteLine("Message: {0}", r.Message);
  106. Console.WriteLine("Informational: {0}", r.InfoMessages);
  107. }
  108. }
  109. }
  110. static void TestUnixListing() {
  111. using (FtpClient cl = new FtpClient()) {
  112. cl.Host = "ftptest";
  113. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  114. if (!cl.FileExists("test.txt")) {
  115. using (Stream s = cl.OpenWrite("test.txt")) {
  116. s.Close();
  117. }
  118. }
  119. foreach (FtpListItem i in cl.GetListing(null, FtpListOption.ForceList)) {
  120. Console.WriteLine(i);
  121. }
  122. }
  123. }
  124. static void TestFtpPath() {
  125. string path = "/home/sigurdhj/errors/16.05.2014/asdasd/asd asd asd aa asd/Kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::asdasd";
  126. Console.WriteLine(path.GetFtpDirectoryName());
  127. Console.WriteLine("./foobar".GetFtpDirectoryName());
  128. Console.WriteLine("foobar".GetFtpDirectoryName());
  129. Console.WriteLine(path.GetFtpFileName());
  130. Console.WriteLine("/foo/bar".GetFtpFileName());
  131. Console.WriteLine("./foo/bar".GetFtpFileName());
  132. Console.WriteLine("./bar".GetFtpFileName());
  133. Console.WriteLine("/bar".GetFtpFileName());
  134. Console.WriteLine("bar".GetFtpFileName());
  135. }
  136. static void TestGetObjectInfo() {
  137. using (FtpClient cl = new FtpClient()) {
  138. FtpListItem item;
  139. cl.Host = "ftptest";
  140. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  141. cl.Encoding = Encoding.Default;
  142. item = cl.GetObjectInfo("/Examples/OpenRead.cs");
  143. Console.WriteLine(item.ToString());
  144. }
  145. }
  146. static void TestManualEncoding() {
  147. using (FtpClient cl = new FtpClient()) {
  148. cl.Host = "ftptest";
  149. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  150. cl.Encoding = Encoding.Default;
  151. using (Stream s = cl.OpenWrite("test.txt")) {
  152. s.Close();
  153. }
  154. }
  155. }
  156. static void TestServer() {
  157. using (FtpClient cl = new FtpClient()) {
  158. cl.Host = "ftptest";
  159. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  160. cl.EncryptionMode = FtpEncryptionMode.Explicit;
  161. cl.ValidateCertificate += (control, e) => {
  162. e.Accept = true;
  163. };
  164. foreach (FtpListItem i in cl.GetListing("/")) {
  165. Console.WriteLine(i.FullName);
  166. }
  167. }
  168. }
  169. static void TestServerDownload(FtpClient client, string path) {
  170. foreach (FtpListItem i in client.GetListing(path)) {
  171. switch (i.Type) {
  172. case FtpFileSystemObjectType.Directory:
  173. TestServerDownload(client, i.FullName);
  174. break;
  175. case FtpFileSystemObjectType.File:
  176. using (Stream s = client.OpenRead(i.FullName)) {
  177. byte[] b = new byte[8192];
  178. int read = 0;
  179. long total = 0;
  180. try {
  181. while ((read = s.Read(b, 0, b.Length)) > 0) {
  182. total += read;
  183. Console.Write("\r{0}/{1} {2:p} ",
  184. total, s.Length, (double)total / (double)s.Length);
  185. }
  186. Console.Write("\r{0}/{1} {2:p} ",
  187. total, s.Length, (double)total / (double)s.Length);
  188. }
  189. finally {
  190. Console.WriteLine();
  191. }
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. static void cl_ValidateCertificate(FtpClient control, FtpSslValidationEventArgs e) {
  198. e.Accept = true;
  199. }
  200. static void TestDisposeWithMultipleThreads() {
  201. using (FtpClient cl = new FtpClient()) {
  202. cl.Host = "ftp.netbsd.org";
  203. cl.Credentials = new NetworkCredential("ftp", "ftp");
  204. Thread t1 = new Thread(() => {
  205. cl.GetListing();
  206. });
  207. Thread t2 = new Thread(() => {
  208. cl.Dispose();
  209. });
  210. t1.Start();
  211. Thread.Sleep(500);
  212. t2.Start();
  213. t1.Join();
  214. t2.Join();
  215. }
  216. }
  217. static void TestConnectionFailure() {
  218. try {
  219. using (FtpClient cl = new FtpClient()) {
  220. cl.Credentials = new NetworkCredential("ftp", "ftp");
  221. cl.Host = "somefakehost";
  222. cl.ConnectTimeout = 5000;
  223. cl.Connect();
  224. }
  225. }
  226. catch (Exception e) {
  227. Console.WriteLine("Caught connection faillure: {0}", e.Message);
  228. }
  229. }
  230. static void TestNetBSDServer() {
  231. using (FtpClient client = new FtpClient()) {
  232. client.Credentials = new NetworkCredential("ftp", "ftp");
  233. client.Host = "ftp.netbsd.org";
  234. foreach (FtpListItem item in client.GetListing(null,
  235. FtpListOption.ForceList | FtpListOption.Modify | FtpListOption.DerefLinks)) {
  236. Console.WriteLine(item);
  237. if (item.Type == FtpFileSystemObjectType.Link && item.LinkObject != null)
  238. Console.WriteLine(item.LinkObject);
  239. }
  240. }
  241. }
  242. static void TestUnixList() {
  243. using (FtpClient client = new FtpClient()) {
  244. client.Credentials = new NetworkCredential("ftptest", "ftptest");
  245. client.Host = "ftptest";
  246. foreach (FtpListItem i in client.GetListing(null, FtpListOption.ForceList | FtpListOption.Recursive)) {
  247. Console.WriteLine(i.FullName);
  248. }
  249. }
  250. }
  251. static void TestFileZillaKick() {
  252. using (FtpClient cl = new FtpClient()) {
  253. cl.Host = "localhost";
  254. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  255. cl.EnableThreadSafeDataConnections = false;
  256. if (cl.FileExists("TestFile.txt"))
  257. cl.DeleteFile("TestFile.txt");
  258. try {
  259. Stream s = cl.OpenWrite("TestFile.txt");
  260. for (int i = 0; true; i++) {
  261. s.WriteByte((byte)i);
  262. Thread.Sleep(100);
  263. }
  264. //s.Close();
  265. }
  266. catch (FtpCommandException ex) {
  267. Console.WriteLine("Exception caught!");
  268. Console.WriteLine(ex.ToString());
  269. }
  270. }
  271. }
  272. static void TestUnixListParser() {
  273. string[] sample = new string[] {
  274. "drwxr-xr-x 7 user1 user1 512 Sep 27 2011 .",
  275. "drwxr-xr-x 31 user1 user1 1024 Sep 27 2011 ..",
  276. "lrwxrwxrwx 1 user1 user1 9 Sep 27 2011 data.0000 -> data.6460",
  277. "drwxr-xr-x 10 user1 user1 512 Jun 29 2012 data.6460",
  278. "lrwxrwxrwx 1 user1 user1 8 Sep 27 2011 sys.0000 -> sys.6460",
  279. "drwxr-xr-x 133 user1 user1 4096 Jun 25 16:26 sys.6460"
  280. };
  281. foreach (string s in sample) {
  282. FtpListItem item = FtpListItem.Parse("/", s, FtpCapability.NONE);
  283. if (item != null)
  284. Console.WriteLine(item);
  285. }
  286. }
  287. static void TestDirectoryWithDots() {
  288. using (FtpClient cl = new FtpClient()) {
  289. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  290. cl.Host = "localhost";
  291. cl.Connect();
  292. // FTP server set to timeout after 5 seconds.
  293. //Thread.Sleep(6000);
  294. cl.GetListing("Test.Directory", FtpListOption.ForceList);
  295. cl.SetWorkingDirectory("Test.Directory");
  296. cl.GetListing(null, FtpListOption.ForceList);
  297. }
  298. }
  299. static void TestDispose() {
  300. using (FtpClient cl = new FtpClient()) {
  301. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  302. cl.Host = "localhost";
  303. cl.Connect();
  304. // FTP server set to timeout after 5 seconds.
  305. //Thread.Sleep(6000);
  306. foreach (FtpListItem item in cl.GetListing()) {
  307. }
  308. }
  309. }
  310. static void TestHash() {
  311. using (FtpClient cl = new FtpClient()) {
  312. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  313. cl.Host = "localhost";
  314. cl.Connect();
  315. Console.WriteLine("Supported HASH algorithms: {0}", cl.HashAlgorithms);
  316. Console.WriteLine("Current HASH algorithm: {0}", cl.GetHashAlgorithm());
  317. foreach (FtpHashAlgorithm alg in Enum.GetValues(typeof(FtpHashAlgorithm))) {
  318. if (alg != FtpHashAlgorithm.NONE && cl.HashAlgorithms.HasFlag(alg)) {
  319. FtpHash hash = null;
  320. cl.SetHashAlgorithm(alg);
  321. hash = cl.GetHash("LICENSE.TXT");
  322. if (hash.IsValid) {
  323. Debug.Assert(hash.Verify(@"C:\FTPTEST\LICENSE.TXT"), "The computed hash didn't match or the hash object was invalid!");
  324. }
  325. }
  326. }
  327. }
  328. }
  329. static void TestReset() {
  330. using (FtpClient cl = new FtpClient()) {
  331. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  332. cl.Host = "localhost";
  333. cl.Connect();
  334. using (Stream istream = cl.OpenRead("LICENSE.TXT", 10)) {
  335. istream.Close();
  336. }
  337. }
  338. }
  339. static void GetMicrosoftFTPListing() {
  340. using (FtpClient cl = new FtpClient()) {
  341. cl.Credentials = new NetworkCredential("ftp", "ftp");
  342. cl.Host = "ftp.microsoft.com";
  343. cl.Connect();
  344. Console.WriteLine(cl.Capabilities);
  345. foreach (FtpListItem item in cl.GetListing(null, FtpListOption.Modify)) {
  346. Console.WriteLine(item.Modified);
  347. }
  348. }
  349. }
  350. static void TestIISParser() {
  351. string[] sample = new string[] {
  352. "03-07-13 10:02AM 901 File01.xml",
  353. "03-07-13 10:03AM 921 File02.xml",
  354. "03-07-13 10:04AM 904 File03.xml",
  355. "03-07-13 10:04AM 912 File04.xml",
  356. "03-08-13 11:10AM 912 File05.xml",
  357. "03-15-13 02:38PM 912 File06.xml",
  358. "03-07-13 10:16AM 909 File07.xml",
  359. "03-07-13 10:16AM 899 File08.xml",
  360. "03-08-13 10:22AM 904 File09.xml",
  361. "03-25-13 07:27AM 895 File10.xml",
  362. "03-08-13 10:22AM 6199 File11.txt",
  363. "03-25-13 07:22AM 31444 File12.txt",
  364. "03-25-13 07:24AM 24537 File13.txt"
  365. };
  366. foreach (string s in sample) {
  367. FtpListItem item = FtpListItem.Parse("/", s, 0);
  368. if (item != null) {
  369. Console.WriteLine(item.Modified);
  370. //Console.WriteLine(item);
  371. }
  372. }
  373. }
  374. static void TestOpenVMSParser() {
  375. string[] sample = new string[] {
  376. "411_4114.TXT;1 11 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  377. "ACT_CC_NAME_4114.TXT;1 30 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  378. "ACT_CC_NUM_4114.TXT;1 30 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  379. "ACT_CELL_NAME_4114.TXT;1 113 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  380. "ACT_CELL_NUM_4114.TXT;1 113 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  381. "AGCY_BUDG_4114.TXT;1 63 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  382. "CELL_SUMM_4114.TXT;1 125 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  383. "CELL_SUMM_CHART_4114.PDF;2 95 21-MAR-2012 10:58 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  384. "DET_4114.TXT;1 17472 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  385. "DET_4114_000.TXT;1 777 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  386. "DET_4114_001.TXT;1 254 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  387. "DET_4114_003.TXT;1 21 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  388. "DET_4114_006.TXT;1 22 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  389. "DET_4114_101.TXT;1 431 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  390. "DET_4114_121.TXT;1 2459 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  391. "DET_4114_124.TXT;1 4610 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  392. "DET_4114_200.TXT;1 936 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
  393. "TEL_4114.TXT;1 1178 21-MAR-2012 15:19 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)"
  394. };
  395. foreach (string s in sample) {
  396. FtpListItem item = FtpListItem.Parse("disk$user520:[4114.2012.Jan]", s, 0);
  397. if (item != null) {
  398. Console.WriteLine(item.Modified);
  399. //Console.WriteLine(item);
  400. }
  401. }
  402. }
  403. static void TestMODCOMP_PWD_Parser() {
  404. string response = "PWD = ~TNA=AMP,VNA=VOL03,FNA=U-ED-B2-USL";
  405. Match m;
  406. if ((m = Regex.Match(response, "PWD = (?<pwd>.*)")).Success)
  407. Console.WriteLine("PWD: {0}", m.Groups["pwd"].Value);
  408. }
  409. static void TestNameListing() {
  410. using (FtpClient cl = new FtpClient()) {
  411. cl.Credentials = new NetworkCredential(m_user, m_pass);
  412. cl.Host = "127.0.0.1";
  413. cl.ValidateCertificate += OnValidateCertificate;
  414. cl.DataConnectionType = FtpDataConnectionType.PASV;
  415. //cl.EncryptionMode = FtpEncryptionMode.Explicit;
  416. //cl.SocketPollInterval = 5000;
  417. cl.Connect();
  418. //Console.WriteLine("Sleeping for 10 seconds to force timeout.");
  419. //Thread.Sleep(10000);
  420. foreach (FtpListItem item in cl.GetListing(null, FtpListOption.SizeModify | FtpListOption.ForceNameList)) {
  421. Console.WriteLine(item.Modified.Kind);
  422. Console.WriteLine(item.Modified);
  423. }
  424. }
  425. }
  426. static FtpClient Connect() {
  427. List<Thread> threads = new List<Thread>();
  428. FtpClient cl = new FtpClient();
  429. cl.ValidateCertificate += OnValidateCertificate;
  430. //cl.EncryptionMode = FtpEncryptionMode.Explicit;
  431. for (int i = 0; i < 10; i++) {
  432. int count = i;
  433. Thread t = new Thread(new ThreadStart(delegate() {
  434. cl.Credentials = new NetworkCredential(m_user, m_pass);
  435. cl.Host = m_host;
  436. cl.Connect();
  437. for (int j = 0; j < 10; j++)
  438. cl.Execute("NOOP");
  439. if (count % 2 == 0)
  440. cl.Disconnect();
  441. }));
  442. t.Start();
  443. threads.Add(t);
  444. }
  445. while (threads.Count > 0) {
  446. threads[0].Join();
  447. threads.RemoveAt(0);
  448. }
  449. return cl;
  450. }
  451. static void OnValidateCertificate(FtpClient control, FtpSslValidationEventArgs e) {
  452. e.Accept = true;
  453. }
  454. static void Upload(FtpClient cl) {
  455. string root = @"..\..\..";
  456. List<Thread> threads = new List<Thread>();
  457. foreach (string s in Directory.GetFiles(root, "*", SearchOption.AllDirectories)) {
  458. string file = s;
  459. if (file.Contains(@"\.git"))
  460. continue;
  461. Thread t = new Thread(new ThreadStart(delegate() {
  462. DoUpload(cl, root, file);
  463. }));
  464. t.Start();
  465. threads.Add(t);
  466. }
  467. while (threads.Count > 0) {
  468. threads[0].Join();
  469. threads.RemoveAt(0);
  470. }
  471. }
  472. static void DoUpload(FtpClient cl, string root, string s) {
  473. FtpDataType type = FtpDataType.Binary;
  474. string path = Path.GetDirectoryName(s).Replace(root, "");
  475. string name = Path.GetFileName(s);
  476. if (Path.GetExtension(s).ToLower() == ".cs" || Path.GetExtension(s).ToLower() == ".txt")
  477. type = FtpDataType.ASCII;
  478. if (!cl.DirectoryExists(path))
  479. cl.CreateDirectory(path, true);
  480. else if (cl.FileExists(string.Format("{0}/{1}", path, name)))
  481. cl.DeleteFile(string.Format("{0}/{1}", path, name));
  482. using (
  483. Stream istream = new FileStream(s, FileMode.Open, FileAccess.Read),
  484. ostream = cl.OpenWrite(s.Replace(root, ""), type)) {
  485. byte[] buf = new byte[8192];
  486. int read = 0;
  487. try {
  488. while ((read = istream.Read(buf, 0, buf.Length)) > 0) {
  489. ostream.Write(buf, 0, read);
  490. }
  491. }
  492. finally {
  493. ostream.Close();
  494. istream.Close();
  495. }
  496. if (cl.HashAlgorithms != FtpHashAlgorithm.NONE) {
  497. Debug.Assert(cl.GetHash(s.Replace(root, "")).Verify(s), "The computed hashes don't match!");
  498. }
  499. }
  500. /*if (!cl.GetHash(s.Replace(root, "")).Verify(s))
  501. throw new Exception("Hashes didn't match!");*/
  502. }
  503. static void Download(FtpClient cl) {
  504. List<Thread> threads = new List<Thread>();
  505. Download(threads, cl, "/");
  506. while (threads.Count > 0) {
  507. threads[0].Join();
  508. lock (threads) {
  509. threads.RemoveAt(0);
  510. }
  511. }
  512. }
  513. static void Download(List<Thread> threads, FtpClient cl, string path) {
  514. foreach (FtpListItem item in cl.GetListing(path)) {
  515. if (item.Type == FtpFileSystemObjectType.Directory)
  516. Download(threads, cl, item.FullName);
  517. else if (item.Type == FtpFileSystemObjectType.File) {
  518. string file = item.FullName;
  519. Thread t = new Thread(new ThreadStart(delegate() {
  520. DoDownload(cl, file);
  521. }));
  522. t.Start();
  523. lock (threads) {
  524. threads.Add(t);
  525. }
  526. }
  527. }
  528. }
  529. static void DoDownload(FtpClient cl, string file) {
  530. using (Stream s = cl.OpenRead(file)) {
  531. byte[] buf = new byte[8192];
  532. try {
  533. while (s.Read(buf, 0, buf.Length) > 0) ;
  534. }
  535. finally {
  536. s.Close();
  537. }
  538. }
  539. }
  540. static void Delete(FtpClient cl) {
  541. DeleteDirectory(cl, "/");
  542. }
  543. static void DeleteDirectory(FtpClient cl, string path) {
  544. foreach (FtpListItem item in cl.GetListing(path)) {
  545. if (item.Type == FtpFileSystemObjectType.File) {
  546. cl.DeleteFile(item.FullName);
  547. }
  548. else if (item.Type == FtpFileSystemObjectType.Directory) {
  549. DeleteDirectory(cl, item.FullName);
  550. cl.DeleteDirectory(item.FullName);
  551. }
  552. }
  553. }
  554. static void TestUTF8() {
  555. // the following file name was reported in the discussions as having
  556. // problems:
  557. // https://netftp.codeplex.com/discussions/445090
  558. string filename = "Verbundmörtel Zubehör + Technische Daten DE.pdf";
  559. using (FtpClient cl = new FtpClient()) {
  560. cl.Host = "localhost";
  561. cl.Credentials = new NetworkCredential("ftptest", "ftptest");
  562. cl.DataConnectionType = FtpDataConnectionType.PASV;
  563. cl.InternetProtocolVersions = FtpIpVersion.ANY;
  564. using (Stream ostream = cl.OpenWrite(filename)) {
  565. StreamWriter writer = new StreamWriter(filename);
  566. writer.WriteLine(filename);
  567. writer.Close();
  568. }
  569. }
  570. }
  571. }
  572. }