/// <summary> /// Downloads a file from the specified <paramref name="url"/>, /// storing in <paramref name="path"/>, under name <paramref name="uncompressedFileName"/>. /// </summary> /// /// <param name="url">The URL where the file should be downloaded from.</param> /// <param name="path">The path where the file will be stored localy.</param> /// <param name="uncompressedFileName">The generated name of the uncompressed file.</param> /// /// <returns><c>true</c> if the download succeeded, <c>false</c> otherwise.</returns> /// public static bool Download(string url, string path, out string uncompressedFileName) { string name = System.IO.Path.GetFileName(url); string downloadedFileName = System.IO.Path.Combine(path, name); if (!File.Exists(downloadedFileName)) { Directory.CreateDirectory(path); using (var client = new WebClient()) client.DownloadFile(url, downloadedFileName); } // If the file is compressed, decompress it to disk if (downloadedFileName.EndsWith(".bz2", StringComparison.InvariantCultureIgnoreCase)) { uncompressedFileName = downloadedFileName.Remove(downloadedFileName.Length - 4); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFileName, FileMode.Open)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew)) { BZip2.Decompress(compressedFile, uncompressedFile, false); } } } else if (downloadedFileName.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase)) { uncompressedFileName = downloadedFileName.Remove(downloadedFileName.Length - 3); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFileName, FileMode.Open)) using (var decompressedFile = new GZipInputStream(compressedFile)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew)) { decompressedFile.CopyTo(uncompressedFile); } } } else if (downloadedFileName.EndsWith(".Z", StringComparison.InvariantCultureIgnoreCase)) { uncompressedFileName = downloadedFileName.Remove(downloadedFileName.Length - 2); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFileName, FileMode.Open)) using (var decompressedFile = new Accord.IO.Compression.LzwInputStream(compressedFile)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew)) { decompressedFile.CopyTo(uncompressedFile); } } } else { uncompressedFileName = downloadedFileName; } return(true); }
/// <summary> /// Downloads a file from the specified <paramref name="url"/>, /// storing in <paramref name="localPath"/>, under name <paramref name="uncompressedFileName"/>. /// </summary> /// /// <param name="url">The URL where the file should be downloaded from.</param> /// <param name="localPath">The path where the file will be stored localy.</param> /// <param name="localFileName">The local file name to be used for the download.</param> /// <param name="uncompressedFileName">The generated name of the uncompressed file.</param> /// /// <returns><c>true</c> if the download succeeded, <c>false</c> otherwise.</returns> /// public static bool Download(string url, string localPath, string localFileName, out string uncompressedFileName) { if (String.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } if (String.IsNullOrEmpty(localPath)) { throw new ArgumentNullException("localPath"); } if (String.IsNullOrEmpty(localFileName)) { throw new ArgumentNullException("localFileName"); } string downloadedFullFilePath = System.IO.Path.Combine(localPath, localFileName); if (!File.Exists(downloadedFullFilePath)) { Directory.CreateDirectory(localPath); using (var client = ExtensionMethods.NewWebClient()) client.DownloadFileWithRetry(url, downloadedFullFilePath); } // If the file is compressed, decompress it to disk if (endsWith(localFileName, ".bz2")) { uncompressedFileName = downloadedFullFilePath.Remove(downloadedFullFilePath.Length - 4); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFullFilePath, FileMode.Open, FileAccess.Read)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew, FileAccess.Write)) { BZip2.Decompress(compressedFile, uncompressedFile, false); } } } else if (endsWith(localFileName, ".gz")) { uncompressedFileName = downloadedFullFilePath.Remove(downloadedFullFilePath.Length - 3); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFullFilePath, FileMode.Open, FileAccess.Read)) using (var decompressedFile = new GZipInputStream(compressedFile)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew, FileAccess.Write)) { decompressedFile.CopyTo(uncompressedFile); } } } else if (endsWith(downloadedFullFilePath, ".Z")) { uncompressedFileName = downloadedFullFilePath.Remove(downloadedFullFilePath.Length - 2); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFullFilePath, FileMode.Open, FileAccess.Read)) using (var decompressedFile = new Accord.IO.Compression.LzwInputStream(compressedFile)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew, FileAccess.Write)) { decompressedFile.CopyTo(uncompressedFile); } } } else { uncompressedFileName = downloadedFullFilePath; } return(true); }