单击 url 下载 gz 文件并使用 scala 转换为 csv

download gz file on clicking a url and convert to csv using scala

我真的搞砸了这里的语法需要帮助...

我有一个 URL,点击它会下载一个 sample.csv.gz 文件

请有人帮我填补下面的句法空白:

val outputFile = "C:\sampleNew" + ".csv"
val inputFile = "C:\sample.csv.gz"
val fileUrl = "someSamplehttpUrl"

// On hitting this Url, sample.csv.gz file should download at destination 'outputFile'
val in = new URL()(fileUrl).openStream()
Files.copy(in, Paths.get(outputFile), StandardCopyOption.REPLACE_EXISTING)

val filePath = new File(outputFile)
if(filePath.exists()) filePath.delete()
val fw = new FileWriter(outputFile, true)
var bf = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(inputFile)), "UTF-8"))
while (bf.ready()) fw.append(bf.readLine() + "\n")

我遇到了几个语法错误...这里有任何更正吗?我基本上有一个 http get 请求 returns a URL,我必须打开它才能下载这个 gz 文件

谢谢!

这里有两种可能的解决方案:

import java.io.{File, PrintWriter}
import scala.io.Source

val outputFile = "out.csv"
val inputFile = "/tmp/marks.csv"
val fileUrl = s"file:///$inputFile"

// Method 1, a traditional copy from the input to the output.
val in = Source.fromURL(fileUrl)
val out = new PrintWriter(outputFile)
for (line <- in.getLines)
  out.println(line)

out.close
in.close

这是一个单一的衬垫,它基本上将数据从输入传输到输出。

import sys.process._
import java.net.URL

val outputFile = "out.csv"
val inputFile = "/tmp/marks.csv"
val fileUrl = s"file:///$inputFile"

// Method 2, pipe the content of the URL to the output file.
new URL(fileUrl) #> new File(outputFile) !!

这是一个使用Files.copy

的版本
val outputFile = "out.csv"
val inputFile = "/tmp/marks.csv"
val fileUrl = s"file:///$inputFile"

import java.nio.file.{Files, Paths, StandardCopyOption}
import java.net.URL

val in = new URL(fileUrl).openStream
val out = Paths.get(outputFile)

Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING)

希望以上一项(或多项)能满足您的需求。